Note the final array is not yet sorted, although we know that the first element has to be the largest. But that wasn't the point. Sorting proper will begin when we remove items from the heap.![]()
You could achieve the same result by inserting elements one at a time
into the heap. But the worst case performance of that algorithm could
then be
, whereas it can be shown that the method
just described is always no worse than
. You ought not
to get too excited about this result. Sorting proper takes place by
removal of items from the heap, and that will generally be
. But it is certainly worth using the more efficient
algorithm, especially as the total code is actually shorter.