public class heap {
static int [] ar={24,43,30,11,40,6,105 };
static int size=ar.length;
static int top, temp;
public static void heapify(int[] a, int f){
int l= f*2,r=l+1;
if(l<size && a[l]>a[f]){
top=l;
} else {
top=f;
}
if(r<size && a[r]>a[top]){
top=r;
}
if(top!=f){
for(int i=0;i<size;i++){
System.out.print(ar[i]+",");
}
System.out.println();
Swap(a,f,top);
heapify(a,top);
}
}
public static void main(String[] args){
heapify(ar,0);
for(int i=0;i<size;i++){
System.out.print(ar[i]+",");
}
}
private static void Swap(int[] a, int f, int top) {
temp=a[top];
a[top]=a[f];
a[f]=temp;
}
}
是我的堆代码,预期输出是105 43 40 30 11 24 6但是我得到43,30,40,11,24,6,105你能告诉我我的代码在哪里错了吗?根据算法我有代码应该工作正常