[Reserved] heapsort (HeapSort) Java realization

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)


Java code   Collection Code
  1. public class HeapSort {  
  2.   
  3.     public static void sort(Comparable[] data) {  
  4.         // build the largest heap  
  5.         buildMaxHeap(data);  
  6.         // loop, each time the root node and the last node exchange position  
  7.         for (int i = data.length; i > 1; i--) {  
  8.             Comparable tmp = data[0];  
  9.             data[0] = data[i - 1];  
  10.             data[i - 1] = tmp;  
  11.   
  12.             1 // stack length is reduced, the final position to exclude replacement root  
  13.             maxHeapify(data, 1, i - 1);  
  14.         }  
  15.     }  
  16.   
  17.     // build a maximum heap according to the input array  
  18.     private static void buildMaxHeap(Comparable[] data) {  
  19.         for (int i = data.length / 2; i > 0; i--) {  
  20.             maxHeapify(data, i, data.length);  
  21.         }  
  22.     }  
  23.   
  24.     // heap adjusted to generate maximum heap  
  25.     private static void maxHeapify(Comparable[] data, int parentNodeIndex, int heapSize) {  
  26.         // left child node index  
  27.         int leftChildNodeIndex = parentNodeIndex * 2;  
  28.         // right child index  
  29.         int rightChildNodeIndex = parentNodeIndex * 2 + 1;  
  30.         // maximum node index  
  31.         int largestNodeIndex = parentNodeIndex;  
  32.   
  33.         // If the left child node is greater than the parent, then the left child node as the largest  
  34.         if (leftChildNodeIndex <= heapSize && data[leftChildNodeIndex - 1].compareTo(data[parentNodeIndex - 1]) > 0) {  
  35.             largestNodeIndex = leftChildNodeIndex;  
  36.         }  
  37.   
  38.         // If the right child node is greater than the maximum, the maximum should be the right child node  
  39.         if (rightChildNodeIndex <= heapSize && data[rightChildNodeIndex - 1].compareTo(data[largestNodeIndex - 1]) > 0) {  
  40.             largestNodeIndex = rightChildNodeIndex;  
  41.         }  
  42.   
  43.         // Finally, if not the largest and parent nodes, the exchange value of their  
  44.         if (largestNodeIndex != parentNodeIndex) {  
  45.             Comparable tmp = data[parentNodeIndex - 1];  
  46.             data[parentNodeIndex - 1] = data[largestNodeIndex - 1];  
  47.             data[largestNodeIndex - 1] = tmp;  
  48.   
  49.             // 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  
  50.             maxHeapify(data, largestNodeIndex, heapSize);  
  51.         }  
  52.     }  
  53.   

Reproduced in: https: //www.cnblogs.com/ericsun/p/3349566.html

Guess you like

Origin blog.csdn.net/weixin_33939843/article/details/93154983