public static void myheapSort(int[] array){ for (int i = array.length; i >1; i--) { for (int j = (i-1)/2; j >-1; j--) { myAdjustHeap(array,j,i); } int temp=array[0]; array[0]=array[i-1]; array[i-1]=temp; } } public static void myAdjustHeap(int[] array,int i,int length){ int maxIndex=i; int leftIndex=2*i+1; int regitIndex=leftIndex+1; if(leftIndex>=length){ return; } if(array[maxIndex]<array[leftIndex]){ maxIndex=leftIndex; } if(regitIndex<length && array[maxIndex]<array[regitIndex]){ maxIndex=regitIndex; } if(maxIndex!=i){ int temp=array[i]; array[i]=array[maxIndex]; array[maxIndex]=temp; } }
来源:CSDN
作者:dalong_bamboo
链接:https://blog.csdn.net/dalong_bamboo/article/details/103800200