1 / 39

Heapsort

Heapsort. A minimalist's approach. Heapsort. Like M ERGE S ORT , it runs in O ( n lg n ) Unlike M ERGE S ORT , it sorts in place Based off of a “heap”, which has several uses The word “heap” doesn’t refer to memory management. The Heap. A binary heap is a nearly complete binary tree

mjake
Download Presentation

Heapsort

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. Heapsort A minimalist's approach Jeff Chastine

  2. Heapsort • Like MERGESORT, it runs in O(n lg n) • Unlike MERGESORT, it sorts in place • Based off of a “heap”, which has several uses • The word “heap” doesn’t refer to memory management Jeff Chastine

  3. The Heap • A binary heap is a nearly complete binary tree • Implemented as an array A • Two similar attributes: • length[A] is the size (number of slots) in A • heap-size[A] is the number of elements in A • Thus, heap-size[A]  length[A] • Also, no element past A[heap-size[A]] is an element Jeff Chastine

  4. The Heap • Can be a min-heap or a max-heap 87 51 67 25 41 55 43 87 21 17 33 35 87 51 67 25 41 55 43 21 17 33 35 Jeff Chastine

  5. Simple Functions PARENT(i) return (i/2) LEFT(i) return (2i) RIGHT(i) return (2i + 1) Jeff Chastine

  6. Properties • Max-heap property: • A[PARENT(i)]  A[i] • Min-heap property: • A[PARENT(i)]  A[i] • Max-heaps are used for sorting • Min-heaps are used for priority queues (later) • We define the height of a node to be the longest path from the node to a leaf. • The height of the tree is (lg n) Jeff Chastine

  7. MAX-HEAPIFY • This is the heart of the algorithm • Determines if an individual node is smaller than its children • Parent swaps with largest child if that child is larger • Calls itself recursively • Runs in O(lg n) or O(h) Jeff Chastine

  8. HEAPIFY MAX-HEAPIFY (A, i) l← LEFT (i) r ← RIGHT(i) ifl ≤ heap-size[A] and A[l] > A[i] thenlargest ← l elselargest ← i ifr ≤ heap-size[A] and A[r]>A[largest] thenlargest ← r iflargest ≠ i then exchange A[i] with A[largest] MAX-HEAPIFY (A, largest) Jeff Chastine

  9. 16 4 10 14 7 9 3 2 8 1 Jeff Chastine

  10. 16 14 10 4 7 9 3 2 8 1 Jeff Chastine

  11. 16 14 10 8 7 9 3 2 4 1 Jeff Chastine

  12. Of Note • The children’s subtrees each have size at most 2n/3 – when the last row is exactly ½ full • Therefore, the running time is: T (n) = T(2n/3) + (1) = O(lg n) Jeff Chastine

  13. BUILD-HEAP • Use MAX-HEAPIFY in bottom up manner • Why does the loop start at length[A]/2? • At the start of each loop, each node i is the root of a max-heap! BUILD-HEAP (A) heap-size[A] ← length[A] fori ← length[A]/2 downto 1 do MAX-HEAPIFY(A, i) Jeff Chastine

  14. Analysis of Building a Heap • Since each call to MAX-HEAPIFY costs O(lg n) and there are O(n) calls, this is O(n lg n)... • Can derive a tighter bound: do all nodes take log n time? • Has at most n/2h+1 nodes at any height (the more the height, the less nodes there are) • It takes O(h) time to insert a node of height h. Jeff Chastine

  15. Thus, the running time is 2n = O(n) Sum up the work at each level Height h islogarithmic The number of nodes at height h Multiplied by their height Jeff Chastine

  16. HEAPSORT HEAPSORT (A) BUILD-HEAP(A) fori← length[A] downto 2 do exchange A[1] withA[i] heap-size[A] ← heap-size[A] - 1 MAX-HEAPIFY(A, 1) Jeff Chastine

  17. 16 14 10 8 7 9 3 2 4 1 Jeff Chastine

  18. 1 14 10 8 7 9 3 2 4 16 Swap A[1] A[i] Jeff Chastine

  19. 14 8 10 4 7 9 3 2 1 16 heap-size  heap-size – 1 MAX-HEAPIFY(A, 1) Jeff Chastine

  20. 1 8 10 4 7 9 3 2 14 16 Swap A[1] A[i] Jeff Chastine

  21. 10 8 9 4 7 1 3 2 14 16 heap-size  heap-size – 1 MAX-HEAPIFY(A, 1) Jeff Chastine

  22. 2 8 9 4 7 1 3 10 14 16 Swap A[1] A[i] Jeff Chastine

  23. 9 8 3 4 7 1 2 10 14 16 heap-size  heap-size – 1 MAX-HEAPIFY(A, 1) Jeff Chastine

  24. 2 8 3 4 7 1 9 10 14 16 Swap A[1] A[i] Jeff Chastine

  25. 8 7 3 4 2 1 9 10 14 16 heap-size  heap-size – 1 MAX-HEAPIFY(A, 1) Jeff Chastine

  26. 1 7 3 4 2 8 9 10 14 16 Swap A[1] A[i] Jeff Chastine

  27. 7 4 3 1 2 8 9 10 14 16 heap-size  heap-size – 1 MAX-HEAPIFY(A, 1) Jeff Chastine

  28. 2 4 3 1 7 8 9 10 14 16 Swap A[1] A[i] Jeff Chastine

  29. 4 2 3 1 7 8 9 10 14 16 heap-size  heap-size – 1 MAX-HEAPIFY(A, 1) Jeff Chastine

  30. 1 2 3 4 7 8 9 10 14 16 Swap A[1] A[i] Jeff Chastine

  31. 3 2 1 4 7 8 9 10 14 16 heap-size  heap-size – 1 MAX-HEAPIFY(A, 1) Jeff Chastine

  32. 1 2 3 4 7 8 9 10 14 16 Swap A[1] A[i] Jeff Chastine

  33. 2 1 3 4 7 8 9 10 14 16 heap-size  heap-size – 1 MAX-HEAPIFY(A, 1) Jeff Chastine

  34. 1 2 3 4 7 8 9 10 14 16 Swap A[1] A[i] Jeff Chastine

  35. 1 2 3 4 7 8 9 10 14 16 heap-size  heap-size – 1 MAX-HEAPIFY(A, 1) Jeff Chastine

  36. Priority Queues • A priority queue is a heap that uses a key • Common in operating systems (processes) • Supports HEAP-MAXIMUM, EXTRACT-MAX, INCREASE-KEY, INSERT HEAP-MAXIMUM (A) 1 returnA[1] Jeff Chastine

  37. HEAP-EXTRACT-MAX (A) 1 ifheap-size[A] < 1 2 then error “heap underflow” 3 max A[1] 4 A[1]  A[heap-size[A]] 5 heap-size[A]  heap-size[A] – 1 6 MAX-HEAPIFY (A, 1) 7 returnmax Jeff Chastine

  38. HEAP-INCREASE-KEY(A, i, key) 1 ifkey < A[i] 2 then error “new key smaller than current” 3 A[i]  key 4 whilei > 1 and A[PARENT(i)] < A[i] 5 do exchange A[i]  A[PARENT(i)] 6 i  PARENT(i) Note: runs in O(lg n) Jeff Chastine

  39. MAX-HEAP-INSERT (A, key) 1 heap-size[A]  heap-size[A] + 1 2 A[heap-size[A]]  - 3 HEAP-INCREASE-KEY(A, heap-size[A], key) Jeff Chastine

More Related