{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

notes111

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

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

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

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

View Full Document
|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
|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

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

View Full Document
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.
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### 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
Ask a homework question - tutors are online