






















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!























0 1 2 3 4 5 6 7 8 8 3 1 9 4 7 2 5 6 0 1 2 3 4 5 6 7 8 8 3 1 9 4 7 2 5 6 0 1 2 3 4 5 6 7 8 8 3 1 9 4 7 2 5 6 0 1 2 3 4 5 6 7 8 Children of index k are at index 2k+1 and 2k+ This is NOT a max-heap, as not all parents are larger than their children.
0 1 2 3 4 5 6 7 8 8 3 7 9 4 1 2 5 6 0 1 2 3 4 5 6 7 8 Consider the value at index 2 and its children. Because 1 is smaller than its children, we select the larger of the children 7 and swap it into position. Because there was a swap, we may need to check recursively to restore the heap property. Move on to the next lower index.
0 1 2 3 4 5 6 7 8 8 9 7 3 4 1 2 5 6 0 1 2 3 4 5 6 7 8 Consider the value at index 1 and its children. Because 1 is smaller than its children, we select the larger of the children 9 and swap it into position. Because there was a swap, we may need to check recursively to restore the heap property.
0 1 2 3 4 5 6 7 8 9 8 7 6 4 1 2 5 3 0 1 2 3 4 5 6 7 8 Consider the value at index 0 and its children. Because 8 is smaller than its child 9, we swap these values. Because there was a swap, we may need to check recursively to restore the heap property.
0 1 2 3 4 5 6 7 8 3 8 7 6 4 1 2 5 9 0 1 2 3 4 5 6 7 8 The array is now a heap and we can begin the sort. Begin by swapping the value at index 0 with the last unsorted value in the array. Mark the value sorted in the array. Restore the heap property.
0 1 2 3 4 5 6 7 8 8 6 7 3 4 1 2 5 9 0 1 2 3 4 5 6 7 8 Compare the value to its children and swap. Continue recursively as needed to restore the heap property.
0 1 2 3 4 5 6 7 8 8 6 7 5 4 1 2 3 9 0 1 2 3 4 5 6 7 8 Compare the value to its children and swap. Reaching the end of the recursion, we continue the sorting with the next value.
0 1 2 3 4 5 6 7 8 7 6 3 5 4 1 2 8 9 0 1 2 3 4 5 6 7 8 Compare the value to its children and swap. Continue recursively as needed to restore the heap property.
0 1 2 3 4 5 6 7 8 7 6 3 5 4 1 2 8 9 0 1 2 3 4 5 6 7 8 Compare the value to its children and swap. No swap is needed. Reaching the end of the recursion, we continue the sorting with the next value.
0 1 2 3 4 5 6 7 8 6 2 3 5 4 1 7 8 9 0 1 2 3 4 5 6 7 8 Compare the value to its children and swap. Continue recursively as needed to restore the heap property.
0 1 2 3 4 5 6 7 8 6 5 3 2 4 1 7 8 9 0 1 2 3 4 5 6 7 8 Compare the value to its children and swap. Reaching the end of the recursion, we continue the sorting with the next value.
0 1 2 3 4 5 6 7 8 5 1 3 2 4 6 7 8 9 0 1 2 3 4 5 6 7 8 Compare the value to its children and swap. Continue recursively as needed to restore the heap property.
0 1 2 3 4 5 6 7 8 5 4 3 2 1 6 7 8 9 0 1 2 3 4 5 6 7 8 Compare the value to its children and swap. Reaching the end of the recursion, we continue the sorting with the next value.