Download presentation
Presentation is loading. Please wait.
Published byMeghan Bond Modified over 9 years ago
1
Lecture 8COMPSCI.220.FS.T - 20041 Algorithm HeapSort J. W. J. Williams (1964): a special binary tree called heap 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 times the maximum item because each deletion takes the logarithmic time O(log n)
2
Lecture 8COMPSCI.220.FS.T - 20042 Complete Binary Tree: linear array representation
3
Lecture 8COMPSCI.220.FS.T - 20043 Complete Binary Tree A complete binary tree of the height h contains between 2 h and 2 h 1 1 nodes A complete binary tree with the n nodes has the height log 2 n 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
4
Lecture 8COMPSCI.220.FS.T - 20044 Binary Heap A heap consists of a complete binary tree of height h with numerical keys in the nodes The defining feature of a heap : the key of each parent node is greater than or equal to the key of any child node The root of the heap has the maximum key
5
Lecture 8COMPSCI.220.FS.T - 20045 Complete Binary Tree: linear array representation
6
Lecture 8COMPSCI.220.FS.T - 20046 Binary Heap: insert a new key Heap of k keys 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 by swapping it with the parent if the latter one is smaller than the new key
7
Lecture 8COMPSCI.220.FS.T - 20047 Binary Heap: an example of inserting a key
8
Lecture 8COMPSCI.220.FS.T - 20048 Binary Heap: delete the maximum key Heap of k keys 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 swapping with the largest child if the latter one is greater
9
Lecture 8COMPSCI.220.FS.T - 20049 Binary Heap: an example of deleting the maximum key
10
Lecture 8COMPSCI.220.FS.T - 200410 Linear Time Heap Construction n insertions take 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
11
Lecture 8COMPSCI.220.FS.T - 200411 Heapifying Recursion
12
Lecture 8COMPSCI.220.FS.T - 200412 Time to build a heap
13
Lecture 8COMPSCI.220.FS.T - 200413 Linear time heap construction: non-recursive procedure Nodes are percolated down in reverse level order When the node p is processed, its descendants will have been already processed. Leaves need not to be percolated down. Worst-case time T(h) for building a heap of height h : T(h) = 2T(h 1) + ch T(h) = O(2 h ) –Form two subheaps of height h 1 –Percolate the root down a path of length at most h
14
Lecture 8COMPSCI.220.FS.T - 200414 Time to build a heap A heap of the height h has n 2 h 1 …2 h 1 nodes so that the height of the heap with n items: h = log 2 n Thus, T(h) O(2 h ) yields the linear time T(n) O(n) Two integral relationships helping to derive the above (see Slide 12) and the like discrete formulas
15
Lecture 8COMPSCI.220.FS.T - 200415 Time to build a heap
16
Lecture 8COMPSCI.220.FS.T - 200416 Steps of HeapSort p/ip/i 1/01/0 2/12/1 3/23/2 4/34/3 5/45/4 6/56/5 7/67/6 8/78/7 9/89/8 10 / 9 a706550202912531158 HEAPIFYHEAPIFY 82 3120 9150 9170 h916570318502520152
17
Lecture 8COMPSCI.220.FS.T - 200417 Steps of HeapSort a1a1 2657031850252015 91 Restore the heap (R.h.) 702 502 H9H9 7065503182252015 a2a2 655031822520 7091 R.h. 6515 3115 2015 h8h8 65315020822515
18
Lecture 8COMPSCI.220.FS.T - 200418 Steps of HeapSort a3a3 153150208225 657091 R.h. 5015 2515 h7h7 503125208215 a4a4 31252082 50657091 R.h. 3115 2015 h6h6 3120251582
19
Lecture 8COMPSCI.220.FS.T - 200419 Steps of HeapSort a5a5 22025158 3150657091 R. h. 252 h5h5 202158 a6a6 820215 253150657091 R. h. 208 158 h4h4 201528
20
Lecture 8COMPSCI.220.FS.T - 200420 Steps of HeapSort a7a7 8152 20253150657091 R. h. 158 h3h3 82 a8a8 28 20253150657091 R. h. 82 h2h2 82 a9a9 2 81520253150657091 s o r t e d a r r a y
Similar presentations
© 2024 SlidePlayer.com Inc.
All rights reserved.