1 / 27

Heapsort Algorithm

Heapsort Algorithm. Eric & Bruce. Q: What is “Heapsort”?. It is a sorting algorithm which takes an array of values and sorts them in either descending order or ascending order. Q: How Fast is heapsort?. Heapsort is guaranteed to run in O(n log n) time. Avg Run time: O(n log n)

braden
Download Presentation

Heapsort Algorithm

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 Algorithm Eric & Bruce

  2. Q: What is “Heapsort”? • It is a sorting algorithm which takes an array of values and sorts them in either descending order or ascending order.

  3. Q: How Fast is heapsort? • Heapsort is guaranteed to run in O(n log n) time. • Avg Run time: O(n log n) • Worst Case run time: O(n log n) • Compare the Big O run time with Quicksort

  4. Q: How does Heapsort compare? Part 1: Average Run time test results, by compiler and chipset Source: http://www.azillionmonkeys.com/qed/sort.html

  5. Q: How does Heapsort compare? Part 2: Comparisons and Read/Writes Source: http://www.azillionmonkeys.com/qed/sort.html

  6. Runtime Conclusions: • Quicksort is faster then Heapsort in the Average case. • Heapsort is faster then Quicksort when Quicksort hits its worst case scenario [O(n^2)] • Best Algorithm: Hybrid of Quicksort and Heapsort • Final Note: Use quicksort. The performance is slightly better then Heapsort.

  7. Q: How does Heapsort work? • General Outline:Step 1: Get an unsorted arrayStep 2: “Heapify” the arrayStep 3: Pop off the root node of the heapStep 4: Adjust Heap to maintain its heapish propertiesStep 5: Pop off roots until none exist

  8. Properties of a Heap • Each node has a left and right child • All the leaves are at the bottom level or the bottom 2 levels • Nodes are filled from left to right • All levels are completely filled with nodes (exception: bottom level) • Min-Heap: Each child node is greater then the parent node • Max-Heap: Each child node is less then the parent node

  9. Almost Complete Binary Tree

  10. V should be pushed to the left

  11. Level 3 should be completely filled

  12. This is a Heap.

  13. Heap as an Array Heap Array

  14. Lets see some L33t code Parent(CI) = (CI - 1) / 2; RightChild(CI) = 2 * (CI + 1); LeftChild(CI) = 2 * CI + 1;

  15. Inserting into a Heap • Insert “E”

  16. Inserting into a Heap Start by filling “E” into the next available node This insertion may cause our tree to not be a heap anymore!!!

  17. Move “E” up the tree until it’s parent is smaller

  18. Inserting “D” into the heap Start by filling “D” into the next available node This insertion may cause our tree to not be a heap anymore!!!

  19. “D” is smaller then “L”. So, Swap positions

  20. “D” is smaller then “H”. So, Swap positions “D” is not smaller then “C” so the heap is complete

  21. Removing from the Heap Always take the root node.

  22. Move the last node “L” into “C’s” old place

  23. Since the smaller Child of “L” is “D” -> Swap “D” and “L”

  24. Since the smaller Child of “L” is “H” -> Swap “L” and “H”

  25. The heap is complete again!

  26. Final Conclusions • Heapsort doesn’t require much memory space! • Heapsort is very fast! • Quicksort > Heapsort • Mergesort > Heapsort • Heapsort + Quicksort = Introsort • Introsort > (Quicksort || Heapsort)

  27. Check out these cool links • http://tide4javascript.com/?s=Heapsort • http://cis.stvincent.edu/html/tutorials/swd/heaps/heaps.html • http://en.wikipedia.org/wiki/Heapsort • SWEET animation: http://www2.hawaii.edu/~copley/665/HSApplet.html

More Related