Sort algorithm (ii): Heap sort-java implementation
First of all, the heap ordering has a whole understanding, heap sorting is an unstable sorting algorithm, its average time complexity is O (NLOGN), Space complexity O (1).
So what is a heap sort? The so-called heap sequencing is a sort algorithm that is done with the concept of a heap, which is one of the selection sorts, so it is easier to understand the heap sort by selecting sort
Here we look at the concept of the heap, in fact, the heap is the use of binary tree concept to define, for the heap is divided into large top heap and small top heap two, the following mathematical expression to accurately describe the following:
Suppose we use array array to store the heap, because the large top heap is expressed as array[i] >=array[2*i+1] && array[i] >= array[2*1+2], the small top heap is represented as Array[i] <= ARRAY[2*I+1] && array[i] <= array[2*1+2], but the value is noted that there is no size constraint between array[2*i+1] and array[2*i+2]
For the basic definition of the heap we are very clear, below we will introduce in detail the idea of heap sorting
The overall idea of heap sequencing is as follows (illustrated by the example of a large top heap)
- According to the concept of the array[0] is always the largest, so with Array[array.length-1] exchange, at this time formed a array[0] ... array[array.length-2] unordered array and array[ An ordered array of array.length-1]
- Array[0 in the form of 1] .... array[array.length-2] In an unordered array, the properties of the heap are broken, so one adjustment is needed, and the adjustment starts at array.length-1.
-
- First find array.length-1 Father node, if there are left and right children, compare to exchange, in turn, loop forward to deal with
3. Follow the steps in the cycle until the sorting is complete
The Java implementation code logic is as follows:
1 Public classHeapsort {2 3 Public Static voidMain (string[] args) {4 int[] Array =New int[]{5, 3, 6, 2, 1, 9, 4, 8, 7};5 6Heapsort Heapsort =Newheapsort ();7 Heapsort.heapsort (array);8 9 for(inti = 0; i < Array.Length; i++) {TenSystem.out.print (array[i]+ ""); One } A } - - the Public voidHeapsort (int[] Array) { - - for(inti = 0; i < Array.Length; i++) { - //Create Array[array.length-1-i] as a heap +Createmaxheap (Array, array.length-1-i); - //because of the large top heap, the maximum value is always array[0], + //Exchange Array[0] and Array[array.length-1-i] ASwap (array, 0, array.length-1-i); at } - } - - //Adjustment Piles - Private voidCreatemaxheap (int[] Array,intLastIndex) { - //Father node from the current location in for(inti = (lastIndex-1)/2; I >= 0; i--) { - //record the current location node to intK =i; + //if the condition is true, then there is a child node that determines if the properties of the heap are satisfied - while(2 * k + 1 <=LastIndex) { the //default left child for Max * intBiggerindex = 2 * k + 1; $ //Judging to see if there's a right childPanax Notoginseng if(Biggerindex <LastIndex) { - //If there is a right child and the right child is large, the default index is added 1 the if(Array[biggerindex] < Array[biggerindex + 1]) { +biggerindex++; A } the } + - //at this point in comparison with the Father node $ if(Array[k] <Array[biggerindex]) { $ Swap (array, k, biggerindex); -}Else{//if not less than the end of this cycle, the comparison of the next node - Break; the } - }Wuyi } the } - Wu //exchange of two elements - Private voidSwapint[] Data,intIintj) { About if(i = =j) { $ return; - } -Data[i] = Data[i] +Data[j]; -DATA[J] = data[i]-Data[j]; AData[i] = data[i]-Data[j]; +}
Blog description of the wrong place, please correct, I hope to bring you help, thank you.
Sort algorithm (ii): Heap sort-java implementation