The Heapsort Algorithm
- After array has been turned into a heap, repeatedly remove the root element
- Swap root element with last element of the tree and then reduce the tree length
- Removed root ends up in the last position of the array, which is no longer needed by the heap
- We can use the same array both to hold the heap (which gets shorter with each step)
and the sorted sequence (which gets longer with each step)
- Use a max-heap rather than a min-heap so that sorted sequence is accumulated in the correct order