Converts the array into a max-heap, then repeatedly extracts the maximum element to the end of the array.