Python: Sorting - Bubblesort Damian Gordon. Sorting: Bubblesort The simplest algorithm for sort an array is called BUBBLE SORT. It works as follows for.

Slides:



Advertisements
Similar presentations
Bubble Sort Algorithm 1.Initialize the size of the list to be sorted to be the actual size of the list. 2.Loop through the list until no element needs.
Advertisements

Decision Maths 1 Sorting Algorithms Bubble Sort A V Ali : 1.Start at the beginning of the data set. 2.Compare the first two elements,
CS 106 Introduction to Computer Science I 02 / 29 / 2008 Instructor: Michael Eckmann.
Simple Sorting Algorithms
Overview Sort – placing data in an array in some order (usually decreasing or increasing order) Bubble Sort More efficient bubble sort.
Bubble Sort Merge Sort. Bubble Sort Sorting Sorting takes an unordered collection and makes it an ordered one
CS 106 Introduction to Computer Science I 02 / 28 / 2007 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 03 / 07 / 2008 Instructor: Michael Eckmann.
Computer Programming Sorting and Sorting Algorithms 1.
CS 106 Introduction to Computer Science I 03 / 03 / 2008 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 03 / 08 / 2010 Instructor: Michael Eckmann.
Searching Arrays Linear search Binary search small arrays
Searching and Sorting Arrays
CS 106 Introduction to Computer Science I 10 / 15 / 2007 Instructor: Michael Eckmann.
CS 106 Introduction to Computer Science I 10 / 16 / 2006 Instructor: Michael Eckmann.
Simple Sorting Algorithms. 2 Bubble sort Compare each element (except the last one) with its neighbor to the right If they are out of order, swap them.
Simple Sorting Algorithms. 2 Outline We are going to look at three simple sorting techniques: Bubble Sort, Selection Sort, and Insertion Sort We are going.
Describing algorithms in pseudo code To describe algorithms we need a language which is: – less formal than programming languages (implementation details.
Week 11 Sorting Algorithms. Sorting Sorting Algorithms A sorting algorithm is an algorithm that puts elements of a list in a certain order. We need sorting.
Examples using Arrays. Summing Squares Problem: To compute the sum of the squares of N numbers N is given N values are also given These should be read.
Searching Damian Gordon. Google PageRank Damian Gordon.
# 1# 1 Searching andSorting What is selection sort? What is bubble sort? What is binary search? CS 105 Spring 2010.
Bubble Sort Merge Sort. Bubble Sort Sorting Sorting takes an unordered collection and makes it an ordered one
קורס מחשב לרפואנים הרצאה 8: סיבוכיות ומיון ראובן בר-יהודה. © כל הזכויות שמורות לטכניון – מכון טכנולוגי לישראל.
Analysis of Bubble Sort and Loop Invariant
BUBBLE SORT. Introduction Bubble sort, also known as sinking sort, is a simple sorting algorithm that works by repeatedly stepping through the list to.
1 2. Program Construction in Java. 2.9 Sorting 3 The need Soritng into categories is relatively easy (if, else if, switch); here we consider sorting.
Fundamentals of Algorithms MCS - 2 Lecture # 15. Bubble Sort.
Sorting. 2 contents 3 kinds of sorting methods – Selection, exchange, and insertion O(n 2 ) sorts – VERY inefficient, but OK for ≈ 10 elements – Simple.
Bubble Sort 18 th December 2014 With Mrs
Sorting: Optimising Bubblesort Damian Gordon. Sorting: Bubble Sort If we look at the bubble sort algorithm again:
Sorting Techniques Rizwan Rehman Centre for Computer Studies Dibrugarh University.
Python: Arrays Damian Gordon. Arrays In Python arrays are sometimes called “lists” or “tuple” but we’ll stick to the more commonly used term “array”.
The Bubble Sort by Mr. Dave Clausen La Cañada High School.
Searching & Sorting Programming 2. Searching Searching is the process of determining if a target item is present in a list of items, and locating it A.
Bubble Sort.
CS 162 Intro to Programming II Bubble Sort 1. Compare adjacent elements. If the first is greater than the second, swap them. Do this for each pair of.
Sorting & Searching Review. Selection Sort 1. Find the smallest element 2. Move to the front of the array (swap with front) 3. Repeat Steps 1&2, but ignoring.
Lists in Python Selection Sort Algorithm. Sorting A very common activity for computers Not just in business, sorting is used in databases, graphics, simulations,
Sorting: Selection Sort Damian Gordon. Sorting: Selection Sort OK, so we’ve seen a way of sorting that easy for the computer, now let’s look at a ways.
Arrays An array is a data object that can hold multiple objects, all of the same type. We can think of an array as a storage box which has multiple compartments.
1 Principles of Computer Science I Honors Section Note Set 5 CSE 1341.
CS 106 Introduction to Computer Science I 03 / 02 / 2007 Instructor: Michael Eckmann.
Chapter 9 Sorting. The efficiency of data handling can often be increased if the data are sorted according to some criteria of order. The first step is.
Sorting Sorting takes an unordered array and makes it an ordered one
Insertion Sort while some elements unsorted: Using linear search, find the location in the sorted portion where the 1 st element of the unsorted portion.
CS1022 Computer Programming & Principles Lecture 2.2 Algorithms.
Sorting & Searching Geletaw S (MSC, MCITP). Objectives At the end of this session the students should be able to: – Design and implement the following.
Python: Structured Programming Damian Gordon. Structured Programming Remember the modularised version of the prime number checking program:
Python: Iteration Damian Gordon. Python: Iteration We’ll consider four ways to do iteration: – The WHILE loop – The FOR loop – The DO loop – The LOOP.
Searching Arrays Linear search Binary search small arrays
The Bubble Sort Mr. Dave Clausen La Cañada High School
CS212: Data Structures and Algorithms
Introduction to Python
Week 9 - Monday CS 113.
Python: Advanced Sorting Algorithms
Sorting Algorithms.
Linear and Binary Search
Boolean Logic Damian Gordon.
And now for something completely different . . .
Straight Selection Sort
Lecture 16 Bubble Sort Merge Sort.
Simple Statistics on Arrays
Bubble Sort Example 9, 6, 2, 12, 11, 9, 3, 7 6, 9, 2, 12, 11, 9, 3, 7 Bubblesort compares the numbers in pairs from left to right exchanging.
Sorting Damian Gordon.
Search,Sort,Recursion.
Simple Sorting Algorithms
Python: Sorting – Selection Sort
Simple Sorting Algorithms
Module 8 – Searching & Sorting Algorithms
Presentation transcript:

Python: Sorting - Bubblesort Damian Gordon

Sorting: Bubblesort The simplest algorithm for sort an array is called BUBBLE SORT. It works as follows for an array of size N: – Look at the first and second element Are they in order? If so, do nothing If not, swap them around – Look at the second and third element Do the same – Keep doing this until you get to the end of the array – Go back to the start again keep doing this whole process for N times.

# PROGRAM Bubblesort: Age = [44, 23, 42, 33, 18, 54, 34, 16] for outerindex in range(0,len(Age)): # DO for index in range(0,len(Age)-1): # DO if Age[index+1] < Age[index]: # THEN TempValue = Age[index+1] Age[index+1] = Age[index] Age[index] = TempValue # ENDIF; # ENDFOR; print(Age) # END.

Sorting: Bubblesort The bubble sort pushes the largest values up to the top of the array.

Sorting: Bubblesort So each time around the loop the amount of the array that is sorted is increased, and we don’t have to check for swaps in the locations that have already been sorted. So we reduce the checking of swaps by one each time we do a pass of the array.

# PROGRAM BetterBubblesort: Age = [44, 23, 42, 33, 18, 54, 34, 16] reducingindex = len(Age)-1 for outerindex in range(0,len(Age)): # DO for index in range(0,reducingindex): # DO if Age[index+1] < Age[index]: # THEN TempValue = Age[index+1] Age[index+1] = Age[index] Age[index] = TempValue # ENDIF; # ENDFOR; reducingindex = reducingindex - 1 # ENDFOR; print(Age) # END.

# PROGRAM BetterBubblesort: Age = [44, 23, 42, 33, 18, 54, 34, 16] reducingindex = len(Age)-1 for outerindex in range(0,len(Age)): # DO for index in range(0,reducingindex): # DO if Age[index+1] < Age[index]: # THEN TempValue = Age[index+1] Age[index+1] = Age[index] Age[index] = TempValue # ENDIF; # ENDFOR; reducingindex = reducingindex - 1 # ENDFOR; print(Age) # END.

Sorting: Bubblesort Also, what if the data is already sorted? We should check if the program has done no swaps in one pass, and if t doesn’t that means the data is sorted. So even if the data started unsorted, as soon as the data gets sorted we want to exit the program

# PROGRAM BetterBubblesortBoolean: Age = [44, 23, 42, 33, 18, 54, 34, 16] reducingindex = len(Age)-1 DidSwap = False Part 1 of 2

# PROGRAM BetterBubblesortBoolean: Age = [44, 23, 42, 33, 18, 54, 34, 16] reducingindex = len(Age)-1 DidSwap = False Part 1 of 2

for outerindex in range(0,len(Age)): # DO for index in range(0,reducingindex): # DO if Age[index+1] < Age[index]: # THEN TempValue = Age[index+1] Age[index+1] = Age[index] Age[index] = TempValue DidSwap = True # ENDIF; # ENDFOR; reducingindex = reducingindex - 1 if DidSwap == False: break # ENDFOR; print(Age) # END. Part 2 of 2

for outerindex in range(0,len(Age)): # DO for index in range(0,reducingindex): # DO if Age[index+1] < Age[index]: # THEN TempValue = Age[index+1] Age[index+1] = Age[index] Age[index] = TempValue DidSwap = True # ENDIF; # ENDFOR; reducingindex = reducingindex - 1 if DidSwap == False: break # ENDFOR; print(Age) # END. Part 2 of 2

Sorting: Bubblesort The Swap function is very useful so we should have that as a module as follows:

# PROGRAM BetterBubblesortBooleanModule: def Swap(a,b): TempValue = b b = a a = TempValue return a, b # END Swap Age = [44, 23, 42, 33, 18, 54, 34, 16] reducingindex = len(Age)-1 DidSwap = False Part 1 of 2

# PROGRAM BetterBubblesortBooleanModule: def Swap(a,b): TempValue = b b = a a = TempValue return a, b # END Swap Age = [44, 23, 42, 33, 18, 54, 34, 16] reducingindex = len(Age)-1 DidSwap = False Part 1 of 2

for outerindex in range(0,len(Age)): # DO for index in range(0,reducingindex): # DO if Age[index+1] < Age[index]: # THEN Age[index],Age[index+1] = Swap(Age[index],Age[index+1]) DidSwap = True # ENDIF; # ENDFOR; reducingindex = reducingindex - 1 if DidSwap == False: break # ENDFOR; print(Age) # END. Part 2 of 2

for outerindex in range(0,len(Age)): # DO for index in range(0,reducingindex): # DO if Age[index+1] < Age[index]: # THEN Age[index],Age[index+1] = Swap(Age[index],Age[index+1]) DidSwap = True # ENDIF; # ENDFOR; reducingindex = reducingindex - 1 if DidSwap == False: break # ENDFOR; print(Age) # END. Part 2 of 2

Sorting: Bubblesort Python is such a neat language it allows a much easier swap, you can say: a, b = b, a

Sorting: Bubblesort or: Age[index],Age[index+1] = Age[index+1],Age[index]

# PROGRAM BetterBubblesortBooleanSwap: Age = [44, 23, 42, 33, 18, 54, 34, 16] reducingindex = len(Age)-1 DidSwap = False for outerindex in range(0,len(Age)): # DO for index in range(0,reducingindex): # DO if Age[index+1] < Age[index]: # THEN Age[index],Age[index+1] = Age[index+1],Age[index] DidSwap = True # ENDIF; # ENDFOR; reducingindex = reducingindex - 1 if DidSwap == False: break # ENDFOR; print(Age) # END.

# PROGRAM BetterBubblesortBooleanSwap: Age = [44, 23, 42, 33, 18, 54, 34, 16] reducingindex = len(Age)-1 DidSwap = False for outerindex in range(0,len(Age)): # DO for index in range(0,reducingindex): # DO if Age[index+1] < Age[index]: # THEN Age[index],Age[index+1] = Age[index+1],Age[index] DidSwap = True # ENDIF; # ENDFOR; reducingindex = reducingindex - 1 if DidSwap == False: break # ENDFOR; print(Age) # END.

etc.