Lectures 7-8 - Heapsort
Lectures 7-8 - Heapsort
Heapsort
Introduction to Algorithms
Da Nang University of Science and Technology
Heapsort
Running time: O(n lg n)
Like merge sort
Sorts in place: only a constant number of array elements
are stored outside the input array at any time
Like insertion sort
Heap
A data structure used by Heapsort to manage information
during the execution of the algorithm
Can be used as an efficient priority queue
h=0
h=0
h=2
h=1
h=3
Intelligent Networking Laboratory Dang Thien Binh 38/82
Heaps
Two key properties
Complete binary tree
Value at node
Smaller than or equal to values in subtrees
Greater than or equal to values in subtrees
Example max-heap
YX
X
ZX
Y Z
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
Intelligent Networking Laboratory Dang Thien Binh 40/82
Binary Heap
An array object that can be viewed as a nearly
complete binary tree
Each tree node corresponds to an array element
that stores the value in the tree node
The tree is completely filled on all levels except possibly the
lowest, which is filled from the left up to a point
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]
max-heap and min-heap
11 7
11 7
Length = 10
Heap-Size = 7
i / 2
PARENT (i ) : return
LEFT (i ) : return
2i
2i + 1
RIGHT (i ) : return
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
The property that the values in the node must satisfy
Max-heap property, for every node i other than the
root
A[PARENT(i)] A[i]
The value of a node is at most the value of its parent
The largest element in a max-heap is stored at the root
The subtree rooted at a node contains values
no larger than value of the node itself
MAX-HEAPIFY
Maintains the max-heap property
O(lg n)
BUILD-MAX-HEAP
Produces a max-heap from an unordered input array
O(n)
HEAPSORT
Sorts an array in place
O(n lg n)
MAX-HEAPIFY
Inputs: an array A and an index i into the array
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
MAX-HEAPIFY lets the value at A[i] “float down” in the
max-heap
16
4 10
4 < 14
14 7 9 3
2 8 1
16
14 10
4 7 9 3
4<8
2 8 1
16
14 10
8 7 9 3
2 4 1
lg n lg n h
n h
h =0
2 h +1 O(h) = O(n 2 h ) = O(n 2 h ) = O(n)
h =0 h =0
x
kxk =
k =0 (1 − x) 2 (for |x| < 1)
8 9 10
2 4 1 16 14 10 8 7 9 3 2 4 1
8 9 10
2 1 16
Intelligent Networking Laboratory Dang Thien Binh 64/82
Example: Heapsort (3/8)
1
10
2 3
8 9
4 5 6 7
4 7 1 3
8 9 10
2 14 16
Intelligent Networking Laboratory Dang Thien Binh 65/82
Example: Heapsort (4/8)
1
9
2 3
8 3
4 5 6 7
4 7 1 2
8 9 10
10 14 16
Intelligent Networking Laboratory Dang Thien Binh 66/82
Example: Heapsort (5/8)
1
7
2 3
4 3
4 5 6 7
1 2 8 9
8 9 10
10 14 16
Intelligent Networking Laboratory Dang Thien Binh 67/82
Example: Heapsort (6/8)
1
4
2 3
2 3
4 5 6 7
1 7 8 9
8 9 10
10 14 16
Intelligent Networking Laboratory Dang Thien Binh 68/82
Example: Heapsort (7/8)
1
1
2 3
2 3
4 5 6 7
4 7 8 9
8 9 10
10 14 16
Intelligent Networking Laboratory Dang Thien Binh 69/82
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
O(lg n)
O(lg n)
8 9 10
2 4 1 increase 4 to 15
Intelligent Networking Laboratory Dang Thien Binh 77/82
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
Intelligent Networking Laboratory Dang Thien Binh 78/82
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
Intelligent Networking Laboratory Dang Thien Binh 79/82
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
Intelligent Networking Laboratory Dang Thien Binh 80/82
Insert-Max
O(lg n)