Professional Documents
Culture Documents
Heapsort
Introduction for Heapsort
¢ Heapsort
u Running time: O(n lg n)
ê Like merge sort
u Sorts in place: only a constant number of array elements
are stored outside the input array at any time
ê Like insertion sort
¢ Heap
u A data structure used by Heapsort to manage information
during the execution of the algorithm
u Can be used as an efficient priority queue
Perfect Binary Tree
h=0
h=0
h=2
h=1
Complete Binary Trees
h=3
Heaps
¢ Two key properties
u Complete binary tree
u Value at node
ê Smaller than or equal to values in subtrees
ê Greater than or equal to values in subtrees
¢ Example max-heap
uY£X
X
uZ£X
Y Z
Heap and Non-heap Examples
5
5
8
5 45
6 45
5 2
2
6 22 6 22
6 22
25 8 45 25
8 45 25
Min-heaps Non-heaps
Binary Heap
¢ An array that can be viewed as a nearly
complete binary tree
u Each tree node corresponds to an array element
that stores the value in the tree node
u The tree is completely filled on all levels except possibly the
lowest, which is filled from the left up to a point
u Array A has two attributes
ê length[A]: number of elements in the array
ê heap-size[A]: number of elements in the heap stored within A
ê heap-size[A] £ length[A]
u max-heap and min-heap
Max-heap
Length and Heap-Size
11 7
11 7
Length = 10
Heap-Size = 7?
Heap Computation
¢ Given the index i of a node, the indices of its parent,
left child, and right child can be computed simply:
8 9 10
1 2 3 4 5 6 7 8 9 10
3 2 4 1 16 14 10 8 7 9 3 2 4 1
Heap Property
¢ Heap property
u The property that the values in the node must satisfy
¢ Max-heap property, for every node i other than the
root
u A[PARENT(i)] ³ A[i]
u The value of a node is at most the value of its parent
u The largest element in a max-heap is stored at the root
u The subtree rooted at a node contains values
no larger than value of the node itself
Heap Height
¢ MAX-HEAPIFY
u Maintains the max-heap property
u O(lg n)
¢ BUILD-MAX-HEAP
u Produces a max-heap from an unordered input array
u O(n)
¢ HEAPSORT
u Sorts an array in place
u O(n lg n)
Maintaining the Heap Property
¢ MAX-HEAPIFY
u Inputs: an array A and an index i into the array
u Assume the binary trees rooted at LEFT(i) and RIGHT(i) are
max-heaps, but A[i] may be smaller than its children
ê violate the max-heap property
u MAX-HEAPIFY lets the value at A[i] “float down” in the
max-heap
MAX-HEAPIFY
16
4 10
4 < 14
14 7 9 3
2 8 1
Example of MAX-HEAPIFY (2/3)
16
14 10
4 7 9 3
4<8
2 8 1
Example of MAX-HEAPIFY (3/3)
16
14 10
8 7 9 3
2 4 1
MAX-HEAPIFY
ëlg n û ëlg n û h ¥
é n ù h
å
h =0
êê 2h+1 úú O(h) = O(n å 2h ) = O(nå 2h ) = O(n)
h =0 h =0
¥ 1
Ignore the constant ½ h 2 =2
å h
=
h =0 2 (1 - 1 ) 2
2
¥
x
å kx k =
k =0 (1 - x) 2 (for |x| < 1)
Heapsort
8 9 10
2 4 1 16 14 10 8 7 9 3 2 4 1
Example: Heapsort (2/8)
1
14
2 3
8 10
4 5 6 7
4 7 9 3
8 9 10
2 1 16
Example: Heapsort (3/8)
1
10
2 3
8 9
4 5 6 7
4 7 1 3
8 9 10
2 14 16
Example: Heapsort (4/8)
1
9
2 3
8 3
4 5 6 7
4 7 1 2
8 9 10
10 14 16
Example: Heapsort (5/8)
1
7
2 3
4 3
4 5 6 7
1 2 8 9
8 9 10
10 14 16
Example: Heapsort (6/8)
1
4
2 3
2 3
4 5 6 7
1 7 8 9
8 9 10
10 14 16
Example: Heapsort (7/8)
1
1
2 3
2 3
4 5 6 7
4 7 8 9
8 9 10
10 14 16
Example: Heapsort (8/8)
1
1
2 3
2 3
4 5 6 7
4 7 8 9
8 9 10
10 14 16
1 2 3 4 7 8 9 10 14 16
Heapsort Algorithm
Priority Queues
O(lg n)
Increase-Key
O(lg n)
Example: increase key (1/4)
1
16
2 3
14 10
4 5 6 7
8 7 9 3
8 9 10
2 4 1 increase 4 to 15
Example: increase key (2/4)
1
16
2 3
14 10
4 5 6 7
8 7 9 3
8 9 10
2 15 1
Example: increase key (3/4)
1
16
2 3
14 10
4 5 6 7
15 7 9 3
8 9 10
2 8 1
Example: increase key (4/4)
1
16
2 3
15 10
4 5 6 7
14 7 9 3
8 9 10
2 8 1
Insert-Max
O(lg n)
Practice Problems
¢ Show the resulting heap after insert 20 into the following
heap