please help me with my heapify...java
public class heap { static int [] ar={16 ,39 ,53 ,91 ,25 ,22}; static int size=ar.length; static int top, temp; public static int [] build(int[] a,int size){ int [] c=new int[a.length]; System.arraycopy(a, 0, c, 0, a.length); for(int n=(size)/2;n>-1;n--){ heapify(c,n); } return c; } 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){ /* for(int i=0;i<size;i++){ System.out.print(ar[i]+","); } System.out.println();*/ //build(ar,0); 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; } } is my code to make a heap from an array, it just doesn't work properly and i can't figure out why
@hartnn
if you go to www.khanacademy.org you can make scripts and if something is not right or is missing it shows you what is wrong with it. I am a Junior and i was taking Computer Programming before i moved and we used Khan Academy. You have to sign up but it is free. it also shows you what it makes on the right side while your code or script is on the left side.
Join our real-time social learning platform and learn together with your friends!