Heap sort of idea is to use data structures - stack . Specific implementation details:
1. Build a max-heap. For a given array of the elements A n [n-], build a max heap (heap property is the maximum value and the maximum value of a node as large as its parent node. Thus, the maximum stack storage elements in the root node; and, in a node in a subtree rooted value of each node is not greater than the value of the sub-root node). From the bottom of the sub-tree began to adjust the structure of the heap to satisfy the largest heap of features. When the stack in order to meet the maximum characteristic, the stack structure is changed, then the corresponding sub-tree recursively adjusted.
2. The heap sort algorithm, which takes out the maximum stack root (since the root node is the largest), while taking the end most leaf node as the root node, the root node from the heap adjusted so as to satisfy the maximum stack characteristic.
3. Repeat the previous step until the heap size of 2 n elements dropped.
4. gif Demo: http://upload.wikimedia.org/wikipedia/commons/4/4d/Heapsort-example.gif (from Wikipedia)
- public class HeapSort {
- public static void sort(Comparable[] data) {
- // build the largest heap
- buildMaxHeap(data);
- // loop, each time the root node and the last node exchange position
- for (int i = data.length; i > 1; i--) {
- Comparable tmp = data[0];
- data[0] = data[i - 1];
- data[i - 1] = tmp;
- 1 // stack length is reduced, the final position to exclude replacement root
- maxHeapify(data, 1, i - 1);
- }
- }
- // build a maximum heap according to the input array
- private static void buildMaxHeap(Comparable[] data) {
- for (int i = data.length / 2; i > 0; i--) {
- maxHeapify(data, i, data.length);
- }
- }
- // heap adjusted to generate maximum heap
- private static void maxHeapify(Comparable[] data, int parentNodeIndex, int heapSize) {
- // left child node index
- int leftChildNodeIndex = parentNodeIndex * 2;
- // right child index
- int rightChildNodeIndex = parentNodeIndex * 2 + 1;
- // maximum node index
- int largestNodeIndex = parentNodeIndex;
- // If the left child node is greater than the parent, then the left child node as the largest
- if (leftChildNodeIndex <= heapSize && data[leftChildNodeIndex - 1].compareTo(data[parentNodeIndex - 1]) > 0) {
- largestNodeIndex = leftChildNodeIndex;
- }
- // If the right child node is greater than the maximum, the maximum should be the right child node
- if (rightChildNodeIndex <= heapSize && data[rightChildNodeIndex - 1].compareTo(data[largestNodeIndex - 1]) > 0) {
- largestNodeIndex = rightChildNodeIndex;
- }
- // Finally, if not the largest and parent nodes, the exchange value of their
- if (largestNodeIndex != parentNodeIndex) {
- Comparable tmp = data[parentNodeIndex - 1];
- data[parentNodeIndex - 1] = data[largestNodeIndex - 1];
- data[largestNodeIndex - 1] = tmp;
- // End the exchange value of the parent and child nodes, to change the value of the child node check compliance with the maximum heap features
- maxHeapify(data, largestNodeIndex, heapSize);
- }
- }
- }
Reproduced in: https: //www.cnblogs.com/ericsun/p/3349566.html