Heapsort: In-place Sorting

  • Explain and trace the in-place sorting stage of heapsort.

Each time we remove an element from the heap, it's the largest item in the underlying array. So, in sorted order, it belongs at the end of the collection. As we'll see, removing an item from the heap conveniently frees up space at the end of the underlying array, where we can put the removed item

Demo

This approach reduces the $\Omicron(n)$ auxiliary space to $\Omicron(1)$.