Lecture 12 - Heap Sort
Lecture 12 - Heap Sort
1
Recap: Types of Sorting Algorithms
• Non-recursive/Incremental comparison sorting
• Selection sort
• Bubble sort
• Insertion sort
• Recursive comparison sorting
• Merge sort
• Quick sort
• Heap sort
• Non-comparison linear sorting
• Count sort
• Radix sort
2
• Bucket sort
What is a Heap?
A heap is a data structure that stores a collection of objects (with
keys), and has the following properties:
3
What is a Complete Binary Tree?
• All the levels in the tree are completely filled except the last level.
• In the last level, all the keys are assigned as left as possible.
4
What is a Complete Binary Tree?
• All the levels in the tree are completely filled except the last level.
• In the last level, all the keys are assigned as left as possible.
1 2 3 4 5 6
8 7 10 1 5 9
5
Array A
What is a Complete Binary Tree?
• All the levels in the tree are completely filled except the last level.
• In the last level, all the keys are assigned as left as possible.
1
8
1 2 3 4 5 6
8 7 10 1 5 9
6
Array A
What is a Complete Binary Tree?
• All the levels in the tree are completely filled except the last level.
• In the last level, all the keys are assigned as left as possible.
1
8
2
7 1 2 3 4 5 6
8 7 10 1 5 9
7
Array A
What is a Complete Binary Tree?
• All the levels in the tree are completely filled except the last level.
• In the last level, all the keys are assigned as left as possible.
1
8
2 3
7 10 1 2 3 4 5 6
8 7 10 1 5 9
8
Array A
What is a Complete Binary Tree?
• All the levels in the tree are completely filled except the last level.
• In the last level, all the keys are assigned as left as possible.
1
8
2 3
7 10 1 2 3 4 5 6
8 7 10 1 5 9
4 9
1 Array A
What is a Complete Binary Tree?
• All the levels in the tree are completely filled except the last level.
• In the last level, all the keys are assigned as left as possible.
1
8
2 3
7 10 1 2 3 4 5 6
8 7 10 1 5 9
4 5 10
1 5 Array A
What is a Complete Binary Tree?
• All the levels in the tree are completely filled except the last level.
• In the last level, all the keys are assigned as left as possible.
1
8
2 3
7 10 1 2 3 4 5 6
8 7 10 1 5 9
4 5 6 11
1 5 9 Array A
What is a Complete Binary Tree?
The root of the tree A[1] and given index 𝑖 of a node, the indices of
its parent, left child and right child can be computed:
1
8
2 3
7 10 1 2 3 4 5 6
8 7 10 1 5 9
4 5 6 12
1 5 9 Array A
What is a Complete Binary Tree?
The root of the tree A[1] and given index 𝑖 of a node, the indices of
its parent, left child and right child can be computed:
1
8
2 3
7 10 1 2 3 4 5 6
8 7 10 1 5 9
4 5 6 13
1 5 9 Array A
What is a Complete Binary Tree?
The root of the tree A[1] and given index 𝑖 of a node, the indices of
its parent, left child and right child can be computed:
PARENT (3)
1 return 3/2 = 1.5 = 1
8
2 3
7 10 1 2 3 4 5 6
8 7 10 1 5 9
4 5 6 14
1 5 9 Array A
What is a Complete Binary Tree?
The root of the tree A[1] and given index 𝑖 of a node, the indices of
its parent, left child and right child can be computed:
PARENT (3)
1 return 3/2 = 1.5 = 1
8
2 3
7 10 1 2 3 4 5 6
8 7 10 1 5 9
4 5 6 15
1 5 9 Array A
What is a Complete Binary Tree?
The root of the tree A[1] and given index 𝑖 of a node, the indices of
its parent, left child and right child can be computed:
1
8
2 3
7 10 1 2 3 4 5 6
8 7 10 1 5 9
4 5 6 16
1 5 9 Array A
What is a Complete Binary Tree?
The root of the tree A[1] and given index 𝑖 of a node, the indices of
its parent, left child and right child can be computed:
LEFT_CHILD (3)
1 return 2(3) = 6
8
2 3
7 10 1 2 3 4 5 6
8 7 10 1 5 9
4 5 6 17
1 5 9 Array A
What is a Complete Binary Tree?
The root of the tree A[1] and given index 𝑖 of a node, the indices of
its parent, left child and right child can be computed:
LEFT_CHILD (3)
1 return 2(3) = 6
8
2 3
7 10 1 2 3 4 5 6
8 7 10 1 5 9
4 5 6 18
1 5 9 Array A
What is a Complete Binary Tree?
The root of the tree A[1] and given index 𝑖 of a node, the indices of
its parent, left child and right child can be computed:
1
8
2 3
7 10 1 2 3 4 5 6
8 7 10 1 5 9
4 5 6 19
1 5 9 Array A
What is a Complete Binary Tree?
The root of the tree A[1] and given index 𝑖 of a node, the indices of
its parent, left child and right child can be computed:
RIGHT_CHILD (2)
1 return 2 2 + 1 = 5
8
2 3
7 10 1 2 3 4 5 6
8 7 10 1 5 9
4 5 6 20
1 5 9 Array A
What is a Complete Binary Tree?
The root of the tree A[1] and given index 𝑖 of a node, the indices of
its parent, left child and right child can be computed:
RIGHT_CHILD (2)
1 return 2 2 + 1 = 5
8
2 3
7 10 1 2 3 4 5 6
8 7 10 1 5 9
4 5 6 21
1 5 9 Array A
What is a Heap Order Property?
• For every node 𝑖, other than the root, the key stored in 𝑖 is greater
or equal than the key stored in the parent of 𝑖. (Case for Min Heap)
• For every node 𝑖, other than the root, the key stored in 𝑖 is smaller
or equal than the key stored in the parent of 𝑖. (Case for Max Heap)
• Max Heap
• Store data in ascending order
• Has property of
𝐴[𝑃𝑎𝑟𝑒𝑛𝑡(𝑖)] ≥ 𝐴[𝑖]
• Min Heap
• Store data in descending order
• Has property of 22
𝐴[𝑃𝑎𝑟𝑒𝑛𝑡(𝑖)] ≤ 𝐴[𝑖]
Max Heap Example
19
12 16
1 4 7
19 12 16 1 4 7
Array A 23
Min Heap Example
4 16
7 12 19
1 4 16 7 12 19
Array A 24
Heap Property
20
Larger than
its parent
15 8
4 10 7 9
Not a Heap
25
Not left-justified 10 12
9 5 11 7
25
1 6 3 8
Insertion of Key in a Heap
Algorithm
1. Add the new element to the next available position at the lowest
level
2. Restore the max-heap property if violated
General strategy is percolate up (or bubble up): if the parent of the
element is smaller than the element, then interchange the parent and
child.
OR
12 16 12 16
4 7 1 4 7 17
1
Insert 17
19
12 17
swap
27
4 7 16 Filter gradually up to
1
maintain the heap property
Deletion of Key in a Heap
Delete max
• Copy the last number to the root (overwrite the maximum element
stored there).
• Restore the max heap property by percolate down (filter down).
Delete min
• Copy the last number to the root (overwrite the minimum element
stored there).
• Restore the min heap property by percolate down (filter down).
28
Deletion of Key in a Max Heap
19
swap
delete 19
12 16 12 16
4 7 1 4 7
1
7 16
swap
12 16 12 7
29
Filter gradually
4 4 down to maintain
1 1
the heap property
Maintaining the Heap Property
Suppose a node is smaller than a child, violating heap property. To
maintain heap property, exchange the node with the larger child,
moving the node down the tree until it is not smaller than its children
Alg: MAX-HEAPIFY(A, i, n)
1. l ← LEFT_CHILD(i)
2. r ← RIGHT_CHILD(i)
3. if l ≤ n and A[l] > A[i]
4. then largest ←l
5. else largest ←i
6. if r ≤ n and A[r] > A[largest]
7. then largest ←r
8. if largest i
9. then exchange A[i] A[largest] 30
10. MAX-HEAPIFY(A, largest, n)
Maintaining the Heap Property
Suppose a node is smaller than a child, violating heap property. To
maintain heap property, exchange the node with the larger child,
moving the node down the tree until it is not smaller than its children
Alg: MAX-HEAPIFY(A, i, n) 1
1. l ← LEFT_CHILD(i) l=?, r=?, n=6 14
largest = ?
2. r ← RIGHT_CHILD(i) 𝑖=2 3
3. if l ≤ n and A[l] > A[i] 4 7
5
4. then largest ←l
4 2 8 1 6
5. else largest ←i
6. if r ≤ n and A[r] > A[largest]
7. then largest ←r
8. if largest i
9. then exchange A[i] A[largest] 38
10. MAX-HEAPIFY(A, largest, n)
Maintaining the Heap Property
Suppose a node is smaller than a child, violating heap property. To
maintain heap property, exchange the node with the larger child,
moving the node down the tree until it is not smaller than its children
Alg: MAX-HEAPIFY(A, i, n) 1
1. l ← LEFT_CHILD(i) l=4, r=5, n=6 14
largest = ?
2. r ← RIGHT_CHILD(i) 𝑖=2 3
3. if l ≤ n and A[l] > A[i] 4 7
5
4. then largest ←l
4 2 8 1 6
5. else largest ←i
6. if r ≤ n and A[r] > A[largest]
7. then largest ←r
8. if largest i
9. then exchange A[i] A[largest] 39
10. MAX-HEAPIFY(A, largest, n)
Maintaining the Heap Property
Suppose a node is smaller than a child, violating heap property. To
maintain heap property, exchange the node with the larger child,
moving the node down the tree until it is not smaller than its children
Alg: MAX-HEAPIFY(A, i, n) 1
1. l ← LEFT_CHILD(i) l=4, r=5, n=6 14
largest = ?
2. r ← RIGHT_CHILD(i) 𝑖=2 3
3. if l ≤ n and A[l] > A[i] 4 7
5
4. then largest ←l
4 2 8 1 6
5. else largest ←i
6. if r ≤ n and A[r] > A[largest]
7. then largest ←r
8. if largest i
9. then exchange A[i] A[largest] 40
10. MAX-HEAPIFY(A, largest, n)
Maintaining the Heap Property
Suppose a node is smaller than a child, violating heap property. To
maintain heap property, exchange the node with the larger child,
moving the node down the tree until it is not smaller than its children
Alg: MAX-HEAPIFY(A, i, n) 1
1. l ← LEFT_CHILD(i) l=4, r=5, n=6 14
largest = 2
2. r ← RIGHT_CHILD(i) 𝑖=2 3
3. if l ≤ n and A[l] > A[i] 4 7
5
4. then largest ←l
4 2 8 1 6
5. else largest ←i
6. if r ≤ n and A[r] > A[largest]
7. then largest ←r
8. if largest i
9. then exchange A[i] A[largest] 41
10. MAX-HEAPIFY(A, largest, n)
Maintaining the Heap Property
Suppose a node is smaller than a child, violating heap property. To
maintain heap property, exchange the node with the larger child,
moving the node down the tree until it is not smaller than its children
Alg: MAX-HEAPIFY(A, i, n) 1
1. l ← LEFT_CHILD(i) l=4, r=5, n=6 14
largest = 2
2. r ← RIGHT_CHILD(i) 𝑖=2 3
3. if l ≤ n and A[l] > A[i] 4 7
5
4. then largest ←l
4 2 8 1 6
5. else largest ←i
6. if r ≤ n and A[r] > A[largest]
7. then largest ←r
8. if largest i
9. then exchange A[i] A[largest] 42
10. MAX-HEAPIFY(A, largest, n)
Maintaining the Heap Property
Suppose a node is smaller than a child, violating heap property. To
maintain heap property, exchange the node with the larger child,
moving the node down the tree until it is not smaller than its children
Alg: MAX-HEAPIFY(A, i, n) 1
1. l ← LEFT_CHILD(i) l=4, r=5, n=6 14
largest = 5
2. r ← RIGHT_CHILD(i) 𝑖=2 3
3. if l ≤ n and A[l] > A[i] 4 7
5
4. then largest ←l
4 2 8 1 6
5. else largest ←i
6. if r ≤ n and A[r] > A[largest]
7. then largest ←r
8. if largest i
9. then exchange A[i] A[largest] 43
10. MAX-HEAPIFY(A, largest, n)
Maintaining the Heap Property
Suppose a node is smaller than a child, violating heap property. To
maintain heap property, exchange the node with the larger child,
moving the node down the tree until it is not smaller than its children
Alg: MAX-HEAPIFY(A, i, n) 1
1. l ← LEFT_CHILD(i) l=4, r=5, n=6 14
largest = 5
2. r ← RIGHT_CHILD(i) 𝑖=2 3
3. if l ≤ n and A[l] > A[i] 4 7
5
4. then largest ←l
4 2 8 1 6
5. else largest ←i
6. if r ≤ n and A[r] > A[largest]
7. then largest ←r
8. if largest i
9. then exchange A[i] A[largest] 44
10. MAX-HEAPIFY(A, largest, n)
Maintaining the Heap Property
Suppose a node is smaller than a child, violating heap property. To
maintain heap property, exchange the node with the larger child,
moving the node down the tree until it is not smaller than its children
Alg: MAX-HEAPIFY(A, i, n) 1
1. l ← LEFT_CHILD(i) l=4, r=5, n=6 14
largest = 5
2. r ← RIGHT_CHILD(i) 𝑖=2 3
3. if l ≤ n and A[l] > A[i] 8 7
5
4. then largest ←l
4 2 4 1 6
5. else largest ←i
6. if r ≤ n and A[r] > A[largest]
7. then largest ←r
8. if largest i
9. then exchange A[i] A[largest] 45
10. MAX-HEAPIFY(A, largest, n)
Maintaining the Heap Property
Suppose a node is smaller than a child, violating heap property. To
maintain heap property, exchange the node with the larger child,
moving the node down the tree until it is not smaller than its children
Alg: MAX-HEAPIFY(A, i, n) 1
1. l ← LEFT_CHILD(i) l=4, r=5, n=6 14
largest = 5
2. r ← RIGHT_CHILD(i) 𝑖=2 3
3. if l ≤ n and A[l] > A[i] 8 7
5
4. then largest ←l
4 2 4 1 6
5. else largest ←i
6. if r ≤ n and A[r] > A[largest]
7. then largest ←r
8. if largest i
9. then exchange A[i] A[largest] 46
10. MAX-HEAPIFY(A, largest, n)
Maintaining the Heap Property
Suppose a node is smaller than a child, violating heap property. To
maintain heap property, exchange the node with the larger child,
moving the node down the tree until it is not smaller than its children
Alg: MAX-HEAPIFY(A, i, n) 1
1. l ← LEFT_CHILD(i) l=?, r=?, n=6 14
largest = ?
2. r ← RIGHT_CHILD(i) 2 3
3. if l ≤ n and A[l] > A[i] 8 7
i=5
4. then largest ←l
4 2 4 1 6
5. else largest ←i
6. if r ≤ n and A[r] > A[largest]
7. then largest ←r
8. if largest i
9. then exchange A[i] A[largest] 47
10. MAX-HEAPIFY(A, largest, n)
Maintaining the Heap Property
Suppose a node is smaller than a child, violating heap property. To
maintain heap property, exchange the node with the larger child,
moving the node down the tree until it is not smaller than its children
Alg: MAX-HEAPIFY(A, i, n) 1
1. l ← LEFT_CHILD(i) l=10, r=11, n=6 14
largest = ?
2. r ← RIGHT_CHILD(i) 2 3
3. if l ≤ n and A[l] > A[i] 8 7
i=5
4. then largest ←l
4 2 4 1 6
5. else largest ←i
6. if r ≤ n and A[r] > A[largest]
7. then largest ←r
8. if largest i
9. then exchange A[i] A[largest] 48
10. MAX-HEAPIFY(A, largest, n)
Maintaining the Heap Property
Suppose a node is smaller than a child, violating heap property. To
maintain heap property, exchange the node with the larger child,
moving the node down the tree until it is not smaller than its children
Alg: MAX-HEAPIFY(A, i, n) 1
1. l ← LEFT_CHILD(i) l=10, r=11, n=6 14
largest = ?
2. r ← RIGHT_CHILD(i) 2 3
3. if l ≤ n and A[l] > A[i] 8 7
i=5
4. then largest ←l
4 2 4 1 6
5. else largest ←i
6. if r ≤ n and A[r] > A[largest]
7. then largest ←r
8. if largest i
9. then exchange A[i] A[largest] 49
10. MAX-HEAPIFY(A, largest, n)
Maintaining the Heap Property
Suppose a node is smaller than a child, violating heap property. To
maintain heap property, exchange the node with the larger child,
moving the node down the tree until it is not smaller than its children
Alg: MAX-HEAPIFY(A, i, n) 1
1. l ← LEFT_CHILD(i) l=10, r=11, n=6 14
largest = 5
2. r ← RIGHT_CHILD(i) 2 3
3. if l ≤ n and A[l] > A[i] 8 7
i=5
4. then largest ←l
4 2 4 1 6
5. else largest ←i
6. if r ≤ n and A[r] > A[largest]
7. then largest ←r
8. if largest i
9. then exchange A[i] A[largest] 50
10. MAX-HEAPIFY(A, largest, n)
Maintaining the Heap Property
Suppose a node is smaller than a child, violating heap property. To
maintain heap property, exchange the node with the larger child,
moving the node down the tree until it is not smaller than its children
Alg: MAX-HEAPIFY(A, i, n) 1
1. l ← LEFT_CHILD(i) l=10, r=11, n=6 14
largest = 5
2. r ← RIGHT_CHILD(i) 2 3
3. if l ≤ n and A[l] > A[i] 8 7
i=5
4. then largest ←l
4 2 4 1 6
5. else largest ←i
6. if r ≤ n and A[r] > A[largest]
7. then largest ←r
8. if largest i
9. then exchange A[i] A[largest] 51
10. MAX-HEAPIFY(A, largest, n)
Maintaining the Heap Property
Suppose a node is smaller than a child, violating heap property. To
maintain heap property, exchange the node with the larger child,
moving the node down the tree until it is not smaller than its children
Alg: MAX-HEAPIFY(A, i, n) 1
1. l ← LEFT_CHILD(i) l=10, r=11, n=6 14
largest = 5
2. r ← RIGHT_CHILD(i) 2 3
3. if l ≤ n and A[l] > A[i] 8 7
i=5
4. then largest ←l
4 2 4 1 6
5. else largest ←i
6. if r ≤ n and A[r] > A[largest]
7. then largest ←r
8. if largest i
9. then exchange A[i] A[largest] 52
10. MAX-HEAPIFY(A, largest, n)
Maintaining the Heap Property
Suppose a node is smaller than a child, violating heap property. To
maintain heap property, exchange the node with the larger child,
moving the node down the tree until it is not smaller than its children
Alg: MAX-HEAPIFY(A, i, n) 1
1. l ← LEFT_CHILD(i) 14
2. r ← RIGHT_CHILD(i) 2 3
3. if l ≤ n and A[l] > A[i] 8 7
5
4. then largest ←l
4 2 4 1 6
5. else largest ←i
6. if r ≤ n and A[r] > A[largest]
7. then largest ←r Complexity of MAX-HEAPIFY()?
8. if largest i
9. then exchange A[i] A[largest] O(lg n) where lg n is the
53
10. MAX-HEAPIFY(A, largest, n) height of the tree
Array Representation of Heaps
A heap can be stored as an array A.
• Root of tree is A[1] 1 2 3 4 5 6 7 8 9 10
26 24 20 18 17 19 13 12 14 11
• Left child of A[i] = A[2i]
Max-heap as an array
• Right child of A[i] = A[2i + 1]
• Parent of A[i] = A[i/2] 26
• Heapsize[A] ≤ length[A]
24 20
The elements in the subarray
A[(n/2+1) .. n] are leaves
18 17 19 13
Alg: BUILD-MAX-HEAP(A)
1. n = length[A]
2. for i ← n/2 downto 1
O(n)
3. do MAX-HEAPIFY(A, i, n) O(lgn)
1
4
Complexity of BUILD-MAX-HEAP? O(n lgn)
2 3
1 3 55
4 5 6 7
A: 4 1 3 2 16 9 10 14 8 7 2 16 9 10
8 9 10
14 8 7
Example: A: 4 1 3 2 16 9 10 14 8 7
4 4 4
2 3 2 3 2 3
1 3 1 3 1 3
4 5 6 7 4 5 6 7 4 5 6 7
8
2 9 10
16 9 10 8
2 9 10
16 9 10 8
14 9 10
16 9 10
14 8 7 14 8 7 2 8 7
i=2 i=1
1 1 1
4 4 16
2 3 2 3 2 3
1 10 16 10 14 10
4 5 6 7 4 5 6 7 4 5 6 7
8
14 9 10
16 9 3 8
14 9 10
7 9 3 8
8 9 10
7 9 3
2 8 7 2 8 1 2 4 1
56
Heap Sort
A given array which has been heapified does not mean it is sorted
25
22 17
14 21 9 11
The new root has lost heap property. Heapify the new root and
repeat this process until only one node remain. 57
Example: A=[7, 4, 3, 1, 2]
MAX-HEAPIFY(A, 1, 1) 58
Heap Sort Pseudocode
HEAPSORT(A)
1. BUILD-MAX-HEAP(A) O(n lgn)
2. for i ← length[A] downto 2
3. do exchange A[1] A[i] n-1 times
4. MAX-HEAPIFY(A, 1, i - 1) O(lgn)
What is the running time of HEAPSORT()? O(n lgn)
19 12 16 1 4 7
19
12 16
60
1 4 7
Example
Take out biggest
19
12 16
Move the last element
to the root
1 4 7
Sorted:
Array A
12 16 1 4 7 19
61
7 12 16 1 4 19
Example
7
swap
HEAPIFY()
12 16
1 4
Sorted:
Array A
7 12 16 1 4 19
62
7 12 16 1 4 19
Example
16
12 7
1 4
Sorted:
Array A
16 12 7 1 4 19
63
16 12 7 1 4 19
Example
Take out biggest
16
Move the last element
to the root
12 7
1 4
Sorted:
Array A
12 7 1 4 16 19
64
4 12 7 1 16 19
Example
4
12 7
Sorted:
Array A
4 12 7 1 16 19
65
4 12 7 1 16 19
Example
swap 4
HEAPIFY()
12 7
Sorted:
Array A
4 12 7 1 16 19
66
4 12 7 1 16 19
Example
12
4 7
Sorted:
Array A
12 4 7 1 16 19
67
12 4 7 1 16 19
Example
Take out biggest
12
Move the last
element to the
root 4 7
Sorted:
Array A
4 7 1 12 16 19
68
1 4 7 12 16 19
Example
1
swap
4 7
Sorted:
Array A
1 4 7 12 16 19
69
1 4 7 12 16 19
Example
4 1
Sorted:
Array A
7 4 1 12 16 19
70
7 4 1 12 16 19
Example
Take out biggest
7
Move the last
element to the
4 1 root
Sorted:
Array A
1 4 7 12 16 19
71
1 4 7 12 16 19
Example
swap 1
HEAPIFY()
4
Sorted:
Array A
1 4 7 12 16 19
72
1 4 7 12 16 19
Example
4
Sorted:
Array A
4 1 7 12 16 19
73
4 1 7 12 16 19
Example
Take out biggest
Move the last 4
element to the
root
1
Sorted:
Array A
1 4 7 12 16 19
74
1 4 7 12 16 19
Example
Take out biggest
1
Sorted:
Array A
1 4 7 12 16 19
75
1 4 7 12 16 19
Example
Sorted:
1 4 7 12 16 19
76
Priority Queues
Priority Queue is an extension of queue with
following properties.
⚫ Every item has a priority associated with it.