Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 CSC212 Data Structure - Section AB Lecture 22 Recursive Sorting, Heapsort & STL Quicksort Instructor: Edgardo Molina Department of Computer Science City.

Similar presentations


Presentation on theme: "1 CSC212 Data Structure - Section AB Lecture 22 Recursive Sorting, Heapsort & STL Quicksort Instructor: Edgardo Molina Department of Computer Science City."— Presentation transcript:

1 1 CSC212 Data Structure - Section AB Lecture 22 Recursive Sorting, Heapsort & STL Quicksort Instructor: Edgardo Molina Department of Computer Science City College of New York

2 2 Topics p Recursive Sorting Algorithms p Divide and Conquer technique p An O(NlogN) Sorting Alg. using a Heap p making use of the heap properties p STL Sorting Functions p C++ sort function p Original C version of qsort

3 3 The Divide-and-Conquer Technique p Basic Idea: p If the problem is small, simply solve it. p Otherwise, p divide the problem into two smaller sub-problems, each of which is about half of the original problem p Solve each sub-problem, and then p Combine the solutions of the sub-problems

4 4 The Divide-and-Conquer Sorting Paradigm 1. Divide the elements to be sorted into two groups of (almost) equal size 2. Sort each of these smaller groups of elements (by recursive calls) 3. Combine the two sorted groups into one large sorted list

