{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

CS300-07_Sorting - 5 Heap Sort 12 9 5 2 4 3 7 6 8 1 11 10...

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

View Full Document Right Arrow Icon
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).
Image of page 1

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

View Full Document Right Arrow Icon
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
Image of page 2
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
Image of page 3

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

View Full Document Right Arrow Icon
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
Image of page 4
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
Image of page 5

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

View Full Document Right Arrow Icon
6 How to create a heap?
Image of page 6
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
Image of page 7

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

View Full Document Right Arrow Icon
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 !!!
Image of page 8
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 )
Image of page 9

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

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

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern