Presentation is loading. Please wait.

Presentation is loading. Please wait.

Department of Computer Science

Similar presentations


Presentation on theme: "Department of Computer Science"— Presentation transcript:

1 Department of Computer Science
COM S 228 Storing Algorithms Instructor: Ying Cai Department of Computer Science Iowa State University Office: Atanasoff 201

2 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

3 The total number of steps:
(n-1) + (n-2) = n (n-1) / 2

4 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

5 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

6 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

7 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

8 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

9 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); }

10 Recursion Implementation of Merge Sort

11

12 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

13 Complexity Analysis i is incremented O(n) times j is decremented O(n) times There are O(n) swaps

14 Recursive Implementation

15 Complexity Analysis

16 Complexity Analysis

17 A non-recursive implementation

18

19

20 C. A. R. Hoare: Inventor of Quick Sort

21 Project 2: Rankings Comparison

22 Spearman Footrule Distance

23 Kemeny Distance

24 Concept of Inversion

25 Counting Inversions

26 Counting Inversions

27 Counting Inversions

28 Counting Inversions

29 Counting Inversions


Download ppt "Department of Computer Science"

Similar presentations


Ads by Google