notes109

# notes109 - Alan Saunders CS 251 Notes for the Week of...

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

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

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

Unformatted text preview: Alan Saunders CS 251 Notes for the Week of February 11, 2002 Monday, February 11 Heap Sort of an Array Use the same array to store sequence, to sort, and for the heap Initially all the array is used for is to store the unsorted sequence. The heap is empty: Heap ][ Sequence ---- ][3 | 7 | 5 | 6 | 1 In the first phase of the algorithm, from left to right, construct a heap. Every time an element from the array is added to the heap, you must fix the order. The heap needs to be inverted so in phase two we can accomodate the values in the sequence from the highest to the lowest. Let's assume the sequence: 3 | 7 | 5 | 6 | 1 1. Add first element to the heap: Heap ][ Sequence -- 3 ][ 7 | 5 | 6 | 1 2. Check if heap is inverted. Heap is okay. 3. Add first element in sequence to heap: Heap ][ Sequence -- 3 | 7 ][ 5 | 6 | 1 4. Check if heap is inverted. Heap needs to be fixed. Heap ][ Sequence -- 7 | 3 ][ 5 | 6 | 1 5. Add first element in sequence to heap: Heap ][ Sequence -- 7 | 3 | 5 ][ 6 | 1 6. Check if heap is inverted. Heap is okay. 7. Add first element in sequence to heap: Heap ][ Sequence -- 7 | 3 | 5 | 6 ][ 1 8. Check if heap is inverted. Heap needs to be fixed. Heap ][ Sequence -- 7 | 6 | 5 | 3 ][ 1 9. Add first element of sequence to heap: Heap ][ Sequence -- 7 | 6 | 5 | 3 | 1 ][ 10. Check if heap is inverted. Heap is okay. Complexity of Phase 1 of Heap Sort (Building the heap): O( n log n) Phase 2 of Heap Sort From right to left we remove the maximum element in the heap and place it at the end of the heap. Swap maximum element and last element in heap, then move the last element in the heap to the sequence. 1. Swap first and last element in heap. Move last element in heap to sequence....
View Full Document

{[ snackBarMessage ]}

### Page1 / 6

notes109 - Alan Saunders CS 251 Notes for the Week of...

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

View Full Document
Ask a homework question - tutors are online