I have an operation on a heap, a fixdown operation . This is the code:
public class Heap {
public static void fixdown (int a[],int k,int n) {
while (2*k<=n) {
int j=2*k;
if (j<n && a[j]<a[j+1]) j++;
if (!(a[k]<a[j])) break;
swap(a,k,j);
k=j;
}
}
public static void main (String[]args) {
int a[]=new int[]{12,15,20,29,23,22,17,40,26,35,19,51};
fixdown(a,1,a.length);
for (int i=0;i<a.length;i++) {
System.out.println(a[i]);
}
}
public static void swap (int a[],int i,int j) {
int t=a[i];
a[i]=a[j];
a[j]=t;
}
}
Update: I have changed it and now there is no error.
//result is
12
29
20
40
23
22
17
15
26
35
19
51