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)• Worst Case run time: O(n log n)
• Compare the Big O run time with Quicksort
Q: How does Heapsort compare?
Source: http://www.azillionmonkeys.com/qed/sort.html
Athlon XP 1.620Ghz Power4 1Ghz
Intel C/C++ WATCOM C/C++ GCC MSVC CC
Heapsort 2.09 4.06 4.16 4.12 16.91
Quicksort 2.58 3.24 3.42 2.80 14.99
Mergesort 3.51 4.28 4.83 4.01 16.90
Part 1: Average Run time test results, by compiler and chipset
Q: How does Heapsort compare?
Source: http://www.azillionmonkeys.com/qed/sort.html
Part 2: Comparisons and Read/Writes
Algorithm Comparisons Reads/Writes
Heapsort 61045.4 40878.2 Quicksort 22037.8 16322.5 Mergesort 31755.0 31225.0
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.
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
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
Almost Complete Binary Tree
V should be pushed to the left
Level 3 should be completely filled
This is a Heap.
Heap as an Array
Heap
Array
Lets see some L33t codeParent(CI) = (CI - 1) / 2;
RightChild(CI) = 2 * (CI + 1);
LeftChild(CI) = 2 * CI + 1;
Inserting into a Heap
• Insert “E”
Inserting into a HeapStart by filling “E” into the next available node
This insertion may cause our tree to not be a heap anymore!!!
Move “E” up the tree until it’s parent is smaller
Inserting “D” into the heapStart by filling “D” into the next available node
This insertion may cause our tree to not be a heap anymore!!!
“D” is smaller then “L”. So, Swap positions
“D” is smaller then “H”. So, Swap positions
“D” is not smaller then “C” so the heap is complete
Removing from the Heap
Always take the root node.
Move the last node “L” into “C’s” old place
Since the smaller Child of “L” is “D” -> Swap “D” and “L”
Since the smaller Child of “L” is “H” -> Swap “L” and “H”
The heap is complete again!
Final Conclusions
1. Heapsort doesn’t require much memory space!
2. Heapsort is very fast!3. Quicksort > Heapsort4. Mergesort > Heapsort5. Heapsort + Quicksort = Introsort6. Introsort > (Quicksort || Heapsort)
Check out these cool links
• http://tide4javascript.com/?s=Heapsort• http://cis.stvincent.edu/html/tutorials/swd/h
eaps/heaps.html• http://en.wikipedia.org/wiki/Heapsort• SWEET animation:
http://www2.hawaii.edu/~copley/665/HSApplet.html