Upload
christopher-perry
View
235
Download
1
Embed Size (px)
Citation preview
HeapsortHeapsort
Lecture 4
Asst. Prof. Dr. İlker Kocabaş
Sorting AlgorithmsSorting Algorithms
• Insertion sort• Merge sort• Quicksort• Heapsort• Counting sort• Radix sort• Bucket sort• Order statistics
HeapsortHeapsort
• The binary heap data structure is an array object that can be viewed as a complete binary tree. Each node of the tree corresponds to an element of the array that stores the value in the node.
• An array A[1 ... n] that represents a heap is an object with two attributes:
length[A] : The number of elements in the array
heap-size[A] : The number of elements in the heap stored within the array A
heap-size[A]≤ length[A].
HeapsHeaps
• The root of the tree is A[1].• Given the index i of a node, the indices of its parent and children can be computed as
PARENT(i) return
LEFT(i) return 2i
RIGHT(i) return 2i+1
2/i
Heap propertyHeap property
• Two kinds of heap: Maximum heap and minimum heap.•Max-heap property is that for every node i other than the root
•Min-heap property is that for every node i other than the root
][)]([ iAiPARENTA
][)]([ iAiPARENTA
Heap propertyHeap property
In both kinds, the values in the nodes satisfy the corresponding property (max-heap and min-heap properties)
Heap propertyHeap property
16
10
59
1 43
78
4
14
109
5
23
1
6 7
8
A max-heap viewed as a binary tree
4
11
4
Heap propertyHeap property
• We need to be precise in specifying whether we need a max-heap or a min-heap.– For example min-heaps are commonly used in priority queues.
• 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.
• We define the height of the heap to be the height of its root.
• The height of a heap is Ω(lg n).
Heap propertyHeap property
In this chapter we will consider the following basic procedures:
• The MAX-HEAPIFY which runs in O(lg n), is the key to maintaining the max-heap.
• The BUILD-MAX-HEAP which runs in O(n), produces a max heap from unordered input array.
• The HEAP-SORT which runs in O(nlg n), sorts an array in place.
• Procedures which allow the heap data structure to be used as priority queue.o MAX-HEAP-INSERTo HEAP-EXTRACT-MAXo HEAP-INCREASE-KEYo HEAP-MAXIMUM
Maintaining the heap propertyMaintaining the heap property
MAX-HEAPIFY is an important subroutine for manipulating max-heaps.
When MAX-HEAPIFY(A,i) is called it forces the value A[i] “float down” in the max heap so that the the subtree rooted at index i becomes a max heap.
Maintaining the heap propertyMaintaining the heap property
),(HEAPIFY MAX 10
][][ exchange 9
8
r 7
][][ and ][_ 6
5
4
][][ and ][_ 3
)RIGHT( 2
)LEFT( 1
),(HEAPIFYMAX
tlargesA
largestAiA
ilargest
largest
largestArAAsizeheapr
ilargest
llargest
iAlAAsizeheapl
ir
il
iA
then
if
then
if
else
then
if
The action of MAX-HEAPY(The action of MAX-HEAPY(AA, 2) , 2) with with heap-size[A]=heap-size[A]=1010
The Running Time of MAX-HEAPIFYThe Running Time of MAX-HEAPIFY
The running time of MAX-HEAPIFY
• Fix up the relationships among the parent and children
• Calling for MAX-HEAPIFY : 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)
)1(
orem)master the of 2 case(by )(lg)(
)1()3/2()(
nOnT
nTnT
Building a heap Building a heap
We note that the elements in the subarray
are all leaves of the tree. Therefore the procedure BUILD-MAX-HEAP goes through the remaining nodes of the tree and runs MAX-HEAPIFY on each one
],1),2[( nn/A
Building a heap Building a heap
)Y(MAX_HEAPIF 3
1 2][ 2
1
HEAPBUILD_MAX_
A,i
/Alengthi
]A[length]A[size_heap
)A(
do
downtofor
Building a Building a Heap(contnd.) Heap(contnd.)
Buiding a Heap (contd.)Buiding a Heap (contd.)
Running time:
• Each call to MAX-HEAPIFY costs O(lg n) time,
• There are O(n) such calls.
• The running time is
(not asymptotically tight.))lg()( nnOnT
Buiding a Heap (contd.)Buiding a Heap (contd.)
Asymptotically tight bound:
We note that heights of most nodes are small.
An n-element heap has height lg n
At most n/2h+1 nodes of any height h.
Buiding a Heap (contd.)Buiding a Heap (contd.)
16
10
59
1 43
78
4
14
109
5
23
1
6 7
8
Maximum number of nodes
at height h (n=11)
4
11
4
h Actual # nodes
0 6 6
1 3 2
2 2 2
3 1 1
12/
nodes #max hn
Buiding a Heap (contd.)Buiding a Heap (contd.)
Asymptotically tight bound (cont.)
Time required by MAX_HEAPIFY
when called on a node of height h is O(h)
Buiding a Heap (contd.)Buiding a Heap (contd.)
)
2()(
2)(
lg
0
lg
01
n
hh
n
hh
hnOhO
nnT
) 1||for ()1(
1
1
20
1
0
xx
xkx
x
xx
k
k
nn
k
k
02
0 )2/11(
2/1)2/1(
have we2/1For
k
k
k
k kkx
x
Buiding a Heap (contd.)Buiding a Heap (contd.)
)()
2()
2()(
as bounded becan
HEAP-MAX-BUILD of timerunning theThus,
0
lg
0nO
hnO
hnOnT
hh
n
hh
The heapsort algorithmThe heapsort algorithm
)1,HEAPIFY(-MAX 5
1-][ ][ 4
][]1[ exchange 3
2 ][ 2
HEAP(A)-MAX-BUILD 1
)(HEAPSORT
A
AsizeheapAsizeheap
iAA
Alengthi
A
do
downtofor
The heapsort The heapsort algorithmalgorithm
Priority queuesPriority queues • Heapsort is an excellent algorithm, but a good
implementation of quicksort usually beats it in practice.
• The heap data structure itself has an enormous utility.
• One of the most popular applications of heap: its use as an efficient priority queue.
Priority queues(Priority queues(contdcontd.).) • 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 suppports 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 x’s key to the new value k, which is assumed to be k ≥ x.
The MAXIMUM operationThe MAXIMUM operation
The procedure HEAP-MAXIMUM implements the MAXIMUM operation
HEAP-MAXIMUM(A)
1 return A[1]
T(n)=Θ(1)
The EXTRACT-MAX operationThe EXTRACT-MAX operation
HEAP-EXTRACT-MAX removes and returns the element of the array A with the largest key
HEAP-EXTRACT-MAX(A)1 if heap-size[A]<12 then error “heap underflow”3 max←A[1]4 A[1]←A[ heap-size[ A]]5 heap-size[ A]←heap-size[ A]-16 MAX-HEAPIFY(A,1)7 return max
)(lg)( nOnT
The INCREASE-KEY operationThe INCREASE-KEY operation
HEAP-INCREASE-KEY increases the value of element x’s key to the new value k which is greater than or equal to x.
HEAP-INCREASE-KEY(A, i, key)1 if key < A[i]2 then error “new key is smaller than current key”3 A[i]←key4 while i >1 and A[ PARENT(i)] < A[i]5 do exchange A[i] ↔ A[ PARENT(i)]6 i ← PARENT(i)
)(lg)( nOnT
The operation of HEAP-INCREASE-KEYThe operation of HEAP-INCREASE-KEY
Increasing the key=4 to15
The INSERT OperationThe INSERT Operation
The MAX-HEAP-INSERT implements the insert operation.
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)
)(lg)( nOnT