1 / 96

Heap Sort

Heap Sort. Heapsort. Priority queues can be used to sort in O ( N log N ) time. First, build a binary heap of N elements in O ( N ) time perform N deleteMin operations each taking O (log N ) time, hence resulting in O ( N log N ). Problem : needs an extra array.

Download Presentation

Heap Sort

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Heap Sort

  2. Heapsort Priority queues can be used to sort in O(NlogN) time. First, build a binary heap of N elements in O(N) time perform NdeleteMin operations each taking O(logN) time, hence resulting in O(NlogN). Problem: needs an extra array. A clever solution: after each deleteMin heap size shrinks by 1, use this space. But the result will be a decreasing sorted order. That is, we may use a max heap by changing the heap-order property Izmir University of Economics

  3. Heapsort Example Max heap after buildHeap phase Max heap after first deleteMax phase Izmir University of Economics

  4. Analysis of Heapsort Izmir University of Economics worst case analysis 2N comparisons-buildHeap N-1 deleteMax operations. Theorem: Average # of comparisons to heapsort a random permutation of N distinct items is 2NlogN-O(NloglogN). Proof: on any input, cost sequence D: d1, d2,..., dN for any D, distinct deleteMax sequences total # of heaps with cost less than M is at most # of heaps with cost M < N(logN-loglogN-4) is at most (N/16)N. So the average # of comparisons is at least 2M.

  5. 7 2  9 4 2 4 7 9 7  2 2 7 9  4 4 9 7 7 2 2 9 9 4 4 Merge Sort

  6. Divide-and conquer is a general algorithm design paradigm: Divide: divide the input data S in two disjoint subsets S1and S2 Recur: solve the subproblems associated with S1and S2 Conquer: combine the solutions for S1and S2 into a solution for S The base case for the recursion are subproblems of size 0 or 1 Merge-sort is a sorting algorithm based on the divide-and-conquer paradigm Like heap-sort It uses a comparator It has O(n log n) running time Unlike heap-sort It does not use an auxiliary priority queue It accesses data in a sequential manner (suitable to sort data on a disk) Divide-and-Conquer

  7. Merge-sort on an input sequence S with n elements consists of three steps: Divide: partition S into two sequences S1and S2 of about n/2 elements each Recur: recursively sort S1and S2 Conquer: merge S1and S2 into a unique sorted sequence Merge-Sort AlgorithmmergeSort(S, C) Inputsequence S with n elements, comparator C Outputsequence S sorted according to C ifS.size() > 1 (S1, S2)partition(S, n/2) mergeSort(S1, C) mergeSort(S2, C) Smerge(S1, S2)

  8. Merging Two Sorted Sequences Algorithmmerge(A, B) Inputsequences A and B withn/2 elements each Outputsorted sequence of A  B S empty sequence whileA.isEmpty() B.isEmpty() ifA.first().element()<B.first().element() S.insertLast(A.remove(A.first())) else S.insertLast(B.remove(B.first())) whileA.isEmpty() S.insertLast(A.remove(A.first())) whileB.isEmpty() S.insertLast(B.remove(B.first())) return S • The conquer step of merge-sort consists of merging two sorted sequences A and B into a sorted sequence S containing the union of the elements of A and B • Merging two sorted sequences, each with n/2 elements and implemented by means of a doubly linked list, takes O(n) time

  9. Merge-Sort Tree • An execution of merge-sort is depicted by a binary tree • each node represents a recursive call of merge-sort and stores • unsorted sequence before the execution and its partition • sorted sequence at the end of the execution • the root is the initial call • the leaves are calls on subsequences of size 0 or 1 7 2  9 4 2 4 7 9 7  2 2 7 9  4 4 9 7 7 2 2 9 9 4 4

  10. 7 2 9 4  2 4 7 9 3 8 6 1  1 3 8 6 7 2  2 7 9 4  4 9 3 8  3 8 6 1  1 6 7  7 2  2 9  9 4  4 3  3 8  8 6  6 1  1 Execution Example • Partition 7 2 9 4  3 8 6 11 2 3 4 6 7 8 9

  11. 7 2  2 7 9 4  4 9 3 8  3 8 6 1  1 6 7  7 2  2 9  9 4  4 3  3 8  8 6  6 1  1 Execution Example (cont.) • Recursive call, partition 7 2 9 4  3 8 6 11 2 3 4 6 7 8 9 7 2  9 4 2 4 7 9 3 8 6 1  1 3 8 6

  12. 7  7 2  2 9  9 4  4 3  3 8  8 6  6 1  1 Execution Example (cont.) • Recursive call, partition 7 2 9 4  3 8 6 11 2 3 4 6 7 8 9 7 2  9 4 2 4 7 9 3 8 6 1  1 3 8 6 7  2 2 7 9 4  4 9 3 8  3 8 6 1  1 6

  13. 7  2 2 7 9 4  4 9 3 8  3 8 6 1  1 6 Execution Example (cont.) • Recursive call, base case 7 2 9 4  3 8 6 11 2 3 4 6 7 8 9 7 2  9 4 2 4 7 9 3 8 6 1  1 3 8 6 77 2  2 9  9 4  4 3  3 8  8 6  6 1  1

  14. Execution Example (cont.) • Recursive call, base case 7 2 9 4  3 8 6 11 2 3 4 6 7 8 9 7 2  9 4 2 4 7 9 3 8 6 1  1 3 8 6 7  2 2 7 9 4  4 9 3 8  3 8 6 1  1 6 77 22 9  9 4  4 3  3 8  8 6  6 1  1

  15. Execution Example (cont.) • Merge 7 2 9 4  3 8 6 11 2 3 4 6 7 8 9 7 2  9 4 2 4 7 9 3 8 6 1  1 3 8 6 7  22 7 9 4  4 9 3 8  3 8 6 1  1 6 77 22 9  9 4  4 3  3 8  8 6  6 1  1

  16. Execution Example (cont.) • Recursive call, …, base case, merge 7 2 9 4  3 8 6 11 2 3 4 6 7 8 9 7 2  9 4 2 4 7 9 3 8 6 1  1 3 8 6 7  22 7 9 4  4 9 3 8  3 8 6 1  1 6 77 22 9 9 4 4 3  3 8  8 6  6 1  1

  17. Execution Example (cont.) • Merge 7 2 9 4  3 8 6 11 2 3 4 6 7 8 9 7 2  9 42 4 7 9 3 8 6 1  1 3 8 6 7  22 7 9 4  4 9 3 8  3 8 6 1  1 6 77 22 9 9 4 4 3  3 8  8 6  6 1  1

  18. Execution Example (cont.) • Recursive call, …, merge, merge 7 2 9 4  3 8 6 11 2 3 4 6 7 8 9 7 2  9 42 4 7 9 3 8 6 1  1 3 6 8 7  22 7 9 4  4 9 3 8 3 8 6 1  1 6 77 22 9 9 4 4 33 88 66 11

  19. Execution Example (cont.) • Merge 7 2 9 4  3 8 6 11 2 3 4 6 7 8 9 7 2  9 42 4 7 9 3 8 6 1  1 3 6 8 7  22 7 9 4  4 9 3 8 3 8 6 1  1 6 77 22 9 9 4 4 33 88 66 11

  20. Mergesort: sort a collection of given integers in increasing order Example: { 4, 7, 2, 3, 0, 1, 9, 11, 5, 12, 13, 6, 15, 8, 10, 14 } sorts to { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 }

  21. Merge: given two lists of numbers in increasing order, produce a list of numbers in increasing order which contains all the numbers of the given lists.

  22. Merge: given two lists of numbers in increasing order, produce a list of numbers in increasing order which contains all the numbers of the given lists. 3, 5, 7, 11, 16, 24 1, 2, 6, 10, 12, 16, 32, 36

  23. Merge: given two lists of numbers in increasing order, produce a list of numbers in increasing order which contains all the numbers of the given lists. First half 3, 5, 7, 11, 16, 24 1, 2, 6, 10, 12, 16, 32, 36 Second half

  24. Merge: given two lists of numbers in increasing order, produce a list of numbers in increasing order which contains all the numbers of the given lists. 3, 5, 7, 11, 16, 24 1, 2, 6, 10, 12, 16, 32, 36 1,

  25. Merge: given two lists of numbers in increasing order, produce a list of numbers in increasing order which contains all the numbers of the given lists. 3, 5, 7, 11, 16, 24 1, 2, 6, 10, 12, 16, 32, 36 1, 2,

  26. Merge: given two lists of numbers in increasing order, produce a list of numbers in increasing order which contains all the numbers of the given lists. 3, 5, 7, 11, 16, 24 1, 2, 6, 10, 12, 16, 32, 36 1, 2, 3,

  27. Merge: given two lists of numbers in increasing order, produce a list of numbers in increasing order which contains all the numbers of the given lists. 3, 5, 7, 11, 16, 24 1, 2, 6, 10, 12, 16, 32, 36 1, 2, 3, 5,

  28. Merge: given two lists of numbers in increasing order, produce a list of numbers in increasing order which contains all the numbers of the given lists. 3, 5, 7, 11, 16, 24 1, 2, 6, 10, 12, 16, 32, 36 1, 2, 3, 5, 6,

  29. Merge: given two lists of numbers in increasing order, produce a list of numbers in increasing order which contains all the numbers of the given lists. 3, 5, 7, 11, 16, 24 1, 2, 6, 10, 12, 16, 32, 36 1, 2, 3, 5, 6, 7,

  30. Merge: given two lists of numbers in increasing order, produce a list of numbers in increasing order which contains all the numbers of the given lists. 3, 5, 7, 11, 16, 24 1, 2, 6, 10, 12, 16, 32, 36 1, 2, 3, 5, 6, 7, 10,

  31. Merge: given two lists of numbers in increasing order, produce a list of numbers in increasing order which contains all the numbers of the given lists. 3, 5, 7, 11, 16, 24 1, 2, 6, 10, 12, 16, 32, 36 1, 2, 3, 5, 6, 7, 10, 11,

  32. Merge: given two lists of numbers in increasing order, produce a list of numbers in increasing order which contains all the numbers of the given lists. 3, 5, 7, 11, 16, 24 1, 2, 6, 10, 12, 16, 32, 36 1, 2, 3, 5, 6, 7, 10, 11, 12,

  33. Merge: given two lists of numbers in increasing order, produce a list of numbers in increasing order which contains all the numbers of the given lists. 3, 5, 7, 11, 16, 24 1, 2, 6, 10, 12, 16, 32, 36 1, 2, 3, 5, 6, 7, 10, 11, 12, 16,

  34. Merge: given two lists of numbers in increasing order, produce a list of numbers in increasing order which contains all the numbers of the given lists. 3, 5, 7, 11, 16, 24 1, 2, 6, 10, 12, 16, 32, 36 1, 2, 3, 5, 6, 7, 10, 11, 12, 16, 16,

  35. Merge: given two lists of numbers in increasing order, produce a list of numbers in increasing order which contains all the numbers of the given lists. 3, 5, 7, 11, 16, 24 1, 2, 6, 10, 12, 16, 32, 36 1, 2, 3, 5, 6, 7, 10, 11, 12, 16, 16, 24,

  36. Merge: given two lists of numbers in increasing order, produce a list of numbers in increasing order which contains all the numbers of the given lists. 3, 5, 7, 11, 16, 24 1, 2, 6, 10, 12, 16, 32, 36 1, 2, 3, 5, 6, 7, 10, 11, 12, 16, 16, 24, 32, 36

  37. { 4, 7, 2, 3, 0, 1, 9, 11, 5, 12, 13, 6, 15, 8, 10, 14 }

  38. { 4, 7, 2, 3, 0, 1, 9, 11, 5, 12, 13, 6, 15, 8, 10, 14 } { 4, 7, 2, 3, 0, 1, 9, 11 } { 5, 12, 13, 6, 15, 8, 10, 14 } First half Second half

  39. { 4, 7, 2, 3, 0, 1, 9, 11, 5, 12, 13, 6, 15, 8, 10, 14 } { 4, 7, 2, 3, 0, 1, 9, 11 } { 5, 12, 13, 6, 15, 8, 10, 14 } { 4, 7, 2, 3 } { 0, 1, 9, 11 } { 5, 12, 13, 6 } { 15, 8, 10, 14 }

  40. { 4, 7, 2, 3, 0, 1, 9, 11, 5, 12, 13, 6, 15, 8, 10, 14 } { 4, 7, 2, 3, 0, 1, 9, 11 } { 5, 12, 13, 6, 15, 8, 10, 14 } { 4, 7, 2, 3 } { 0, 1, 9, 11 } { 5, 12, 13, 6 } { 15, 8, 10, 14 } { 5, 12 } { 13, 6 } { 15, 8 } { 10, 14 } { 4, 7 } { 2, 3 } { 0, 1 } { 9, 11 }

  41. { 4, 7, 2, 3, 0, 1, 9, 11, 5, 12, 13, 6, 15, 8, 10, 14 } { 4, 7, 2, 3, 0, 1, 9, 11 } { 5, 12, 13, 6, 15, 8, 10, 14 } { 4, 7, 2, 3 } { 0, 1, 9, 11 } { 5, 12, 13, 6 } { 15, 8, 10, 14 } { 5, 12 } { 13, 6 } { 15, 8 } { 10, 14 } { 4, 7 } { 2, 3 } { 0, 1 } { 9, 11 } { 4, 7 } { 2, 3 } { 0, 1 } { 9, 11 } { 5, 12 } { 6, 13 } { 8, 15 } { 10, 14 }

  42. { 4, 7, 2, 3, 0, 1, 9, 11, 5, 12, 13, 6, 15, 8, 10, 14 } { 4, 7, 2, 3, 0, 1, 9, 11 } { 5, 12, 13, 6, 15, 8, 10, 14 } { 4, 7, 2, 3 } { 0, 1, 9, 11 } { 5, 12, 13, 6 } { 15, 8, 10, 14 } { 5, 12 } { 13, 6 } { 15, 8 } { 10, 14 } { 4, 7 } { 2, 3 } { 0, 1 } { 9, 11 } { 4, 7 } { 2, 3 } { 0, 1 } { 9, 11 } { 5, 12 } { 6, 13 } { 8, 15 } { 10, 14 } { 2, 3, 4, 7 } { 0, 1, 9, 11 } { 5, 6, 12, 13 } { 8, 10, 14, 15 }

  43. { 4, 7, 2, 3, 0, 1, 9, 11, 5, 12, 13, 6, 15, 8, 10, 14 } { 4, 7, 2, 3, 0, 1, 9, 11 } { 5, 12, 13, 6, 15, 8, 10, 14 } { 4, 7, 2, 3 } { 0, 1, 9, 11 } { 5, 12, 13, 6 } { 15, 8, 10, 14 } { 5, 12 } { 13, 6 } { 15, 8 } { 10, 14 } { 4, 7 } { 2, 3 } { 0, 1 } { 9, 11 } { 4, 7 } { 2, 3 } { 0, 1 } { 9, 11 } { 5, 12 } { 6, 13 } { 8, 15 } { 10, 14 } { 2, 3, 4, 7 } { 0, 1, 9, 11 } { 5, 6, 12, 13 } { 8, 10, 14, 15 } { 5, 6, 8, 10, 12, 13, 14, 15 } { 0, 1, 2, 3, 4, 7, 9, 11 }

  44. { 4, 7, 2, 3, 0, 1, 9, 11, 5, 12, 13, 6, 15, 8, 10, 14 } { 4, 7, 2, 3, 0, 1, 9, 11 } { 5, 12, 13, 6, 15, 8, 10, 14 } { 4, 7, 2, 3 } { 0, 1, 9, 11 } { 5, 12, 13, 6 } { 15, 8, 10, 14 } { 5, 12 } { 13, 6 } { 15, 8 } { 10, 14 } { 4, 7 } { 2, 3 } { 0, 1 } { 9, 11 } { 4, 7 } { 2, 3 } { 0, 1 } { 9, 11 } { 5, 12 } { 6, 13 } { 8, 15 } { 10, 14 } { 2, 3, 4, 7 } { 0, 1, 9, 11 } { 5, 6, 12, 13 } { 8, 10, 14, 15 } { 5, 6, 8, 10, 12, 13, 14, 15 } { 0, 1, 2, 3, 4, 7, 9, 11 } { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 }

  45. Mergesort runs in O(N logN), # of comparisons is nearly optimal, fine example of a recursive algorithm. Fundamental operation is merging of 2 sorted lists. The basic merging algorithm takes 2 input arrays A and B, an output array C. 3 counters, Actr, Bctr, and Cctr are initially set to the beginning of their respective arrays. The smaller of A[Actr] and B[Bctr] is copied to C[Cctr ] and the appropriate counters are advanced. When either list is exhausted, the rest of the other list is copied to C. Izmir University of Economics 45

  46. Mergesort - merge Izmir University of Economics 46 The time to merge is linear, at most N-1 comparisons are required (since each comparison adds an element to C. It should also be noted that after N-1 elements are added to array C, the last element need not be compared but it simply gets copied). Mergesort algorithm is then easy to describe. If N=1 DONE else { mergesort(left half); mergesort(right half); merge(left half, right half); }

  47. Mergesort - Implementation Izmir University of Economics 47

  48. MSort - Implementation Izmir University of Economics

  49. Analysis of Mergesort Izmir University of Economics • Write down recurrence relations and solve them • T(1)=1 // For N=1 • T(N)=2T(N/2)+N // assumption is N=2k

  50. Homework Assignments 50 7.11, 7.12, 7.15, 7.17, 7.43 (7.38 in 3/e), 7.53 (7.48 in 3/e) You are requested to study and solve the exercises. Note that these are for you to practice only. You are not to deliver the results to me.

More Related