5 Heapsort
5 Heapsort
Heapsort
Combines the better attributes of merge sort
and insertion sort.
» Like merge sort, but unlike insertion sort, running
time is O(n lg n).
» Like insertion sort, but unlike merge sort, sorts in
place.
Introduces an algorithm design technique
» Create data structure (heap) to manage information
during the execution of an algorithm.
The heap has other applications beside sorting.
» Priority Queues
Data Structure Binary Heap
Array viewed as a nearly complete binary tree.
» Physically – linear array.
» Logically – binary tree, filled on all levels (except lowest.)
Map from array elements to
tree nodes and vice versa
» Root – A[1]
» Left[i] – 2i
» Right[i] – 2i+1
» Parent[i] – i/2
Max-heap as a binary
tree. 26
24 20
18 17 19 13
14 17 19 13
12 4 11
Procedure MaxHeapify
MaxHeapify(A, i)
1. l left(i) Assumption:
2. r right(i) Left(i) and Right(i)
3. if l heap-size[A] and A[l] > A[i] are max-heaps.
4. then largest l
5. else largest i
6. if r heap-size[A] and A[r] > A[largest]
7. then largest r
8. if largest i
9. then exchange A[i] A[largest]
10. MaxHeapify(A, largest)
Running Time for MaxHeapify
MaxHeapify(A, i)
1. l left(i)
2. r right(i)
3. if l heap-size[A] and A[l] > A[i]
Time to fix node i and
4. then largest l its children = (1)
5. else largest i
6. if r heap-size[A] and A[r] > A[largest]
PLUS
7. then largest r
8. if largest i
Time to fix the
9. then exchange A[i] A[largest] subtree rooted at one
10. MaxHeapify(A, largest) of i’s children =
T(size of subree at
largest)
Running Time for MaxHeapify(A, n)
T(n) = T(largest) + (1)
largest 2n/3 (worst case
occurs when the last row of
tree is exactly half full)
T(n) T(2n/3) + (1)
T(n) = O(lg n)
BuildMaxHeap(A)
1. heap-size[A] length[A]
2. for i length[A]/2 downto 1
3. do MaxHeapify(A, i)
BuildMaxHeap – Example
Input Array:
24 21 23 22 36 29 30 34 28 27
Initial Heap:
(not max-heap) 24
21 23
22 36 29 30
34 28 27
BuildMaxHeap – Example
MaxHeapify(10/2 = 5)
MaxHeapify(4)
MaxHeapify(3) 24
36
MaxHeapify(2)
MaxHeapify(1)
21
34
24
36 30
23
28
34
22
24 36
27
21 29 30
23
34
22 28
24 27
21
Running Time of BuildMaxHeap
Loose upper bound:
» Cost of a MaxHeapify call No. of calls to MaxHeapify
» O(lg n) O(n) = O(nlg n)
Tighter bound:
» Cost of a call to MaxHeapify at a node depends on the height,
h, of the node – O(h).
» Height of most nodes smaller than lg n.
» Height of nodes h ranges from 0 to lg n.
» No. of nodes of height h is n/2h+1
Running Time of BuildMaxHeap
Tighter Bound for T(BuildMaxHeap)
T(BuildMaxHeap)
lg n lg n
n h
h 0 2
h 1 O ( h )
h 0 2 h
h
h
lg n
h , x 1 / 2 in (A.8)
O n h
h 0 2
h 0 2
1/ 2
h
lg n
h (1 1 / 2) 2
O n h O n h
h 0 2 h 0 2 2
O ( n)
26
24 20
18 17 19 13
12 14 11
Heapsort – Example
BuildMaxHeap(A)
1.heap-size[A] length[A]
2.for i length[A]/2 downto 1
3. do MaxHeapify(A, i)
Applying BuildMaxHeap(A)
heap-size[A] =10
i = 10/2 = 5 to 1
BuildMaxHeap(A)
BuildMaxHeap(A)
i = 10 to 2
HeapSort(A)
1. Build-Max-Heap(A)
2. for i length[A] downto 2
3. do exchange A[1] A[i]
4. heap-size[A] heap-size[A] – 1
5. MaxHeapify(A, 1)
HeapSort(A)
HeapSort(A)
HeapSort(A)
Heap-Extract-Max(A)
1. if heap-size[A] < 1
2. then error “heap underflow”
3. max A[1]
4. A[1] A[heap-size[A]]
5. heap-size[A] heap-size[A] - 1
6. MaxHeapify(A, 1)
7. return max
Heap-Insert(A, key)
1 heap-size[A] heap-size[A] + 1
2 A[heap-size[A]] –
3 Heap-Increase-Key(A, heap-size[A], key)
Example: Heap-Insert(A, 15)
Example:Heap-Increase-Key(A, 9, 15)