给定一个无序数组,找到其中最小的K个数

/**
* 给定一个无序数组,找到其中最小的K个数
* @author Administrator
*
*/
public class getMinKNum {
public static void main(String[] args) {

}
//利用最大堆
public static int[] getMinKNumByHeap(int[]arr ,int k){
    if(arr==null ||arr.lengtharr[i]){
            kHeap[0] = arr[i];
            heapify(kHeap, 0, k);
        }
    }
    return kHeap;
} 
//建堆的过程
public static void heapInsert(int[] heap, int value, int index){
    heap[index] = value;
    int parent = (index-1)/2;
    while(index!=0){
        if(heap[index]>heap[parent]){
            swap(heap,index,parent);
            index = parent;
        }else{
            break;
        }
    }
}
//调整的过程
public static void heapify(int[] heap, int index, int heapSize){
    int left = 2*index +1;
    int right = 2*index +2;
    int largest = index;
    while(left < heapSize){
        if(heap[index]= arr[r]){
            r--;
        }
        if(l k-1){
            index = partition(arr, start, index-1);
        }else{
            return;
        }
    }
}

}

你可能感兴趣的:(数据结构与算法)