Notes 8

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

CS210: Data Structures and Algorithms 16 Aug 2013

Notes 8 Heaps [Heapsort]


We continue our discussion on heaps with heapsort and priority queues.
1 Heapsort
The heap data structure allows an easy and (worst-case) ecient sorting algorithm. It starts by
building a heap using Build-Max-Heap to build a max-heap on the input array A[1 . . . n]. This
sets A.heapsize to n. The max-heap keeps the largest element of the array in position 1. We begin
the sorting procedure by exchanging A[1] with A[n], and decrementing A.heapsize by 1 (to make it
n 1). In terms of the tree, we have taken the last leaf of the tree and exchanged it with the root,
and then, by decrementing the heapsize, eectively removed the largest element from the heap. The
heap property is satised at all nodes of the new heap A[1 . . . n 1] except possibly the root. To
x this, we call Max-Heapify(A, 1, n) and now A is a heap again, of size n 1. We continue the
process, that is, exchange A[1] with A[heapsize] and call Max-Heapify(A, 1, n1). This process is
repeated until A is a 1 element heap, in which case terminate. The procedure Heapsort presents
the pseudo-code.
Heapsort (A, n) // n is the number of elements in the heap
1. Build-Max-Heap(A, n)
2. for i = n downto2
3. exchange A[1] with A[i]
4. Max-Heapify(A, 1, i 1)
Figure 1 illustrates the procedure Heapsort on an example input.
Correctness of Heapsort
The correctness of Heapsort follows from the following loop invariant.
Loop invariant.
At the beginning of iteration with value i, the array A[i +1, . . . , n] contains the n i largest
elements of A in sorted order, and, the elements A[1, . . . , i] forms a max-heap.
We can prove the loop invariant by induction.
Base Case. The base case is when i equals n. The invariant property holds as a result of the
call to Build-Max-Heap, which transforms the input array A[1 . . . n] max-heap. The range
A[n + 1 . . . n] is empty and is therefore vacuously sorted.
1
Figure 1: Illustration of procedure Heapsort on input 16 14 10 8 7 9 3 4 1 [CLRS]
2
Induction. Suppose the statement of the invariant holds at the beginning of iteration correspond-
ing to value i + 1. Hence, the array A[i + 2, . . . , n] contains the n i 1 largest elements of
A in sorted order and the elements A[1 . . . i + 1] forms a max-heap. So, A[1] is the largest of
the elements in A[1 . . . i +1] by the heap property, and by exchanging A[1] with A[i +1], the
elements A[i + 1 . . . n] now contain the n i largest elements of A. The heap property may
be disturbed at the root, but holds at all the other nodes 2 . . . i. By calling Heapify(A, 1, i),
the heap property is restored at the root and A[1 . . . i] becomes a heap. So the loop invariant
holds at the beginning of the next iteration, i.e., corresponding to value i.
Termination. Upon termination, i = 1. By the loop invariant, A[2 . . . n] are the n 1 largest
elements of the array and in sorted order. Hence, A[1] is not larger than any other element
of the array, that is, A[1 . . . n] is sorted.
Running time analysis. We have seen that Max-Heapify procedure requires time (h) when
called at a node at height h. If there are k elements in the heap, then, h log k. In the iteration
corresponding to value i in the for loop in Heapsort, the exchange operation takes (1) time,
and the Max-Heapify operation takes time O(log(ni)). Hence, the running time is bounded by
2

i=n
O(log(n i)) nO(log n) = O(nlog n) .
3

You might also like