max heap sort
The core of max heap sorting is that the root node is always the maximum value of the binary tree, which introduces the first part of the algorithm: for an initial binary tree, how to maintain the property of the maximum heap - the top root node is the maximum value?
Array to build binary tree? When the parent node is i, the left and right children are 2*i and 2*i+1 respectively, which will not be repeated here;
In order to maximize the root node, it is necessary to select a maximum value from the root node and the left and right children. When the maximum value replaces the original root node, the root node moves down;
At this point, the second part of the algorithm is introduced: in order to generate the maximum root node of the current binary tree, it must be guaranteed that the left and right children should be the largest among all the child nodes of the node, so the establishment of the maximum heap should be done from the bottom up. ;
After the above two steps, a maximum heap has been successfully established at this time, so how to sort it?
At this point, back to the nature of the maximum heap: the root node is the largest, which means that the maximum value of the array has been obtained - as it should be, the maximum value is placed in the last bit of the array, and the last bit is also It is logical to put it in the first place;
At this point, all we need to do is to build the heap again, and then find the second largest value, and so on, the third largest, the fourth largest...the nth largest;
So far, the maximum heap sorting algorithm has been summarized, and the detailed steps and algorithms are shown in the following code.
1 /** 2 * Accept an array and use the maximum heap sorting algorithm to achieve sorting, the algorithm time complexity is O(nlgn) 3 * 4 * @author http://www.cnblogs.com/Xuxy1996/p/8903538.html 5 * @author xuxy 6 * 7 */ 8 public class HEAPSORT { 9 /** 10 * Maintain the property of the maximum heap - any root node is larger than the child node; algorithm premise - the child tree satisfies the maximum heap property 11 * 12 * @param array 13 * Accepted array parameter 14 * @param i 15 * Root node 16 of the current binary tree * @param size 17 * The size of the current binary tree - in the heap sorting algorithm, the maximum value is assigned to the last edge of the array, and the size needs to be reduced by one 18 */ 19 private static void MAX_HEAPIFY( int array[], int i , int size) { 20 int left = 2 * i; 21 int right = 2 * i + 1 ; 22 int max = i; 23 if (left < size && array[left] > array[i]) 24 max = left ; 25 if (right < size && array[right] >array[max]) 26 max = right; 27 if (max != i) { 28 int tmp = array[i]; 29 array[i] = array[max]; 30 array[max] = tmp; 31 MAX_HEAPIFY( array, max, size); 32 } 33 } 34 35 /** 36 * Create a maximum heap, and draw a simple judgment that larger than size/2 are all non-leaf nodes, in order to meet the precondition of maintaining the maximum heap - the child tree satisfies the maximum heap nature, so bottom-up max heap sort 37 * 38 * @param array 39 * accept sorted array 40 * @param size 41 * The size of the current array - this value will change as the algorithm progresses 42 */ 43 private static void BUILD_MAX_HEAP( int [] array, int size) { 44 for ( int i = size / 2; i >= 0; i-- ) 45 MAX_HEAPIFY(array, i, size); 46 } 47 48 /** 49 * The core of the algorithm - the implementation steps of the maximum heap sorting A value) is the maximum value. Naturally, it needs to be placed at the last edge of the array. 50 * At this time, the value of the last edge is placed in the first position. At this time, in order to meet the properties of the maximum heap, once again Proceed to build max heap 51 * 52 *@param array 53 * Array to be sorted 54 * @return Array 55 sorted by max heap sort algorithm */ 56 public static int [] HEAP_SORT( int array[]) { 57 int size = array.length; 58 for ( int i = array.length - 1; i >= 0; i-- ) { 59 BUILD_MAX_HEAP(array, size); 60 int tmp = array[0 ]; 61 array[0] = array[i]; 62 array[ i] = tmp; 63 size -= 1 ; 64 ; // Discard the original maximum value 65 } 66 return array; 67 } 68 69 /** 70 * Test method, used for basic data input and sending information, to realize the beginning of the algorithm 71 * 72 * @param args 73 */ 74 public static void main(String[] args) { 75 Scanner sc = new Scanner(System.in); 76 int n = sc.nextInt(); 77 int array[] = new int[n]; 78 for (int i = 0; i < n; i++) 79 array[i] = sc.nextInt(); 80 HEAP_SORT(array); 81 for (int i : array) 82 System.out.print(i + " "); 83 System.out.println(); 84 } 85 }
Test Data:
To be continued...