Professional Documents
Culture Documents
CLRS6
CLRS6
lgorithms
Heaps
Heap Sort
Priority Queues
Heap Sort
INTRODUCTION
In this chapter we introduce another
sorting algorithm which combines
the better attributes of insertion
sort and merge sort algorithms.
Heap sort also introduces another
algorithm desingn thechnique: the
use of a data structure called heap,
to manage information during the
execution of the algorithm.
HEAPS
1 2 3 4 5 6 7 8 9 10
16 14 10 8 7 9 3 2 4 1
HEAPS
An array A that represents a heap is
an object with two attributes:
• Parent(i): return i / 2
• Left(i): return 2i.
• Right(i): return 2i +1.
HEAPS
There are two kinds of binary heaps:
Max-heaps and min-heaps.
In both kinds, the values in the nodes
satisfy a heap property, the specifics
of which depend on the kind of heap.
HEAPS
In a max-heap, the max- heap property
is that for every node i other than the Root,
A[Parent(i)] A [i ]
That is, the value of a node is at most the
value of its parent. Thus the largest element
in a max-heap is stored at the root, and the
Subtree rooted at a node contains values no
larger than the contained at the node itself.
HEAPS
A min-heap is organized in the opposite way;
The min-heap property is that for every node
i other than the root,
A[Parent(i)] A [i ]
The smallest element in a min-heap is at the
root.
For the heapsort algorithm, we use
max-heaps.
HEAP PROPERTIES
Viewing a heap as a tree, we define
the height of a node in a heap to be
the number of edges on the longest
simple downward path from the node
to a leaf, and we define the height of
the heap to be the height of its root.
Since a heap of n elements is based on
a complete binary tree , its height is
(lg n).
HEIGHT
1 Height
16 3
2 3
14 10 2
4 5 6 7
8 7 9 3 1
8 9 10
2 4 1 0
BASIC PROCEDURES
Here we present five basic procedures and
shows how they are used in a sorting
Algorithm and a priority-queue data structure
• Max-Heapify.
• Build-Max-Heap.
• HEAPSORT.
• Max-Heap-Insert, Heap-Extract-Max,
Heap-increase-key, and Heap-Maximum.
MAINTAINING THE
HEAP PROPERTY
MAX-HEAPIFY
Max-Heapify is an important subroutine for
Manipulating max-heaps. Its inputs are an
array A and an index i into the array.
When Max-Heapify is called, it is assumed
that the binary trees rooted at LEFT(i) and
RIGHT(i) are max-heaps, but that A[i] may be
smaller than its children, thus violating
the max-heap property. The function of
Max-Heapify is to let the value at A[i] “float
down” in the max-heap so that the subtree
rooted at index i becomes a max-heap.
MAX-HEAPIFY
Max-Heapify(A,i)
1 l LEFT(A,i)
2 r RIGHT(i)
3 If l heap-size[A] and A[l] A[i]
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 Max-Heapify(A,largest)
MAX-HEAPIFY(A,2)
11
16
16
22 33
14
4
14 10
10
44 55 66 77
14
48
4 77 99 33
88 99 10
10
22 84
8 11
MAX-HEAPIFY
RUNNING TIME
The running time of Max-Heapify on a
Subtree of size n rooted at given node
i is the (1) time to fix up the
relationships among the elements 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.
MAX-HEAPIFY
RUNNING TIME
The children´s subtrees each have size at
most 2n/3 –-the worst case occurs when the
last row of the tree is exactly half full--- and
the running time of Max-Heapify can be
described by the recurrence:
lg n n n lg n h
h 0 2 h 1 O(h) h 0 h
=
2 2
BUILD-MAX-HEAP
RUNNING TIME
Where,
h 1/ 2
h0 2h 1 1 / 22 2
n lg n h n
h 0 h 2 (n)
2 2 2
THE HEAPSORT
ALGORITHM
THE HEAPSORT
ALGORITHM
The heapsort algorithm starts by using
Build-MaxHeap to build a max-heap on
the imput array A[1..n], where
n = length[A]. Since the maximum
element of the array is stored at the
root A[1], it can be put into its correct
final position by exchanging it with
A[n].
THE HEAPSORT
ALGORITHM
If we now “discard” node n from the
heap ( by decrementing heap-size[A] ),
we observe that A[1..(n-1)] can easily
be made into max-heap. The children
of the root remain max-heaps, but the
new root element may violate the max-
heap property. All that is needed to restore
the max-heap property, however, is one call
to Max-Heapify(A,1), which leaves a max-
heap in A[1..(n-1)].
THE HEAPSORT
ALGORITHM
The heapsort algorithm then repeats
this process for the max-heap of size
n-1 down to a heap of size 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 Max-Heapify(A,1)
HEAPSORT RUNNING
TIME
The heapsort procedure takes time
(n lgn), since the call to Build-Max-
Heap takes time (n) and each of the
n –1 calls to Max-Heapify takes time
(lg n).
Heapsort Applet
Taken from
http://www2.hawaii.edu/~copley/665/
PRIORITY QUEUES
PRIORITY QUEUES
Now we present one of the most
popular applications of a heap : its use
as an efficient priority queue. As with
heaps, there are two kinds of priority
queues: max-priority queues and min-
priority queues. We will focus here on
how to implement max-priority queues,
which are in turn based on max-heaps.
PRIORITY QUEUES
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 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
x´s key to the new value k,
which is assumed to be at least
as large as x´s current key value.
HEAP MAXIMUM
The procedure Heap-Maximum
implements the Maximum operation
in (1) time.
Heap-Maximum(A)
Return A[1];
HEAP-EXTRACT-MAX
The procedure Heap-Extract-Max implements
the extract-Max operation.
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 Max-Heapify(A,1)
7 return max