Lect5 Heapsort
Lect5 Heapsort
Algorithms
Example:
Input: 8 2 4 9 3 6
Output:
2 3 4 6 8 9
- Input sequence is called an instance of the sorting problem. 2/30
Sorting Algorithms
3/30
Ch2: Heapsort
- Heap Structure
- Maintaining the heap property
- Building a heap
- The heapsort algorithm
4/30
Heap Structure
An array of objects than can be viewed as a complete binary tree
such that:
– Each tree node corresponds
to elements of the array
– The tree is complete except possibly the
lowest level, filled from left to right
– The heap property for all nodes 𝐼 in the
tree must be maintained except for the root
• Parent node(𝐼) 𝐼
5/30
Heap Example
• Given array 22 13 10 8 7 6 2 4 3 5
Min-heaps Array A 1 4 16 7 12 19
– Store data in descending order
1
– The min-heap property is that for every node 𝑖
other than the root, 4 16 Min-heap
- A[Parent(i)] ≤ A[i]
– The smallest element in a min-heap is at the root 7 12 19
8/30
Heapsort Algorithm
o A sorting algorithm that works by first organizing the data to be sorted into a
special type of binary tree called a heap.
o For the heapsort algorithm, we use max-heaps.
Procedures on Heap
o Heapify
Maintain the max-heap property, we call the procedure MAX-HEAPIFY.
How to turn a binary tree into a max-heap
o Build Heap
Convert an array into a max-heap
o Heapsort
Heapsort algorithm starts by using BUILD-MAX-HEAP to build a max-heap 9/30
Maintain the Heap Property
In order to maintain the max-heap property, we call the procedure Max-
Heapify.
Max-Heapify lets the value at 𝐴[𝑖] “float down” in the max-heap so that the
subtree rooted at index 𝑖 obeys the max-heap property.
10/30
Maintaining the heap property(cont.)
11/30
Example of Max-Heapify
A=[1 13 10 8 7 6 2 4 3 5] A=[13 1 10 8 7 6 2 4 3 5]
Max-Heapify(A,1). 1
13
1
1 2 3
1 10
2 3
13 10 4 5 6 7
4 5 6 7
8 7 6 2
8 7 6 2 8 9 10
8 9 10
4 3 5
4 5 6 7 4 7 6 2
1 7 6 2 8 9 10 On this iteration
we have reached
8 9 10
1 3 5 a leaf and are
finished.
4 3 5
Next is Max-Heapify(A,8). 13/30
Max-Heapify Runtime
Conservative recurrence for runtime:
2n
T ( n) T ( ) (1) 𝑻 (𝒏) = 𝑶(𝒍𝒈 𝒏)
3
We can always split the problem into at least 2/3 the size. Consider the
number of nodes on the left side vs. the right in the most unbalanced
state:
In the worst case a heap of height 𝑛 has all of the bottom leaves of the
left child filled and the right child has height 𝑛 − 1. This is the most
unbalanced a tree will ever become due to the heap property.
- Complete binary tree: 2k leaves, 2k-1 interior nodes
14/30
Building a heap
Given an array 𝐴[1 … . . 𝑛], we want to build this array into a max-heap
using the procedure Max-Heapify
Note: Leaves “𝐴[( 𝑛/2 + 1) … . 𝑛]” are already a heap! So start to build
from the leaves and continue recursively to call Max-Heapify, moving up the
tree to the root.
15/30
Example of Building a heap
Build-Max-Heap(A) 1
1
A=[1 5 9 4 7 10 2 6 3 14]
2 3
Max-Heapify(A,10) exits since this is a leaf.
Max-Heapify(A,9) exits since this is a leaf. 5 9
Max-Heapify(A,8) exits since this is a leaf. 6
4 5 7
Max-Heapify(A,7) exits since this is a leaf.
Max-Heapify(A,6) exits since this is a leaf. 4 7 10 2
8 9 10
6 3 14
16/30
Example of Building a heap
1. Max-Heapify(A,5) puts the largest of A[5] and its children, A[10] into A[5]
1 1
1 1
2 3 2 3
5 9 5 9
4 5 6 7 4 5 6 7
4 7 10 2 4 14 10 2
9 10 8 9 10
8
6 3 14 6 3 7
2 3 2 3
5 9 5 10
4 5 6 7 4 5 6 7
6 14 10 2 6 14 9 2
8 9 10
8 9 10
4 3 7
4 3 7
A=[1 5 9 6 14 10 2 4 3 7] A=[1 5 10 6 14 9 2 4 3 7] 19/30
Example of Building a heap
4. Max-Heapify(A,2) 5. Max-Heapify(A,5)
1
1 1
1
1 1
2 3
5 10 2 3 2 3
14 10 14 10
4 5 6 7
4 5 6 7 4 5 6 7
6 14 9 2
6 5 9 2 6 7 9 2
8 9 10
9 10 8 9 10
8
4 3 7
4 3 7 4 3 5
2 3 2 3
7 10 7 10
4 5 6 7 4 5 6 7
6 1 9 2 6 5 9 2
8 9 10 8 9 10
4 3 5 4 3 1
A=[14 7 10 6 1 9 2 4 3 5] A=[14 7 10 6 5 9 2 4 3 1]
25/30
Heapsort Algorithm
Once we can build a Max-Heap and Max-Heapify, sorting is easy
28/30
Example of Heapsort(cont.)
[10,8,9,4,7,1,3,2]– [14,16] [9,8,3,4,7,1,2] – [10,14,16]
29/30
Example of Heapsort(cont.)
[4,2,3,1] – [7,8,9,10,14,16] [3,2,1] – [4,7,8,9,10,14,16] [2,1] – [3,4,7,8,9,10,14,16]
[1] –[2,3,4,7,8,9,10,14,16]
Sorted Array
30/30