Heapsort: By: Vimal Awasthi B.Tech (CSE)
Heapsort: By: Vimal Awasthi B.Tech (CSE)
By:
Vimal Awasthi
B.Tech (CSE)
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(n2)
– Quicksort is generally faster, but Heapsort is
better in time-critical applications
• Heapsort is a really cool algorithm!
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
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
n-2
n-1
n
Balanced Balanced Not balanced
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
8 3 8 12 8 14
Blue node has Blue node has Blue node does not
heap property heap property have heap property
• All leaf nodes automatically have the heap property
• A binary tree is a heap if all nodes in it have the
heap property
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
12 14
8 14 8 12
Blue node does not Blue node has
have heap property heap property
10 8 8 5
1 2 3
10 10 12
8 5 12 5 10 5
12 8 8
4
Other children are not affected
12 12 14
10 5 14 5 12 5
8 14 8 10 8 10
• The node containing 8 is not affected because its parent gets larger,
not smaller
• The node containing 5 is not affected because its parent gets larger,
not smaller
• The node containing 8 is still not affected because, although its
parent got smaller, its parent is still greater than it was originally
A sample heap
• Here’s a sample binary tree after it has been heapified
25
22 17
19 22 14 15
18 14 21 3 9 11
22 17
19 22 14 15
18 14 21 3 9 11
22 17
19 22 14 15
18 14 21 3 9
11 17
19 22 14 15
18 14 21 3 9
22 17
19 11 14 15
18 14 21 3 9
22 17
19 21 14 15
18 14 11 3 9
22 17
19 22 14 15
18 14 21 3 9 11
0 1 2 3 4 5 6 7 8 9 10 11 12
25 22 17 19 22 14 15 18 14 21 3 9 11
• Notice:
– The left child of index i is at index 2*i+1
– The right child of index i is at index 2*i+2
– Example: the children of node 3 (19) are 7 (18) and 8 (14)
Removing and replacing the root
• The “root” is the first element in the array
• The “rightmost node at the deepest level” is the last element
• Swap them...
0 1 2 3 4 5 6 7 8 9 10 11 12
25 22 17 19 22 14 15 18 14 21 3 9 11
0 1 2 3 4 5 6 7 8 9 10 11 12
11 22 17 19 22 14 15 18 14 21 3 9 25
0 1 2 3 4 5 6 7 8 9 10 11 12
22 22 17 19 21 14 15 18 14 11 3 9 25
0 1 2 3 4 5 6 7 8 9 10 11 12
9 22 17 19 22 14 15 18 14 21 3 22 25