View
3
Download
0
Category
Preview:
Citation preview
Lecture 8 COMPSCI 220 - AP G. Gimel'farb 1
Algorithm HeapSort
• J. W. J. Williams (1964): a special binary tree calledheap to obtain an O(n log n) worst-case sorting
• Basic steps:– Convert an array into a heap in linear time O(n)
– Sort the heap in O(n log n) time by deleting n timesthe maximum item because each deletion takes thelogarithmic time O(log n)
Lecture 8 COMPSCI 220 - AP G. Gimel'farb 2
Complete Binary Tree: linear array representation
Lecture 8 COMPSCI 220 - AP G. Gimel'farb 3
Complete Binary Tree
• A complete binary tree of the height h contains between2h and 2h+1−1 nodes
• A complete binary tree with the n nodes has the heightlog2n
• Node positions are specified by the level-order traversal(the root position is 1)
• If the node is in the position p then:– the parent node is in the position p/2– the left child is in the position 2p– the right child is in the position 2p + 1
Lecture 8 COMPSCI 220 - AP G. Gimel'farb 4
Binary Heap
• A heap consists of a complete binary tree ofheight h with numerical keys in the nodes
• The defining feature of a heap:
the key of each parent node is greater thanor equal to the key of any child node
• The root of the heap has the maximum key
Lecture 8 COMPSCI 220 - AP G. Gimel'farb 5
Binary Heap: linear array representation
Lecture 8 COMPSCI 220 - AP G. Gimel'farb 6
Binary Heap: insert a new key
• Heap of k keys into a heap of k + 1 keys
• Logarithmic time O( log k ) to insert a new key:
– Create a new leaf position k + 1 in the heap
– Bubble (or percolate) the new key up byswapping it with the parent if the parent key issmaller than the new key
Lecture 8 COMPSCI 220 - AP G. Gimel'farb 7
Binary Heap:an example ofinserting a key
Lecture 8 COMPSCI 220 - AP G. Gimel'farb 8
Binary Heap:delete the maximum key
• Heap of k keys into a heap of k − 1 keys
• Logarithmic time O( log k ) to delete the root(or maximum) key:– Remove the root key– Delete the leaf position k and move its key into the
root
– Bubble (percolate) the root key down by swappingit with the largest child if that child is greater
Lecture 8 COMPSCI 220 - AP G. Gimel'farb 9
Binary Heap:an example ofdeleting themaximum key
Lecture 8 COMPSCI 220 - AP G. Gimel'farb 10
Linear Time Heap Construction
• Do not use n insertions → O(n log n) time!• Alternative O(n) procedure uses a recursively
defined heap structure:
– form recursively the left and right subheaps– percolate the root down to establish the heap order
everywhere
Lecture 8 COMPSCI 220 - AP G. Gimel'farb 11
Non-recursive Heap Building
• Nodes percolate down in reverse level order– Each node p is processed after its descendants
have been already processed– Leaves need not be percolated down
• Worst-case time T(h) to build a heap of height h:T(h) = 2T(h−1) + ch → T(h) = O(2h)
– Form two subheaps of height at most h − 1– Percolate the root down a path of length at most h
Lecture 8 COMPSCI 220 - AP G. Gimel'farb 12
Time to Build a Heap
!
T(h) = 2T(h "1) + ch
2T(h "1) = 22T(h " 2) + 2c(h "1)
... ... ...
2h"2
T(2) = 2h"1
T(1) + 2h"2
c # 2
2h"1
T(1) = 2hT(0) + 2
h"1c #1= 2
h"1c #1
!
T(h) = c " 1" 2h#1 + 2 " 2h#2 + ...+ (h # 2) " 22 + (h #1) " 21 + h " 20( )= c " 2h+1
# h #1( )
Lecture 8 COMPSCI 220 - AP G. Gimel'farb 13
Worst-case Time Complexity
• A heap of n nodes is of height h = log2n sothat 2h ≤ n ≤ 2h+1−1
• Therefore, the time for converting an array into aheap is linear: T(h) = O(2h), or T(n) = O(n)
• To sort a heap, the maximum element is deletedn times, so that the worst-case time complexityof HeapSort is O( n log n )– Each deletion takes logarithmic time O( log n)
Lecture 8 COMPSCI 220 - AP G. Gimel'farb 14
Steps of HeapSort
215202550831706591h
28
10/9
70915091
20318H
EAPIFY
15312591220506570a
9/88/7
7/66/5
5/44/3
3/22/1
1/0p/i
Lecture 8 COMPSCI 220 - AP G. Gimel'farb 15
Steps of HeapSort
15252820503165h8
15201531
91
91
1565R.h.7020252831506515a2
1520252831506570H9
250270Restore
the heap(R.h.)
1520255083170652a1
Lecture 8 COMPSCI 220 - AP G. Gimel'farb 16
Steps of HeapSort
2815252031h6
1520
91
91
1531R.h.7065502820253115a4
152820253150h7
15251550R.h.
7065252820503115a3
Lecture 8 COMPSCI 220 - AP G. Gimel'farb 17
Steps of HeapSort
821520h4
815
91
91
820R. h.7065503125152208a6
81522025h5
225R. h.7065503181525202a5
Lecture 8 COMPSCI 220 - AP G. Gimel'farb 18
Steps of HeapSort
917065503125201582a9
28h2
91
91
28R. h.7065503125201582a8
2815h3
815R. h.7065503125202158a7
s o r t e d a r r a y
Recommended