堆排序

public static void adjustHeap(int[] array,int parent, int len){
    int temp = array[parent];
    int child = 2 * parent + 1;
    while(child < len){
        if(array[child] < array[child + 1]){
            child ++;
        }
        if(array[child] < temp){
            return;
        }
        array[parent] = array[child];
        parent = child;
        child *= 2 + 1;
    }
}

public static void sort(int[] array){
    int len = array.length;
    for(int i = len/2; i >= 0; i--){
        adjustHeap(array, i,len - 1);
    }

    for(int i = len - 1;i >= 0; i--){
        int temp = array[0];
        array[0] = array[i];
        array[i] = temp;
        adjustHeap(array,0,i);
    }
}
点赞