# CS300-07_Sorting - 5 Heap Sort 12 9 5 2 4 3 7 6 8 1 11 10 level 0 level 1 level 2 level 3 Defn A heap is said to be a complete binary tree with the

This preview shows pages 1–11. Sign up to view the full content.

1 12 9 11 5 7 8 10 2 4 3 6 1 level 2 level 3 level 1 level 0 5. Heap Sort Def’n : A heap is said to be a complete binary tree with the property that the value of each node is at least as large as its child nodes( if any).

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
2 n i k k i i < 2200 1 2 / i k i ( i , k i ) value, k i , i = 1,2,…, n label, i = 1,2,…, n 1 12 2 9 3 11 4 5 5 7 6 8 7 10 8 2 9 4
3 Complete Binary Tree the left child of node i : 2 i ( if 2 i n ) the right child of node i : 2 i + 1 ( if 2 i + 1 n ) i 2 i 2 i +1 What is the parent of node i ? 2 i

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
4 Basic Idea (1) Construct a heap (2) Successively remove the root and restructure the remaining tree into a heap Given a heap, how can you construct its sorted list? 49 50 47 35 39 49 33 34 23 47 42 31 27 22 28 39 37 30 32 15 10 11 13 9 7 15 19 20 21 26 23 50
5 i k i i k i i k i 1 50 49 10 22 19 13 2 35 11 28 20 9 3 49 47 12 39 23 21 7 4 33 13 37 22 15 5 34 14 30 23 19 6 47 39 15 32 24 20 7 42 16 15 25 21 8 31 17 10 26 26 9 27 18 11 27 23 50

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
6 How to create a heap?
7 Algorithm Fixing a heap procedure FixHeap(root :Node ; k :Key ); var vacant, LargerChild :Node ; begin vacant := root; while vacant is not a leaf do LargerChild := the child of vacant with the larger key; if k<LargerChild’s key then copy LargerChild’s key to vacant; vacant := LargerChild else exitloop end {if} end {while} put k in vacant end {FixHeap} 7 50 49 8 10 5 9 47 48

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
8 Lemma : FixHeap does 2 d comparisons of keys in the worst case on a heap with depth d . [proof] 2 comparisons /while loop max. d iterations O ( log n ) How can you prove the correctness of the algorithms? Exercise !!!
9 Constructing a Heap Procedure ConstructHeap ( root :Node) begin if root is not a leaf then ConstructHeap ( left child of root ); ConstructHeap ( right child of root ); FixHeap ( root , key in root ); end {if} end {if} T (1) = 0 n c n T n T log 2 2 ) ( + = M ( n )

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
10 Homework: prove that :if M ( n ) ≤ c log (n) , c >0, then T ( n ) = O( n ).
This is the end of the preview. Sign up to access the rest of the document.

## This note was uploaded on 02/04/2010 for the course COMPUTER S cs300 taught by Professor Unkown during the Spring '08 term at Korea Advanced Institute of Science and Technology.

### Page1 / 37

CS300-07_Sorting - 5 Heap Sort 12 9 5 2 4 3 7 6 8 1 11 10 level 0 level 1 level 2 level 3 Defn A heap is said to be a complete binary tree with the

This preview shows document pages 1 - 11. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online