Department of Computer Science COM S 228 Storing Algorithms Instructor: Ying Cai Department of Computer Science Iowa State University yingcai@iastate.edu Office: Atanasoff 201
Selection Sort 7 8 2 10 5 21 Get a list of unsorted numbers Set a marker for the unsorted section at the front of the list Repeat the following steps until one number remains in the unsorted section Compare all unsorted numbers in order to select the smallest one Swap this number with the first number in the unsorted section Advance the marker to the right one position 2 8 7 10 5 21 2 5 7 10 8 21 2 5 7 10 8 21 2 5 7 8 10 21
The total number of steps: (n-1) + (n-2) + ... + 2 + 1 = n (n-1) / 2
Insertion Sort 7 8 2 10 5 21 Get a list of unsorted numbers Set a marker for the sorted section after the first number in the list Repeat these steps until the unsorted section is empty Select the first unsorted number Swap this number to the left until it arrives at the correct sorted position Advance the marker to the right one position 7 8 2 10 5 21 2 7 8 10 5 21 2 7 8 10 5 21 2 5 7 8 10 21
Which one is better? 7 8 2 10 5 21 7 8 2 10 5 21 7 8 2 10 5 21 2 8 7 10 5 21 2 7 8 10 5 21 2 5 7 10 8 21 2 7 8 10 5 21 2 5 7 10 8 21 2 5 7 8 10 21 2 5 7 8 10 21 Selection Sort Insertion Sort
Merge Sort Key observation: we can merge two sorted arrays into one sorted array in linear time, O(n) i j 7 13 17 20 4 5 8 18 4 5 7 8 13 17 18 20
Merge(i, j) /* merge A[i..j-1] with A[j..l], where l is at most j+j-1-i. A[i..j-1] and A[j..l] are sorted */ :: 7 13 17 20 4 5 8 18 :: i j
Merge Sort 7 13 17 20 4 5 8 18 for (int i=0; i<n-1; i=i+2) { Merge(i, i+1); // merge A[0:0] with A[1:1]; } // merge A[2:2] with A[3:3]; ... for (int i=0; i<n-1; i=i+4) Merge(i, i+3); // merge A[0:1] with A[2:3]; } // merge A[4:5] with A[6:7]; ... for (int i=0; i<n-1; i=i+8) Merge(i, i+7); // merge A[0:3] with A[4:7] } // merge A[8:11] with A[12:15] ::::: 7 13 17 20 4 5 8 18
Merge Sort for (int r=1; r<n; r=r*2) { for (int i=0; i<n-1; i=i+r*2) Merge(i, i+r*2-1); }
Recursion Implementation of Merge Sort
Quick Sort Divide and conquer: partition and sort Pick an element, called a pivot, from the list. Place the pivot on the position that Every elements on its left is less than the pivot Every elements on its right is greater than the pivot Recursively apply the above steps to the sub-list of elements with smaller values and separately the sub-list of elements with greater values. 7 13 17 20 4 5 8 18
Complexity Analysis i is incremented O(n) times j is decremented O(n) times There are O(n) swaps
Recursive Implementation
Complexity Analysis
Complexity Analysis
A non-recursive implementation
C. A. R. Hoare: Inventor of Quick Sort
Project 2: Rankings Comparison
Spearman Footrule Distance
Kemeny Distance
Concept of Inversion
Counting Inversions
Counting Inversions
Counting Inversions
Counting Inversions
Counting Inversions