Sorting
What is Sorting? An list A of N numbers is sorted in ascending order if the array entries increase (or never decrease) as indices increase:
} } Sorting Algorithms There are many strategies for sorting arrays. Among them: Bubble sort Selection sort Insertion sort Shell sort Merge sort Quicksort } Horrible } Not too bad } Among the very best
Notation Let A be an list of length N Let last be an index in the range of the list: A[0..last] denotes the portion of the array consisting of A[0], A[1], …, A[last]
A Simple Sorting Strategy for (last = N -1; last >= 1; last --) { Move the largest entry in A[0…last] to A[last] }
A Simple Sorting Strategy How it works on 15 35 20 10 25. Portion of array already sorted in orange. Largest value in unsorted portion of array in bright blue. 15 35 20 10 25 Move largest of A[0..4] to A[4]: 15 20 10 25 35 Move largest of A[0..3] to A[3]: 15 20 10 25 35 Move largest of A[0..2] to A[2]: 15 10 20 25 35 Move largest of A[0..1] to A[1]: 10 15 20 25 35 for (last = 4; last >= 1; last--) { Move largest of A[0..last] to A[last] }
Selection and Bubble Sort Selection and Bubble Sort are similar: both use the Simple Sorting Strategy of the previous slide. Selection and Bubble Sort differ in how they implement the step: Move the largest of A[0..last] to A[last]
Bubble Sort Bubble Sort moves the largest entry to the end of A[0..last] by comparing and swapping adjacent elements as an index sweeps through the unsorted portion of the array: 15 35 20 10 25 //Compare A[0], A[1], no swap 15 35 20 10 25 //Compare A[1], A[2], swap 15 20 35 10 25 15 20 35 10 25 //Compare A[2], A[3], swap 15 20 10 35 25 15 20 10 35 25 //Compare A[3], A[4], swap 15 20 10 25 35 //Largest is at A[4]
Bubble Sort 3 7 5 2 6 1 4
BubbleSort 3 7 5 2 6 1 4 ^ ^
3 7 5 2 6 1 4 3 7 5 2 6 1 4 ^ ^
3 7 5 2 6 1 4 3 7 5 2 6 1 4 3 5 7 2 6 1 4 ^ ^
3 7 5 2 6 1 4 3 7 5 2 6 1 4 3 5 7 2 6 1 4 3 5 2 7 6 1 4 ^ ^
3 7 5 2 6 1 4 3 7 5 2 6 1 4 3 5 7 2 6 1 4 3 5 2 7 6 1 4 3 5 2 6 7 1 4 ^ ^
3 7 5 2 6 1 4 3 7 5 2 6 1 4 3 5 7 2 6 1 4 3 5 2 7 6 1 4 3 5 2 6 7 1 4 3 5 2 6 1 7 4 ^ ^
End of Pass 1 3 7 5 2 6 1 4 Note that the last element must be the biggest 3 7 5 2 6 1 4 3 5 7 2 6 1 4 3 5 2 7 6 1 4 3 5 2 6 7 1 4 3 5 2 6 1 7 4 3 5 2 6 1 4 7
Bubble Sort: Pass 2 3 5 2 6 1 4 7 ^ ^
Bubble Sort: Pass 2 3 5 2 6 1 4 7 3 5 2 6 1 4 7 ^ ^
Bubble Sort: Pass 2 3 5 2 6 1 4 7 3 5 2 6 1 4 7 3 2 5 6 1 4 7 ^ ^
Bubble Sort: Pass 2 3 5 2 6 1 4 7 3 5 2 6 1 4 7 3 2 5 6 1 4 7 3 2 5 6 1 4 7 ^ ^
Bubble Sort: Pass 2 3 5 2 6 1 4 7 3 5 2 6 1 4 7 3 2 5 6 1 4 7 3 2 5 6 1 4 7 3 2 5 1 6 4 7 ^ ^
Bubble Sort: Pass 2 3 5 2 6 1 4 7 3 5 2 6 1 4 7 3 2 5 6 1 4 7 3 2 5 6 1 4 7 3 2 5 1 6 4 7 3 2 5 1 4 6 7 Note that the last 2 elements are the largest and in order
Bubble Sort: Pass 3 3 2 5 1 4 6 7 ^ ^
Bubble Sort: Pass 3 3 2 5 1 4 6 7 2 3 5 1 4 6 7 ^ ^
Bubble Sort: Pass 3 3 2 5 1 4 6 7 2 3 5 1 4 6 7 2 3 5 1 4 6 7 ^ ^
Bubble Sort: Pass 3 3 2 5 1 4 6 7 2 3 5 1 4 6 7 2 3 5 1 4 6 7 2 3 1 5 4 6 7 ^ ^
Bubble Sort: Pass 3 3 2 5 1 4 6 7 2 3 5 1 4 6 7 2 3 5 1 4 6 7 2 3 1 5 4 6 7 2 3 1 4 5 6 7 The last 3 elements are the largest and in order
Bubble Sort: Pass 4 2 3 1 4 5 6 7 ^ ^
Bubble Sort: Pass 4 2 3 1 4 5 6 7 2 3 1 4 5 6 7 ^ ^
Bubble Sort: Pass 4 2 3 1 4 5 6 7 2 3 1 4 5 6 7 2 1 3 4 5 6 7 ^ ^
Bubble Sort: Pass 4 2 3 1 4 5 6 7 2 3 1 4 5 6 7 2 1 3 4 5 6 7 2 1 3 4 5 6 7 The last 4 elements are the largest and in order
Bubble Sort: Pass 5 2 3 1 4 5 6 7 2 3 1 4 5 6 7 2 1 3 4 5 6 7 2 1 3 4 5 6 7 ^ ^
Bubble Sort: Pass 5 2 3 1 4 5 6 7 2 3 1 4 5 6 7 2 1 3 4 5 6 7 2 1 3 4 5 6 7 1 2 3 4 5 6 7 ^ ^
Bubble Sort: Pass 5 2 3 1 4 5 6 7 2 3 1 4 5 6 7 2 1 3 4 5 6 7 2 1 3 4 5 6 7 1 2 3 4 5 6 7 The last 5 elements are the largest and in order
Bubble Sort: Pass 6 2 3 1 4 5 6 7 2 3 1 4 5 6 7 2 1 3 4 5 6 7 2 1 3 4 5 6 7 1 2 3 4 5 6 7 1 2 3 4 5 6 7 ^ ^
Bubble Sort: Pass 6 2 3 1 4 5 6 7 2 3 1 4 5 6 7 2 1 3 4 5 6 7 2 1 3 4 5 6 7 1 2 3 4 5 6 7 1 2 3 4 5 6 7 1 2 3 4 5 6 7 The last 6 elements are the largest and in order
Bubble Sort is Done 2 3 1 4 5 6 7 2 3 1 4 5 6 7 2 1 3 4 5 6 7 2 1 3 4 5 6 7 1 2 3 4 5 6 7 1 2 3 4 5 6 7 1 2 3 4 5 6 7 1 2 3 4 5 6 7 What is the complexity of this algorithm?
Bubble Sort def bubbleSort(alist): for passnum in range(len(alist)-1,0,-1): for i in range(passnum): if alist[i]>alist[i+1]: temp = alist[i] alist[i] = alist[i+1] alist[i+1] = temp
Selection Sort Selection sort, like Bubble sort, is based on a strategy that repeatedly executes the step: Move the largest of A[0..last] to A[last]
Selection Sort Selection Sort moves the largest entry to the end of A[0..last] by determining the position positionofMax of the largest entry, and then swapping A[positionOfMax] with A[last]: Selection Sort uses 2 variables: fillslot: keeps track of the portion A[0.. fillslot] that has already been examined: fillslot will range from 0 to last. positionOfMax: keeps track of the position of the largest entry in A[0.. fillslot]. When fillslot equals last, the variable positionOfMax will be the position of the largest entry in A[0..last].
Selection Sort def selectionSort(alist): for fillslot in range(len(alist)-1,0,-1): positionOfMax=0 for location in range(1,fillslot+1): if alist[location]>alist[positionOfMax]: positionOfMax = location temp = alist[fillslot] alist[fillslot] = alist[positionOfMax] alist[positionOfMax] = temp
Why is selection sort typically faster than bubble sort? Bubble sort has to do O(n2) comparisons and it may do up to O(n2) swaps. Selection sort has to do O(n2) comparisons but only O(n) swaps.
Insertion Sort
Insertion Sort Note that for any array A[0..N-1], the portion A[0..0] consisting of the single entry A[0] is already sorted. Insertion Sort works by extending the length of the sorted portion one step at a time: A[0] is sorted A[0..1] is sorted A[0..2] is sorted A[0..3] is sorted, and so on, until A[0..N-1] is sorted.
Insertion Sort The strategy for Insertion Sort: //A[0..0] is sorted for (index = 1; index <= N -1; index ++) { // A[0..index-1] is sorted insert A[index] at the right place in A[0..index] // Now A[0..index] is sorted } // Now A[0..N -1] is sorted, so entire array is sorted
Insertion Sort 3 4 5 1 2 5 2 4 6 1 3
Insertion Sort 3 4 5 1 2 5 4 6 1 3 2
Insertion Sort 3 4 5 1 2 5 4 6 1 3 2
Insertion Sort 3 4 5 1 2 2 5 4 6 1 3
Insertion Sort 3 4 5 1 2 2 5 6 1 3 4
Insertion Sort 3 4 5 1 2 2 5 6 1 3 4
Insertion Sort 3 4 5 1 2 2 4 5 6 1 3
Insertion Sort 3 4 5 1 2 2 4 5 1 3 6
Insertion Sort 3 4 5 1 2 2 4 5 6 1 3
Insertion Sort 3 4 5 1 2 2 4 5 6 3 1
Insertion Sort 3 4 5 1 2 2 4 5 6 3 1
Insertion Sort 3 4 5 1 2 2 4 5 6 3 1
Insertion Sort 3 4 5 1 2 2 4 5 6 3 1
Insertion Sort 3 4 5 1 2 2 4 5 6 3 1
Insertion Sort 3 4 5 1 2 1 2 4 5 6 3
Insertion Sort 3 4 5 1 2 1 2 4 5 6 3
Insertion Sort 3 4 5 1 2 1 2 4 5 6 3
Insertion Sort 3 4 5 1 2 1 2 4 5 6 3
Insertion Sort 3 4 5 1 2 1 2 4 5 6 3
Insertion Sort 3 4 5 1 2 1 2 3 4 5 6
Insertion Sort: def insertionSort(alist): for index in range(1,len(alist)): currentvalue = alist[index] position = index while position>0 and alist[position-1]>currentvalue: alist[position]=alist[position-1] position = position-1 alist[position]=currentvalue
Why Insertion Sort tends to be better than either Bubble sort or Selection Bubble sort MUST do O(n2) comparisons and may do O(n2) swaps. Selection sort MUST do O(n2) comparisons. Insertion sort MAY do O(n2) comparisons in the worst case.