heapsort algorithm
DESCRIPTION
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) - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: Heapsort Algorithm](https://reader033.vdocuments.net/reader033/viewer/2022051317/568149f3550346895db724b2/html5/thumbnails/1.jpg)
Heapsort Algorithm
Eric & Bruce
![Page 2: Heapsort Algorithm](https://reader033.vdocuments.net/reader033/viewer/2022051317/568149f3550346895db724b2/html5/thumbnails/2.jpg)
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.
![Page 3: Heapsort Algorithm](https://reader033.vdocuments.net/reader033/viewer/2022051317/568149f3550346895db724b2/html5/thumbnails/3.jpg)
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
![Page 4: Heapsort Algorithm](https://reader033.vdocuments.net/reader033/viewer/2022051317/568149f3550346895db724b2/html5/thumbnails/4.jpg)
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
![Page 5: Heapsort Algorithm](https://reader033.vdocuments.net/reader033/viewer/2022051317/568149f3550346895db724b2/html5/thumbnails/5.jpg)
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
![Page 6: Heapsort Algorithm](https://reader033.vdocuments.net/reader033/viewer/2022051317/568149f3550346895db724b2/html5/thumbnails/6.jpg)
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.
![Page 7: Heapsort Algorithm](https://reader033.vdocuments.net/reader033/viewer/2022051317/568149f3550346895db724b2/html5/thumbnails/7.jpg)
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
![Page 8: Heapsort Algorithm](https://reader033.vdocuments.net/reader033/viewer/2022051317/568149f3550346895db724b2/html5/thumbnails/8.jpg)
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
![Page 9: Heapsort Algorithm](https://reader033.vdocuments.net/reader033/viewer/2022051317/568149f3550346895db724b2/html5/thumbnails/9.jpg)
Almost Complete Binary Tree
![Page 10: Heapsort Algorithm](https://reader033.vdocuments.net/reader033/viewer/2022051317/568149f3550346895db724b2/html5/thumbnails/10.jpg)
V should be pushed to the left
![Page 11: Heapsort Algorithm](https://reader033.vdocuments.net/reader033/viewer/2022051317/568149f3550346895db724b2/html5/thumbnails/11.jpg)
Level 3 should be completely filled
![Page 12: Heapsort Algorithm](https://reader033.vdocuments.net/reader033/viewer/2022051317/568149f3550346895db724b2/html5/thumbnails/12.jpg)
This is a Heap.
![Page 13: Heapsort Algorithm](https://reader033.vdocuments.net/reader033/viewer/2022051317/568149f3550346895db724b2/html5/thumbnails/13.jpg)
Heap as an Array
Heap
Array
![Page 14: Heapsort Algorithm](https://reader033.vdocuments.net/reader033/viewer/2022051317/568149f3550346895db724b2/html5/thumbnails/14.jpg)
Lets see some L33t codeParent(CI) = (CI - 1) / 2;
RightChild(CI) = 2 * (CI + 1);
LeftChild(CI) = 2 * CI + 1;
![Page 15: Heapsort Algorithm](https://reader033.vdocuments.net/reader033/viewer/2022051317/568149f3550346895db724b2/html5/thumbnails/15.jpg)
Inserting into a Heap
• Insert “E”
![Page 16: Heapsort Algorithm](https://reader033.vdocuments.net/reader033/viewer/2022051317/568149f3550346895db724b2/html5/thumbnails/16.jpg)
Inserting into a HeapStart by filling “E” into the next available node
This insertion may cause our tree to not be a heap anymore!!!
![Page 17: Heapsort Algorithm](https://reader033.vdocuments.net/reader033/viewer/2022051317/568149f3550346895db724b2/html5/thumbnails/17.jpg)
Move “E” up the tree until it’s parent is smaller
![Page 18: Heapsort Algorithm](https://reader033.vdocuments.net/reader033/viewer/2022051317/568149f3550346895db724b2/html5/thumbnails/18.jpg)
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!!!
![Page 19: Heapsort Algorithm](https://reader033.vdocuments.net/reader033/viewer/2022051317/568149f3550346895db724b2/html5/thumbnails/19.jpg)
“D” is smaller then “L”. So, Swap positions
![Page 20: Heapsort Algorithm](https://reader033.vdocuments.net/reader033/viewer/2022051317/568149f3550346895db724b2/html5/thumbnails/20.jpg)
“D” is smaller then “H”. So, Swap positions
“D” is not smaller then “C” so the heap is complete
![Page 21: Heapsort Algorithm](https://reader033.vdocuments.net/reader033/viewer/2022051317/568149f3550346895db724b2/html5/thumbnails/21.jpg)
Removing from the Heap
Always take the root node.
![Page 22: Heapsort Algorithm](https://reader033.vdocuments.net/reader033/viewer/2022051317/568149f3550346895db724b2/html5/thumbnails/22.jpg)
Move the last node “L” into “C’s” old place
![Page 23: Heapsort Algorithm](https://reader033.vdocuments.net/reader033/viewer/2022051317/568149f3550346895db724b2/html5/thumbnails/23.jpg)
Since the smaller Child of “L” is “D” -> Swap “D” and “L”
![Page 24: Heapsort Algorithm](https://reader033.vdocuments.net/reader033/viewer/2022051317/568149f3550346895db724b2/html5/thumbnails/24.jpg)
Since the smaller Child of “L” is “H” -> Swap “L” and “H”
![Page 25: Heapsort Algorithm](https://reader033.vdocuments.net/reader033/viewer/2022051317/568149f3550346895db724b2/html5/thumbnails/25.jpg)
The heap is complete again!
![Page 26: Heapsort Algorithm](https://reader033.vdocuments.net/reader033/viewer/2022051317/568149f3550346895db724b2/html5/thumbnails/26.jpg)
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)
![Page 27: Heapsort Algorithm](https://reader033.vdocuments.net/reader033/viewer/2022051317/568149f3550346895db724b2/html5/thumbnails/27.jpg)
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