Directory (?) [+] Introduction to heap sorting algorithm
A heap is an important data structure, for a complete binary tree , if the bottom is to store the data in an array, suppose an element is ordinal I (Java array starting from 0, I is 0 to n-1), if it has Zoozi tree, then Zuozi position is 2i+1, if there is a right subtree, The position of the right subtree is 2i+2, and if there is a parent node, the location of the parent node is (n-1)/2 rounding. It is divided into the maximum heap and the minimum heap, and the root node of the maximum heap is not less than that of any child node, and the minimum heap is not more than any child nodes. The so-called heap sort is to use the heap of this data structure to sort the array, we are using the largest heap. Dealing with the thought and bubbling sort, the selection sort is very similar to a layer capping, just the largest element of the selection using the largest heap. The largest element of the maximum heap must be in the No. 0 position, after building the heap, swap 0 position elements and the top can be. Heap sort is in-situ ordering (small space), and the worst running time is O (N2), which is the asymptotic optimal comparison sort algorithm. Java implementation of heap sort algorithm
Like the Java implementation of insert sort (insertsort), implement an Array tool class first. The code is as follows:[Java] View Plain copy public class arrayutils { public static void printarray (Int[] array) { system.out.print ("{"); for (int i = 0; i < array.length; i++) { system.out.print (Array[i]); if (i < array.length - 1) { system.out.print (", "); } } system.out.println ("}"); } public static void exchangeelements (INT[]&NBSP;ARRAY,&NBSP;INT&NBSP;INDEX1,&NBSP;INT&NBSP;INDEX2) { int temp = array[index1]; array[index1] = array[index2]; array[index2 ] = temp; } }
The approximate steps for heap sorting are as follows: Build the largest heap. Select the top and swap with the No. 0 position element because the exchange of step 2 May break the maximum heap nature, No. 0 is no longer the largest element, need to invoke the maxheap adjustment heap (sedimentation method), if necessary repeat step 2
The most important algorithm in heap sequencing is maxheap, this function assumes that each of the two subnodes of an element satisfies the nature of the maximum heap (the left and right subtree are the largest heap), and only if the element may violate the maximum heap property, then the element and the largest element of the left and right child nodes are found, if the element is the largest, Then the entire tree is the largest heap, the program exits, otherwise the exchange with the element and the maximum element position, continue to call the Maxheap the original largest element of the subtree. This algorithm is a typical application of partition method. The specific code is as follows:[Java] View Plain Copy public class heapsort { public static void main (String[] args) { int[] array = { 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, -1, -2, -3 }; system.out.println ("Before heap:" ); arrayutils.printarray ( Array); Heapsort (array); system.out.println ("After heap sort:"); &nbsP; arrayutils.printarray (array); } public static void heapsort (int[) array) { if (array == null | | array.length <= 1) { return; } buildmaxheap (array); for (int i = array.length - 1; i >= 1; i--) { &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSp; arrayutils.exchangeelements (array, 0, i); maxheap ( array, i, 0); } } private static void buildmaxheap (Int[] array) { if (array == null || array.length <= 1) { return; } int half = array.length / 2; for (int i = half; i >= 0; i--) { maxheap (array, array.length, i); } } private static void maxheap (Int[] array, int heapsize, int index) { int left = index * 2 + 1; int right = index * 2 + 2; int largest = index; if (Left < heapsize && array[left] > array[index]) { largest = left; } if (right < heapsize && array[right] > array[largest]) { largest = right; } if (Index != largest) { arrayutils.exchangeelements (array, index, largest); maxheap (Array, heapSize, largest); } } }