public class HeapFindMinK {
private int[] r;
private int k;
HeapFindMinK(int[] r,int k){
this.r = r;
this.k = k;
}
public void findMinK(){
int[] s = new int[k];
for(int i = 0; i < k; i++){
s[i] = r[i];
}
buildMaxHeap(s);
for(int i = k; i < r.length;i++){
if(r[i] < s[0]){
s[0] = r[i];
maxHeapify(s, k, 0);
}
}
heapSort(s);
for(int i = 0; i < k; i++){
System.out.print(s[i]);
}
}
public void swap(int[] r,int one,int two){
int t = r[one];
r[one] = r[two];
r[two] = t;
}
public void printAll(){
for(int i = 0; i < r.length;i++){
System.out.print(r[i]);
}
System.out.println();
}
public void maxHeapify(int[] r,int curSize,int index){
int left = 2*index+1;
int right = 2*index+2;
int larger = index;
if(left<curSize&&r[larger] < r[left]) larger = left
版权声明:本文为gordon_rawe原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。