5 5 Mergesort p Divide the array in the middle p Sort the two half-arrays by recursion p Merge the two halves void mergesort(int data[ ], size_t n) { size_t n1; // Size of the first subarray size_t n1; // Size of the first subarray size_t n2; // Size of the second subarray size_t n2; // Size of the second subarray if (n > 1) if (n > 1) { // Compute sizes of the subarrays. // Compute sizes of the subarrays. n1 = n / 2; n1 = n / 2; n2 = n - n1; n2 = n - n1; // Sort from data[0] through data[n1-1] // Sort from data[0] through data[n1-1] mergesort(data, n1); mergesort(data, n1); // Sort from data[n1] to the end // Sort from data[n1] to the end mergesort((data + n1), n2); mergesort((data + n1), n2); // Merge the two sorted halves. // Merge the two sorted halves. merge(data, n1, n2); merge(data, n1, n2); }}

6 6 Mergesort – an Example 161276321810 [0] [1] [2] [3] [4] [5] [6] [7]

7 7 Mergesort – an Example 236710121618 161276321810?

8 8 161276321810 161276321810divide

9 9 161276321810 161276321810 161276321810divide divide

10 10 161276321810 161276321810 161276321810161276321810 divide divide divide

11 11 161276321810 161276321810 161276321810 121667231018161276321810 divide divide divide merge

12 12 161276321810 161276321810 161276321810 121667231018161276321810671216231018divide divide divide merge merge

13 13 161276321810 161276321810 161276321810 121667231018161276321810671216231018 236710121618divide divide divide merge merge merge

14 14 Mergesort – two issues p Specifying a subarray with pointer arithmetic p int data[10]; p (data+i)[0] is the same of data[i] p (data+i][1] is the same as data[i+1] p Merging two sorted subarrays into a sorted list p need a temporary array (by new and then delete) p step through the two sub-arrays with two cursors, and copy the elements in the right order

15 15 Mergesort - merge 671216231018 ????????data temp [0] [1] [2] [3] [4] [5] [6] [7] c1c2 d

16 16 Mergesort - merge 671216231018 2???????data temp [0] [1] [2] [3] [4] [5] [6] [7] c1c2 d

17 17 Mergesort - merge 671216231018 23??????data temp [0] [1] [2] [3] [4] [5] [6] [7] c1c2 d

18 18 Mergesort - merge 671216231018 236?????data temp [0] [1] [2] [3] [4] [5] [6] [7] c1c2 d

19 19 Mergesort - merge 671216231018 2367????data temp [0] [1] [2] [3] [4] [5] [6] [7] c1 c2 d

20 20 Mergesort - merge 671216231018 236710???data temp [0] [1] [2] [3] [4] [5] [6] [7] c1 c2 d

21 21 Mergesort - merge 671216231018 23671012??data temp [0] [1] [2] [3] [4] [5] [6] [7] c1 c2 d

22 22 Mergesort - merge 671216231018 2367101216?data temp [0] [1] [2] [3] [4] [5] [6] [7] c1c2 d

23 23 Mergesort - merge 671216231018 236710121618data temp [0] [1] [2] [3] [4] [5] [6] [7] c1c2 d

24 24 Mergesort - merge 236710121618 data temp [0] [1] [2] [3] [4] [5] [6] [7] 236710121618

25 25 Mergesort - merge data [0] [1] [2] [3] [4] [5] [6] [7] 236710121618

26 26 Mergesort – Time Analysis p The worst-case running time, the average- case running time and the best-case running time for mergesort are all O(n log n)

27 27 Mergesort – an Example 161276321810 161276321810 161276321810 121667231018161276321810671216231018 236710121618divide divide divide merge merge merge

28 28 Mergesort – Time Analysis p At the top (0) level, 1 call to merge creates an array with n elements p At the 1 st level, 2 calls to merge creates 2 arrays, each with n/2 elements p At the 2 nd level, 4 calls to merge creates 4 arrays, each with n/4 elements p At the 3 rd level, 8 calls to merge creates 8 arrays, each with n/8 elements p At the dth level, 2 d calls to merge creates 2 d arrays, each with n/2 d elements p Each level does total work proportional to n => c n, where c is a constant p Assume at the dth level, the size of the subarrays is n/2 d =1, which means all the work is done at this level, therefore p the number of levels d = log 2 n p The total cost of the mergesort is c nd = c n log 2 n p therefore the Big-O is O(n log 2 n)

29 29 Heapsort p Heapsort – Why a Heap? (two properties) p Heapsort – How to? (two steps) p Heapsort – How good? (time analysis)

30 30 Heap Definition p A heap is a binary tree where the entries of the nodes can be compared with the less than operator of a strict weak ordering. p In addition, two rules are followed: p The entry contained by the node is NEVER less than the entries of the node’s children p The tree is a COMPLETE tree.

31 31 Why a Heap for Sorting? p Two properties p The largest element is always at the root p Adding and removing an entry from a heap is O(log n)

32 32 Heapsort – Basic Idea p Step 1. Make a heap from elements p add an entry to the heap one at a time p reheapification upward n times – O(n log n) p Step 2. Make a sorted list from the heap p Remove the root of the heap to a sorted list and p Reheapification downward to re-organize into a updated heap p n times – O(n log n)

33 33 Heapsort – Step 1: Make a Heap 161276321810 [0] [1] [2] [3] [4] [5] [6] [7] add an entry to the heap one at a time

34 34 Heapsort – Step 1: Make a Heap 161276321810 [0] [1] [2] [3] [4] [5] [6] [7] add an entry to the heap one at a time 16

35 35 Heapsort – Step 1: Make a Heap 161276321810 [0] [1] [2] [3] [4] [5] [6] [7] add an entry to the heap one at a time 16 12

36 36 Heapsort – Step 1: Make a Heap 161276321810 [0] [1] [2] [3] [4] [5] [6] [7] add an entry to the heap one at a time 16 12 7

37 37 Heapsort – Step 1: Make a Heap 161276321810 [0] [1] [2] [3] [4] [5] [6] [7] add an entry to the heap one at a time 16 12 7 6

38 38 Heapsort – Step 1: Make a Heap 161276321810 [0] [1] [2] [3] [4] [5] [6] [7] add an entry to the heap one at a time 16 12 7 63

39 39 Heapsort – Step 1: Make a Heap 161276321810 [0] [1] [2] [3] [4] [5] [6] [7] add an entry to the heap one at a time 16 12 7 632

40 40 Heapsort – Step 1: Make a Heap 161276321810 [0] [1] [2] [3] [4] [5] [6] [7] add an entry to the heap one at a time 16 12 7 632 18 reheapification upward: push the out-of-place node upward

41 41 Heapsort – Step 1: Make a Heap 161218632710 [0] [1] [2] [3] [4] [5] [6] [7] add an entry to the heap one at a time 16 12 18 632 7 reheapification upward: push the out-of-place node upward

42 42 Heapsort – Step 1: Make a Heap 181216632710 [0] [1] [2] [3] [4] [5] [6] [7] add an entry to the heap one at a time 18 12 16 632 7 reheapification upward: push the out-of-place node upward until it is in the right place

43 43 Heapsort – Step 1: Make a Heap 181216632710 [0] [1] [2] [3] [4] [5] [6] [7] add an entry to the heap one at a time 18 12 16 632 7 10 reheapification upward: push the out-of-place node upward until it is in the right place

44 44 Heapsort – Step 1: Make a Heap add an entry to the heap one at a time 18 12 16 1032 7 6 reheapification upward: push the out-of-place node upward until it is in the right place 181216103276 [0] [1] [2] [3] [4] [5] [6] [7]

45 45 Heapsort – Step 1: Make a Heap A heap is created: it is saved in the original array- the tree on the right is only for illustration! 18 12 16 1032 7 6 181216103276 [0] [1] [2] [3] [4] [5] [6] [7] Sorted???

46 46 Heapsort – Step 2: Sorting from Heap 18 12 16 1032 7 6 181216103276 [0] [1] [2] [3] [4] [5] [6] [7] heap -> sorted list from smallest to largest Q: where is the largest entry?

47 47 Heapsort – Step 2: Sorting from Heap 18 12 16 1032 7 6 181216103276 [0] [1] [2] [3] [4] [5] [6] [7] Idea: remove the root of the heap and place it in the sorted list => recall: how to remove the root?

48 48 Heapsort – Step 2: Sorting from Heap 6 12 16 1032 7 612161032718 [0] [1] [2] [3] [4] [5] [6] [7] How to remove the root? move the last entry in the root... and for the sake of sorting, put the root entry in the “sorted side” almost a heap... sorted side

49 49 Heapsort – Step 2: Sorting from Heap 6 12 16 1032 7 612161032718 [0] [1] [2] [3] [4] [5] [6] [7] How to remove the root? move the last entry in the root... then reposition the out-of place node to update the heap sorted side almost a heap...

50 50 Heapsort – Step 2: Sorting from Heap 16 12 6 1032 7 161261032718 [0] [1] [2] [3] [4] [5] [6] [7] How to remove the root? move the last entry in the root... then reposition the out-of place node to update the heap sorted side reheapification downward almost a heap...

51 51 Heapsort – Step 2: Sorting from Heap 16 12 7 1032 6 161271032618 [0] [1] [2] [3] [4] [5] [6] [7] How to remove the root? move the last entry in the root... then reposition the out-of place node to update the heap sorted side reheapification downward a heap in the unsorted side

52 52 Heapsort – Step 2: Sorting from Heap 16 12 7 1032 6 161271032618 [0] [1] [2] [3] [4] [5] [6] [7] do the same thing again for the heap in the unsorted side until all the entries have been moved to the sorted side sorted side a heap in the unsorted side

53 53 Heapsort – Step 2: Sorting from Heap 6 12 7 1032 612710321618 [0] [1] [2] [3] [4] [5] [6] [7] do the same thing again for the heap in the unsorted side until all the entries have been moved to the sorted side sorted side almost a heap...

54 54 Heapsort – Step 2: Sorting from Heap 12 6 7 1032 12 6710321618 [0] [1] [2] [3] [4] [5] [6] [7] do the same thing again for the heap in the unsorted side until all the entries have been moved to the sorted side sorted side almost a heap...

55 55 Heapsort – Step 2: Sorting from Heap 12 10 7 632 12 10 1076321618 [0] [1] [2] [3] [4] [5] [6] [7] do the same thing again for the heap in the unsorted side until all the entries have been moved to the sorted side sorted side almost a heap...

56 56 Heapsort – Step 2: Sorting from Heap 12 10 7 632 12 10 1076321618 [0] [1] [2] [3] [4] [5] [6] [7] do the same thing again for the heap in the unsorted side until all the entries have been moved to the sorted side sorted side a heap again!

57 57 Heapsort – Step 2: Sorting from Heap 2 10 7 63 2 10 10763121618 [0] [1] [2] [3] [4] [5] [6] [7] do the same thing again for the heap in the unsorted side until all the entries have been moved to the sorted side sorted side almost a heap...

58 58 Heapsort – Step 2: Sorting from Heap 10 2 7 63 10 10 2763121618 [0] [1] [2] [3] [4] [5] [6] [7] do the same thing again for the heap in the unsorted side until all the entries have been moved to the sorted side sorted side almost a heap...

59 59 Heapsort – Step 2: Sorting from Heap 10 6 7 23 10 10 6723121618 [0] [1] [2] [3] [4] [5] [6] [7] do the same thing again for the heap in the unsorted side until all the entries have been moved to the sorted side sorted side a heap again!

60 60 Heapsort – Step 2: Sorting from Heap 3 6 7 2 3 67210121618 [0] [1] [2] [3] [4] [5] [6] [7] do the same thing again for the heap in the unsorted side until all the entries have been moved to the sorted side sorted side almost a heap...

61 61 Heapsort – Step 2: Sorting from Heap 7 6 3 2 7 63210121618 [0] [1] [2] [3] [4] [5] [6] [7] do the same thing again for the heap in the unsorted side until all the entries have been moved to the sorted side sorted side a heap again !

62 62 Heapsort – Step 2: Sorting from Heap 2 6 3 2 63710121618 [0] [1] [2] [3] [4] [5] [6] [7] do the same thing again for the heap in the unsorted side until all the entries have been moved to the sorted side a heap ?? sorted side

63 63 Heapsort – Step 2: Sorting from Heap 6 2 3 6 23710121618 [0] [1] [2] [3] [4] [5] [6] [7] do the same thing again for the heap in the unsorted side until all the entries have been moved to the sorted side sorted side a heap !!

64 64 Heapsort – Step 2: Sorting from Heap 3 2 3 26710121618 [0] [1] [2] [3] [4] [5] [6] [7] do the same thing again for the heap in the unsorted side until all the entries have been moved to the sorted side sorted side heap !

65 65 Heapsort – Step 2: Sorting from Heap 2 2 36710121618 [0] [1] [2] [3] [4] [5] [6] [7] do the same thing again for the heap in the unsorted side until all the entries have been moved to the sorted side sorted side heap !

66 66 Heapsort – Step 2: Sorting from Heap 2 36710121618 [0] [1] [2] [3] [4] [5] [6] [7] do the same thing again for the heap in the unsorted side until all the entries have been moved to the sorted side sorted side DONE!

67 67 Heapsort – Time Analysis p Step 1. Make a heap from elements p add an entry to the heap one at a time p reheapification upward n times – O(n log n) p Step 2. Make a sorted list from the heap p Remove the root of the heap to a sorted list and p Reheapification downward to re-organize the unsorted side into a updated heap p do this n times – O(n log n) p The running time is O(n log n)

68 68 C++ STL Sorting Functions p The C++ sort function p void sort(Iterator begin, Iterator end); p The original C version of qsort void qsort( void *base, size_t number_of_elements, size_t element_size, int compare(const void*, const void*) );

69 69 Summary & Homework p Recursive Sorting Algorithms p Divide and Conquer technique p An O(NlogN) Sorting Alg. using a Heap p making use of the heap properties p STL Sorting Functions p C++ sort function p Original C version of qsort


Download ppt "1 CSC212 Data Structure - Section AB Lecture 22 Recursive Sorting, Heapsort & STL Quicksort Instructor: Edgardo Molina Department of Computer Science City."

Similar presentations


Ads by Google