Heap sorting is a kind of tree-selecting sorting method, which is characterized by: in the process of sorting, the array[0,...,n-1] is considered as a sequential storage structure of a complete binary tree , using the intrinsic relationship between the parent node and the child node in the complete binary tree, Select the maximum (minimum) element of the keyword in the current unordered region.
1. If array[0,...,n-1] represents a sequential storage pattern for a fully binary tree, the intrinsic relationship between the parent node pointer and the child node pointer is as follows:
Any node pointer I: parent node: i==0? Null: (I-1)/2
Left Child: 2*i + 1
Right Child: 2*i + 2
2. Heap definition: n keyword sequence array[0,...,n-1], when and only if the following requirements are met: (0 <= I <= (n-1)/2)
①array[i] <= Array[2*i + 1] and Array[i] <= array[2*i + 2]; called small Gan;
②array[i] >= Array[2*i + 1] and Array[i] >= array[2*i + 2]; called Dagen;
3. Establish Dagen:
n nodes of the complete binary tree array[0,...,n-1], the last node n-1 is the child (n-1-1)/2 nodes. The subtree is adjusted to the root of the (n-1-1)/2 nodes, so that the subtree is called a heap.
For Dagen, the adjustment method is: If the "root node keyword" is less than "the key word in the left and right children", then the exchange.
Then forward to each node ((n-2)/2-1) ~ 0 as the root of the subtree to adjust to see if the node value is greater than the value of its left and right child nodes, if not, the left and right child nodes in the exchange of large values, the exchange may break the next heap, and then continue to use the above method to build the next heap, Until the heap is made up of a subtree that is the root of the node.
Reuse the above-mentioned adjustment heap method to build the heap until the root node.
4. Heap sequencing: (Dagen)
① The n elements stored in array[0,...,n-1] into the initial heap;
② the top element of the heap is exchanged with the base element, the maximum value of the sequence is placed in the correct position;
③ But at this point the heap is destroyed, the top element of the heap is adjusted downward so that it continues to maintain the nature of Dagen, and then the ②③ step is repeated until only one element is left in the heap.
Performance analysis of heap sorting algorithm:
Space complexity: O (1);
Time Complexity: Build heap: O (n), each adjustment O (log n), so the best, worst, average case: O (N*LOGN);
Stability: Unstable
Methods for establishing Dagen:
1//Build Dagen: Treat array as the sequential storage structure of the complete binary Tree 2 private int[] Buildmaxheap (int[] array) {3//parent node from last node Array.length-1 (a RRAY.LENGTH-1-1)/2 starts until root node 0, repeatedly adjusts heap 4 for (int i= (array.length-2)/2;i>=0;i--) {5 Adjustdowntoup (array , i,array.length); 6} 7 return array; 8} 9 10//Element array[k] from bottom to top gradually adjust tree structure one private void Adjustdowntoup (int[] array,int k,int length) {12 int temp = Array[k]; for (int i=2*k+1; i<length-1; i=2*i+1) {//i is the left child initialized to Node K, adjust the IF (I<length &&A) along the larger child nodes of the node. mp Array[i]<array[i+1]) {//Take the subscript of a node with a large sub-node of i++; If the right child of the node > the left child, then take the right child node subscript}17 if (Temp>=array[i]) {///root node >= the larger of the keywords in the child, adjust the end 18 Break;19}else{//root node < left and right children keywords greater than array[k] = array[i]; Adjust the larger value of the left and right sub-nodes Array[i] to the parent node, k = i; "Critical" modifies the K value so that it continues to adjust the}23}24 array[k] = temp; The value of the adjusted node puts the person mostFinal position 25}
Heap Sort:
1// heap sort 2 public int[] Heapsort (int[] array) {3 array = buildmaxheap (array);//initial heap, array[0] is the element with the largest first trip value 4 for (int i=array.length-1;i>1;i--) { 5 int temp = array[0]; Swap the top element of the heap with the low element of the heap, i.e. get the correct sort position of the current largest element 6 array[0] = array[i]; 7 array[i] = temp; 8 adjustdowntoup (array, 0,i); c12/>//the remaining elements in piles 9 }10 return array;11 }
Delete the top element of the heap (that is, the maximum in the sequence): first, the last element of the heap is exchanged with the top element of the heap, because the nature of the heap is destroyed and the root node at this point is adjusted downward.
1 //delete heap top element Operation 2 public int[] Deletemax (int[] array) {3 ///The last element of the heap is exchanged with the top element of the heap, the base element value is set to -999994 array[0] = Array[array.length-1];5 array[array.length-1] = -99999;6 //down adjustment to the root node at this point 7 adjustdowntoup (array, 0, Array.Length); 8 return array;9 }
Insert operations on the heap: first place the new node at the end of the heap, and then perform an upward adjustment on the new node.
Assuming that the last element of the array array[array.length-1] is empty, the newly inserted node is initially placed here.
1 //insert operation: Insert data into Dagen array 2 public int[] InsertData (int[] array, int data) {3 array[array.length-1] = Data Place the new node at the end of the heap 4 int k = array.length-1; Node to be adjusted 5 int parent = (k-1)/2; Parent Node 6 while (parent >=0 && Data>array[parent]) {7 array[k] = array[parent]; Parent node lowered by 8 k = parent, 9 if (parent! = 0) {Ten parent = (parent-1)/2; Continue to compare one by one}else{ //root node has been adjusted, jump out of the loop break;13 }14 }15 array[k] = data; Place the inserted node in the correct position. return array;17 }
Test:
1 public void toString (int[] array) {2 for (int i:array) {3 System.out.print (i+ ""); 4 } 5 } 6 7 public static void Main (String args[]) {8 heapsort hs = new Heapsort (); 9 int[] Array = {87,45,78,32,17,65,53,9,122};10 System.out.print ("Build Dagen:"); hs.tostring (Hs.buildmaxheap (array)); 12 System.out.print ("\ n" + "delete the top element of the heap:"), hs.tostring (Hs.deletemax (array)), and System.out.print ("\ n" + "Insert element:"), hs.tostring (array, hs.insertdata), System.out.print ("\ n" + "Big root heap Sort:"); Hs.tostring (Hs.heapsort (array)); (+ }
Java implements large top heap