+ All Categories
Home > Documents > Heapsort Algorithm

Heapsort Algorithm

Date post: 18-Mar-2016
Category:
Upload: braden
View: 56 times
Download: 0 times
Share this document with a friend
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 Presentation
27
Heapsort Algorithm Eric & Bruce
Transcript
Page 1: Heapsort Algorithm

Heapsort Algorithm

Eric & Bruce

Page 2: Heapsort Algorithm

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

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

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

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

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

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

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

Almost Complete Binary Tree

Page 10: Heapsort Algorithm

V should be pushed to the left

Page 11: Heapsort Algorithm

Level 3 should be completely filled

Page 12: Heapsort Algorithm

This is a Heap.

Page 13: Heapsort Algorithm

Heap as an Array

Heap

Array

Page 14: Heapsort Algorithm

Lets see some L33t codeParent(CI) = (CI - 1) / 2;

RightChild(CI) = 2 * (CI + 1);

LeftChild(CI) = 2 * CI + 1;

Page 15: Heapsort Algorithm

Inserting into a Heap

• Insert “E”

Page 16: Heapsort Algorithm

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

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

Page 18: Heapsort Algorithm

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

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

Page 20: Heapsort Algorithm

“D” is smaller then “H”. So, Swap positions

“D” is not smaller then “C” so the heap is complete

Page 21: Heapsort Algorithm

Removing from the Heap

Always take the root node.

Page 22: Heapsort Algorithm

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

Page 23: Heapsort Algorithm

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

Page 24: Heapsort Algorithm

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

Page 25: Heapsort Algorithm

The heap is complete again!

Page 26: Heapsort Algorithm

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

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


Recommended