18.2.堆排序
\(18.2\)Heap Sort
1.The step of Heap Sorting
The simplest method to implement sorting via heap is to put the whole array into a heap, and pick out top of the heap one by one.
However, this method will lead to unnecessarily memory waste, for we can do heap sorting in place without creating a new heap.
2.Step impletation
\(a.\)Basic Heap operation
1 | private static void sink(Comparable[] pq, int k, int n) { |
\(b.\)Heap Sorting
The in place heap sorting use the following step: 1. Sink the element in the array one by one, so that the array forms a heap.
- Pop the front element one by one, and move the popped element to the back of the array.
1 | public static void sort(Comparable[] pq) { |