Unit07B - Heap Sort Sorting Poly-logarithmic Sorts 7B We...

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

View Full Document Right Arrow Icon
1 1 Sorting Poly-logarithmic Sorts 7B 2 Heap Sort We can use a max-heap to sort data. Convert an array to a max-heap. Remove the root from the heap and store it in its proper position in the same array. Repeat until all elements in the array are in sorted order. 3 Building the max-heap 83 48 61 72 95 53 39 6 5 4 3 2 1 0 39 83 48 61 72 95 39 53 6 5 4 3 2 1 0 39 53 83 48 61 72 53 39 95 6 5 4 3 2 1 0 53 39 95 ADD NEXT VALUE TO HEAP AND FIX HEAP 4 Building the max-heap (cont'd) 83 48 61 39 53 72 95 6 5 4 3 2 1 0 53 72 95 39 53 48 61 39 83 72 95 6 5 4 3 2 1 0 83 72 95 39 61 48 53 CONTINUE UNTIL THE HEAP IS COMPLETED. .. 5 Sorting from the heap 53 48 61 39 83 72 95 6 5 4 3 2 1 0 SWAP THE MAX OF THE HEAP WITH THE LAST VALUE OF THE HEAP: 95 48 61 39 83 72 53 6 5 4 3 2 1 0 53 72 83 39 61 48 95 48 61 39 53 72 83 6 5 4 3 2 1 0 FIX THE HEAP (NOT INCLUDING MAX): 83 72 95 39 61 48 53 remove max 6 Sorting from the heap (cont'd) 95 48 61 39 53 72 83 6 5 4 3 2 1 0 SWAP THE MAX OF THE HEAP WITH THE LAST VALUE OF THE HEAP: 95 83 61 39 53 72 48 6 5 4 3 2 1 0 53 61 72 39 48 95 83 48 39 53 61 72 6 5 4 3 2 1 0 FIX THE HEAP (NOT INCLUDING MAX): 53 72 83 39 61 48 remove max
Background image of page 1

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

View Full DocumentRight Arrow Icon
2 7 Sorting from the heap (cont'd) 95 83 48 39 53 61 72 6 5 4 3 2 1 0 SWAP THE MAX OF THE HEAP WITH THE LAST VALUE OF THE HEAP: 95 83 72 39 53 61 48 6 5 4 3 2 1 0 53 48 61 39 95 83 72 39 53 48 61 6 5 4 3 2 1 0 FIX THE HEAP (NOT INCLUDING THAT MAX): 53 61 72 39 48 remove max REPEAT UNTIL THE HEAP
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 4

Unit07B - Heap Sort Sorting Poly-logarithmic Sorts 7B We...

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

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