The basic idea of heap sorting: to sort by establishing the maximum minimum heap according to a complete binary tree
Complexity of Time: O (NLONGN)
/** * */Package com;/** * @author Wenb * @time pm 03:52:55 * @date 2014-10-24*/ Public classHeapsort { Public Static voidMain (String args[]) {int[] A = {1,3,2, +,5, A,98, Wu}; Headsort (a); for(intI=0; i<a.length;i++) {System. out. Print (a[i]+" "); } } Public Static int[] Headsort (int[] sortarray) { for(intI=0; i<sortarray.length-1; i++) {buildmaxheap (sortarray,sortarray.length-1-i); Swap (Sortarray,0, sortarray.length-1-i); } returnSortarray; } //ways to exchange two data Public Static voidSwapint[] Data,intIintj) { inttemp =Data[i]; Data[i]=Data[j]; DATA[J]=temp; } //Build Maximum Heap Public Static voidBuildmaxheap (int[] Data,intLastIndex) { //start the ship stack from the parent node of the lastindex node for(intI= (lastindex-1)/2; i>=0; i--){ //Save the node you are judging intK =i; //This creates a large top heap for each node, as long as the root node has child nodes while((2*k+1) <=LastIndex) { //assuming the value of the left node is the largest intBiggerindex =2*k+1; //It means there's a right node. if(Biggerindex <LastIndex) { //Select the largest value in the child node if(Data[biggerindex] < data[biggerindex+1]) {Biggerindex++; } } //compare nodes to child nodes if(Data[k] <Data[biggerindex]) {swap (Data,k,biggerindex); K=Biggerindex; }Else{ Break; } } } } }
Various sorting complexities:
Specific analysis: http://blog.sina.com.cn/s/blog_771849d301010ta0.html
Sorting problem of data structure---heap ordering and the complexity of various sorting time space