HeapSort
This algorithm has low memory usage like quicksort, with a better worst-case performance.
First we must re-arrange the array elements into a "Binary Max-Heap".
This is a type of binary tree stored in an array, meeting the following rules:
- each node is greater than or equal to both of its children
- every level of the tree is fully filled, except possibly the last level
- The last level must be filled from left to right without any gaps.
Then we repeatedly:
- take the greatest element and swap it with the last element
- repair the heap