#13 Heap Sort

#13 Heap Sort - Heapsort Why study Heapsort? It is a...

Info iconThis preview shows pages 1–9. Sign up to view the full content.

View Full Document Right Arrow Icon
Heapsort
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Why study Heapsort? It is a well-known, traditional sorting algorithm you will be expected to know Heapsort is always O(n log n) Quicksort is usually O(n log n) but in the worst case slows to O(n 2 ) Quicksort is generally faster, but Heapsort is better in time-critical applications Heapsort is a really cool algorithm!
Background image of page 2
What is a “heap”? Definitions of heap : 1. A large area of memory from which the programmer can allocate blocks as needed, and deallocate them (or allow them to be garbage collected) when no longer needed 2. A balanced, left-justified binary tree in which no node has a value greater than the value in its parent These two definitions have little in common Heapsort uses the second definition
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Balanced binary trees Recall: The depth of a node is its distance from the root The depth of a tree is the depth of the deepest node A binary tree of depth n is balanced if all the nodes at depths 0 through n-2 have two children Balanced Balanced Not balanced n-2 n-1 n
Background image of page 4
Left-justified binary trees A balanced binary tree is left-justified if: all the leaves are at the same depth, or all the leaves at depth n+1 are to the left of all the nodes at depth n Left-justified Not left-justified
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Plan of attack First, we will learn how to turn a binary tree into a heap Next, we will learn how to turn a binary tree back into a heap after it has been changed in a certain way Finally (this is the cool part) we will see how to use these ideas to sort an array
Background image of page 6
The heap property A node has the heap property if the value in the node is as large as or larger than the values in its children All leaf nodes automatically have the heap property A binary tree is a heap if all nodes in it have the heap property 12 8 3 Blue node has heap property 12 8 12 Blue node has heap property 12 8 14 Blue node does not have heap property
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
siftUp Given a node that does not have the heap property, you can give it the heap property by exchanging its value with the value of the larger child This is sometimes called sifting up Notice that the child may have lost the heap property 14 8 12 Blue node has heap property 12 8 14 Blue node does not have heap property
Background image of page 8
Image of page 9
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 06/14/2011 for the course COMPUTER 091 taught by Professor Rajivsir during the Summer '11 term at MIT.

Page1 / 27

#13 Heap Sort - Heapsort Why study Heapsort? It is a...

This preview shows document pages 1 - 9. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online