ragazzi
ho questo algoritmo scritto in java e volevo sapere come funziona, se potete spiegarmi brevemente cosa fa e come funziona e come posso testarlo..vi ringrazio in anticipo!
ps: ho già eclipse sul pc!
codice:public class heap { protected int[] A; protected int heapSize; public heap(){ A=new int[50]; heapSize=0; } public boolean insertIn(int i, int key) { if(A[i]==0 && heapSize<A.length){ A[i]=key; heapSize++; return true; } else return false; } protected void exch(int i, int t) { int temp=A[i]; A[i]=A[t]; A[t]=temp; } private int left(int i){ return (2*i); } private int right(int i){ return (2*i+1); } protected int parent(int i){ return (i/2); } public void printArray(int l){ int i; System.out.println(""); for(i=1;i<=l;i++){ System.out.print(A[i]); System.out.print(" "); } } public void maxHeapify(int i) { int max; int l = left(i); int r = right(i); if(l <= heapSize && A[l]>A[i]) max=l; else max=i; if(r<=heapSize && A[r]>A[max]) max=r; if(max!=i){ exch(i,max); maxHeapify(max); } } public void buildHeap() { int i; for(i=heapSize/2; i>0; i--) maxHeapify(i); } public void heapSort() { int i; buildHeap(); for(i=heapSize;i>=2;i--) { exch(1,i); heapSize--; maxHeapify(1); } } }

Rispondi quotando