Analysis of Heapsort
Analysis of Heapsort
Analysis of Heapsort
CMSC 142
Analysis of Heapsort
INSERTION SORT
MERGE SORT
HEAP SORT
FEW UNIQUE
RANDOM
REVERSED
3
ALMOST SORTED
Heap Types
Max-heaps (largest element at root), have the
max-heap property:
for all nodes i, excluding the root:
A[PARENT(i)] A[i]
Adding/Deleting Nodes
New nodes are always inserted at the bottom
level (left to right)
Nodes are removed from the bottom level (right
to left)
Operations on Heaps
Maintain/Restore the max-heap property
MAX-HEAPIFY
Priority queues
Example
MAX-HEAPIFY(A, 2, 10)
A[2] A[4]
A[4] A[9]
Alg: MAX-HEAPIFY(A, i, n)
Left and Right
1. l LEFT(i)
subtrees of i are 2. r RIGHT(i)
max-heaps
3. if l n and A[l] > A[i]
A[i] may be
4.
then largest l
smaller than its
5.
else largest i
children
6. if r n and A[r] > A[largest]
7.
then largest r
8. if largest i
9.
then exchange A[i] A[largest]
10.
MAX-HEAPIFY(A, largest, n)
9
h
O(h)
2h
Building a Heap
Convert an array A[1 n] into a max-heap (n = length[A])
The elements in the subarray A[( n/2 +1) .. n] are leaves
Apply MAX-HEAPIFY on elements between 1 and n/2
Alg: BUILD-MAX-HEAP(A)
1.
2.
3.
n = length[A]
4
2
14
A:
do MAX-HEAPIFY(A, i, n)
1
3
5
16 9
10
16
10 14
10
7
11
Example:
14
14
16 9
2
7
10 8
14
10
16 9
2
7
10 8
14
3
5
10
i=1
1
16
16 9
10
2
7
4
8
14
16
9
10
10
2
7
4
8
16 9
i=2
5
10
10 14
i=3
5
10
16
i=4
i=5
1
10
14
9
10
10
3
12
n = length[A]
2.
3.
do MAX-HEAPIFY(A, i, n)
O(lgn)
O(n)
13
Height
T (n) ni hi 2i h i O(n)
i 0
i 0
Level
No. of nodes
h0 = 3 ( lgn )
i=0
20
h1 = 2
i=1
21
h2 = 1
i=2
22
h3 = 0
i = 3 ( lgn )
23
14
T (n) ni hi
i 0
h
2i h i
i 0
hi
h i 2 h
i 0 2
h
k
h
2 k
k 0 2
h
k
k
2
k 0
Change variables: k = h - i
O(n)
15
Heapsort
Goal:
Sort an array using heap representations
Idea:
Build a max-heap from the array
Swap the root (the maximum element) with the last
element in the array
Discard this last node by decreasing the heap size
Call MAX-HEAPIFY on the new root
Repeat this process until only one node remains
16
Example:
MAX-HEAPIFY(A, 1, 4)
A=[7, 4, 3, 1, 2]
MAX-HEAPIFY(A, 1, 3)
MAX-HEAPIFY(A, 1, 2)
MAX-HEAPIFY(A, 1, 1)
17
Alg: HEAPSORT(A)
1. BUILD-MAX-HEAP(A)
O(n)
n-1 times
O(lgn)
Operations
on Priority Queues
Max-priority queues support the following
operations:
INSERT(S, x): inserts element x into set S
EXTRACT-MAX(S): removes and returns element of
S with largest key
MAXIMUM(S): returns element of S with largest key
INCREASE-KEY(S, x, k): increases value of element
xs key to k (Assume k xs current key value)
19
HEAP-MAXIMUM
Goal:
Return the largest element of the heap
Alg: HEAP-MAXIMUM(A)
1.
return A[1]
Heap A:
Heap-Maximum(A) returns 7
20
HEAP-EXTRACT-MAX
Goal:
Extract the largest element of the heap (i.e., return the max
value and also remove that element from the heap
Idea:
Heap A:
21
Example: HEAP-EXTRACT-MAX
16
14
7
4
max = 16
10
8
2
14
10
14
Call MAX-HEAPIFY(A, 1, n-1)
8
4
2
10
7
1
22
HEAP-EXTRACT-MAX
Alg: HEAP-EXTRACT-MAX(A, n)
1. if n < 1
2.
3.
max A[1]
4.
A[1] A[n]
5.
MAX-HEAPIFY(A, 1, n-1)
6.
return max
remakes heap
HEAP-INCREASE-KEY
Goal:
Increases the key of an element i in the heap
Idea:
Increment the key of A[i] to its new value
If the max-heap property does not hold anymore:
traverse a path toward the root to find the proper
place for the newly increased key
16
14
8
Key [i] 15 2
i
4
10
7
1
24
Example: HEAP-INCREASE-KEY
16
14
8
2
i
4
16
10
14
3
8
2
10
i
15 1
Key [i ] 15
16
i
15
2
14
i
15
10
7
16
10
14
2
7
8
1
25
HEAP-INCREASE-KEY
Alg: HEAP-INCREASE-KEY(A, i, key)
1.
2.
3.
4.
5.
6.
i
4
10
3
Key [i] 15
26
MAX-HEAP-INSERT
Goal:
16
Idea:
Expand the max-heap with a new
element whose key is -
Calls HEAP-INCREASE-KEY to
set the key of the new node to its
correct value and maintain the
max-heap property
14
10
8
2
7
4
-
16
14
10
8
2
7
4
15
27
Example: MAX-HEAP-INSERT
Insert value 15:
- Start by inserting -
16
14
8
2
7
4
14
10
3
2
10
7
15
16
14
8
2
15 9
4
15
10
10
3
2
14 9
4
7
28
MAX-HEAP-INSERT
Alg: MAX-HEAP-INSERT(A, key, n)
14
1. heap-size[A] n + 1
2. A[n + 1] -
16
10
8
2
7
4
3. HEAP-INCREASE-KEY(A, n + 1, key)
Running time: O(lgn)
29
Summary
We can perform the following operations on
heaps:
MAX-HEAPIFY
O(lgn)
BUILD-MAX-HEAP
O(n)
HEAP-SORT
O(nlgn)
MAX-HEAP-INSERT
O(lgn)
HEAP-EXTRACT-MAX
O(lgn)
HEAP-INCREASE-KEY
O(lgn)
HEAP-MAXIMUM
O(1)
Average
O(lgn)
30
31
Example:
14
14
16 9
2
7
10 8
14
10
16 9
2
7
10 8
14
3
5
10
i=1
1
16
16 9
10
2
7
4
8
14
16
9
10
10
2
7
4
8
16 9
i=2
5
10
10 14
i=3
5
10
16
i=4
i=5
1
10
14
9
10
10
3
32
LOOP INVARIANT
33
Alg: BUILD-MAX-HEAP(A)
1.
n = length[A]
2.
3.
do MAX-HEAPIFY(A, i, n)
34
Example:
14
14
16 9
2
7
10 8
14
10
16 9
2
7
10 8
14
3
5
10
i=1
1
16
16 9
10
2
7
4
8
14
16
9
10
10
2
7
4
8
16 9
i=2
5
10
10 14
i=3
5
10
16
i=4
i=5
1
10
14
9
10
10
3
35
Mas
magaling
ako!
36
Example:
MAX-HEAPIFY(A, 1, 4)
A=[7, 4, 3, 1, 2]
Im on the
top of the
MAX-HEAPIFY(A, 1, 3)
world
MAX-HEAPIFY(A, 1, 2)
MAX-HEAPIFY(A, 1, 1)
37