1 / 34

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 (however, it isn’t a stable sort) Based off of a “heap”, which has several uses other than sorting The word “heap” doesn’t refer to memory management.

mervyn
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

  2. Heapsort • Like MERGESORT, it runs in O(nlgn) • Unlike MERGESORT, it sorts in place (however, it isn’t a stable sort) • Based off of a “heap”, which has several uses other than sorting • The word “heap” doesn’t refer to memory management

  3. The Heap • A binary heap is an essentially complete binary tree • All levels are full except possibly the last level • If the lowest level isn’t full, all of the missing leaves are on the right • A binary heap exhibits parental dominance; that is, each node is ≥ its two children • A binary heap may be implemented as an array

  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

  5. Simple Functions When using an array to implement a heap, these simple functions are helpful: PARENT(i) // Who is the parent of node i? { return(i/2) } LEFT(i) // Which node is the left child of i? { return(2i) } RIGHT(i) // Which node is the right child of i? { return(2i + 1) } HEAP-SIZE(A)// Current size of heap stored in the array A

  6. Properties • Max-heap property: • A[PARENT(i)]  A[i] • Min-heap property: • A[PARENT(i)]  A[i] • We define the height of a node to be the longest path from the node to a leaf. • The height of the entire tree is (lgn)

  7. HEAPIFY • This is the heart of the algorithm • Determine if an individual node is smaller than its children. If so, Parent swaps with largest child. • Calls itself recursively • Runs in O(lgn) or O(h)

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

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

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

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

  12. BUILD-HEAP • To convert an array (of a size equal to length[A]) into a heap, use HEAPIFYin a 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 heap! BUILD-HEAP (A) heap-size(A)← length[A] fori ← length[A]/2 downto 1 doHEAPIFY(A, i)

  13. Analysis of Building a Heap • Since each call to HEAPIFYcosts O(lgn) and there are n/2calls, this is a O(nlgn)algorithm. • It takes O(h) time to insert a new node.

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

  15. 16 14 10 8 7 9 3 2 4 1

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  34. Priority Queues • A priority queue is a heap that uses a key and a defined ‘<‘ operation.

More Related