notes111 - Heap sort of an Array -We use the same array to...

Info iconThis preview shows pages 1–5. Sign up to view the full content.

View Full Document Right Arrow Icon
Heap sort of an Array -We use the same array to store sequence to sort and the heap. -Initially all the array is used to store unsorted sequence. The heap is empty. |3|7|5|6|1| ^H - In the first phase of the algorithm, from left to right construct a heap. The heap needs to be an inverted heap so in phase 2 we accommodate the values in the sequence to the highest to lowest. |3|7|5|6|1| ^H |3|7|5|6|1| ^H NEED TO USE UP-HEAP FIX |7|3|5|6|1| ^H |7|3|5|6|1| ^H |7|3|5|6|1| ^H
Background image of page 1

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

View Full DocumentRight Arrow Icon
|7|6|5|3|1| ^H |7|6|5|3|1| ^H Complexity of phase 1 O(n log 2 n) (This is the # of elements times the # of swaps (height of the tree)) Phase Two - From right to left we remove maximum element in the heap and place it at the end of the heap and place it at the end of the array. |1|6|5|3|7| ^H NEED TO USE DOWN-HEAP FIX |6|1|5|3|7| ^H
Background image of page 2
|6|3|5|1|7| ^H |1|3|5|6|7| ^H |5|3|1|6|7| ^H |1|3|5|6|7| ^H |3|1|5|6|7| ^H |1|3|5|6|7| ^H |1|3|5|6|7| ^H Finished - The heap implementation we saw assumes that the root is in node #1
Background image of page 3

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

View Full DocumentRight Arrow Icon
1 2 3 4 5 |7|6|5|3|1| In C, C++, Java arrays start at zero. Solution when implementing algorithm C/C++/Java subtract 1 from index.
Background image of page 4
Image of page 5
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 02/02/2012 for the course CS 251 taught by Professor Staff during the Fall '08 term at Purdue University-West Lafayette.

Page1 / 7

notes111 - Heap sort of an Array -We use the same array to...

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

View Full Document Right Arrow Icon
Ask a homework question - tutors are online