You are on page 1of 30

1

Chapter 6 Heapsort
2
Introduction
Heapsort
Running time: O(n lg n)
Like merge sort
Sort in place: only a constant number of array elements are stored
outside the input array at any time
Like insertion sort
Heap
A data structure used by Heapsort to manage information during the
execution of the algorithm
Can be used as an efficient priority queue
3
Heaps
4
Binary Heap
An array object that can be viewed as a nearly complete
binary tree (see Section B.5.3)
Each tree node corresponds to an array element that stores the
value in the tree node
The tree is completely filled on all levels except possibly the lowest,
which is filled from the left up to a point
A has two attributes
length[A]: # of elements in the array
heap-size[A]: # of elements in the heap stored within A
heap-size[A] s length[A]
No element past A[heap-size[A]] is an element of the heap
max-heap and min-heap
5
A Max-Heap
6
Length and Heap-Size
11 7
7 11
Length = 10
Heap-Size = 7
7
Heap Computation
Given the index i of a node, the indices of its parent, left
child, and right child can be computed simply:


1 2 : ) (
2 : ) (
2 / : ) (
+ i return i RIGHT
i return i LEFT
i return i PARENT

8
Heap Property
Heap property the property that the values in the node
must satisfy
Max-heap property: for every node i other than the root
A[PARENT(i)] > A[i]
The value of a node is at most the value of its parent
The largest element in a max-heap is stored at the root
The subtree rooted at a node contains values on larger than that
contained at the node itself
Min-heap property: for every node i other than the root
A[PARENT(i)] s A[i]
9
Heap Height
The height of a node in a heap is the number of edges on
the longest simple downward path from the node to a leaf
The height of a heap is the height of its root
The height of a heap of n elements is O(lg n)
10
Heap Procedures
MAX-HEAPIFY: maintain the max-heap property
O(lg n)
BUILD-MAX-HEAP: produces a max-heap from an
unordered input array
O(n)
HEAPSORT: sorts an array in place
O(n lg n)
MAX-HEAP-INSERT, HEAP-EXTRACT, HEAP-INCREASE-
KEY, HEAP-MAXIMUM: allow the heap data structure to be
used as a priority queue
O(lg n)
11
Maintaining the Heap
Property
MAX-HEAPIFY
Inputs: an array A and an index i into the array
Assume the binary tree rooted at LEFT(i) and RIGHT(i) are max-
heaps, but A[i] may be smaller than its children (violate the max-
heap property)
MAX-HEAPIFY let the value at A[i] floats down in the max-heap
12
Example of MAX-HEAPIFY
13
MAX-HEAPIFY
Extract the indices of LEFT and RIGHT
children of i
Choose the largest of A[i], A[l], A[r]
Float down A[i] recursively
14
Running time of MAX-
HEAPIFY
O(1) to find out the largest among A[i], A[LEFT(i)], and
A[RIGHT(i)]
Plus the time to run MAX-HEAPIFY on a subtree rooted at
one of the children of node i
The childrens subtrees each have size at most 2n/3 the worst
case occurs when the last row of the tree is exactly half full
T(n) s T(2n/3) + O(1)
By case 2 of the master theorem: T(n) = O(lg n)

7/11
15
Building A Heap
16
Build Max Heap
Observation: A[(n/2+1)..n] are all leaves of the tree
Each is a 1-element heap to begin with
Upper bound on the running time
O(lg n) for each call to MAX-HEAPIFY, and call n times O(n lg n)
Not tight



17
18
Loop Invariant
At the start of each iteration of the for loop of lines 2-3, each
node i+1, i+2, .., n is the root of a max-heap
Initialization: Prior to the first iteration of the loop, i = n/2. Each
node n/2+1, n/2+2,.., n is a leaf and the root of a trivial max-heap.
Maintenance: Observe that the children of node i are numbered
higher than i. By the loop invariant, therefore, they are both roots of
max-heaps. This is precisely the condition required for the call MAX-
HEAPIFY(A, i) to make node i a max-heap root. Moreover, the MAX-
HEAPIFY call preserves the property that nodes i+1, i+2, , n are all
roots of max-heaps. Decrementing i in the for loop update
reestablishes the loop invariant for the next iteration
Termination: At termination, i=0. By the loop invariant, each node 1,
2, , n is the root of a max-heap. In particular, node 1 is.
19
Cost for Build-MAX-HEAP
Heap-properties of an n-element heap
Height = lg n
At most n/2
h+1
( nodes of any height h

) ( )
2
( )
2
( ) (
2 0
lg
0
lg
0
1
n O
h
n O
h
n O h O
n
h
h
n
h
h
n
h
h
=

(
(
(


= = =
+
Ignore the constant
2
)
2
1
1 (
2
1
2
2
0
=

= h
h
h
2
0 ) 1 ( x
x
kx
h
k

=
(for |x| < 1)
20
The HeapSort Algorithm
21
Idea
Using BUILD-MAX-HEAP to build a max-heap on the input
array A[1..n], where n=length[A]
Put the maximum element, A[1], to A[n]
Then discard node n from the heap by decrementing heap-size(A)
A[2..n-1] remain max-heaps, but A[1] may violate
call MAX-HEAPIFY(A, 1) to restore the max-heap property for
A[1..n-1]
Repeat the above process from n down to 2
Cost: O(n lg n)
BUILD-MAX-HEAP: O(n)
Each of the n-1 calls to MAX-HEAPIFY takes time O(lg n)
22
Example of HeapSort
Next Slide
23
14
1
14
10
1
14
10
9
1
Example of HeapSort (Cont.)
24
Algorithm
25
Priority Queues
26
Definition
A priority queue is a data structure for maintaining a set S of
elements, each with an associated value called a key. A
max-priority queue supports the following operations:
INSERT(S, x) inserts the element x into the set S
MAXIMUM(S) returns the element of S with the largest key
EXTRACT-MAX(S) removes and returns the element of S with the
largest key
INCREASE-KEY(S, x, k) increases the value of element xs key to
the new value k, which is assumed to be at least as largest as xs
current key value
Application of max-priority queue: Job scheduling in
computer
27
HEAP-MAXIMUM and
HEAP-EXTRACT-MAX
O(1)
O(lg n)
28
HEAP-INCREASE-KEY
Steps
Update the key of A[i] to its new value
May violate the max-heap property
Traverse a path from A[i] toward the root to find a proper place for
the newly increased key
O(lg n)
29
Example of HEAP-INCREASE-KEY
30
MAX-HEAP-INSERT
O(lg n)