CS300-07_Sorting

CS300-07_Sorting - 5. Heap Sort 1 2 7 4 3 6 8 1 level 0 9 5...

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

11 1 2 9 1 1 5 7 8 1 0 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
22 i ki ( i , ki ) value, ki , 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
33 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 ?

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

View Full Document
44 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
55 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
66 How to create a heap?
77 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 va- cant; 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
88 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 1 O ( log n ) How can you prove the correctness of the algorithms? Exercise !!!
99 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 M ( n )

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

View Full Document
1010
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 10/07/2010 for the course CS 300 taught by Professor Shin during the Spring '10 term at Korea Advanced Institute of Science and Technology.

Page1 / 37

CS300-07_Sorting - 5. Heap Sort 1 2 7 4 3 6 8 1 level 0 9 5...

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

View Full Document
Ask a homework question - tutors are online