Heap Sort

public class HeapSort {
public static void main(String[] args) {
int i;
int arr[] = {1, 3, 4, 5, 2};
System.out.println(“\n HEAP SORT\n—————-\n”);
System.out.println(“Data  : 1, 3, 4, 5, 2”);
System.out.print(“NILAI SETELAH SORTING = “);
for(i=0;i<arr.length;i++){
System.out.print(” ” + arr[i]);
for(i=arr.length;i>1;i–){
fnSortHeap(arr, i-1);
}

System.out.print(“\nNILAI SEBELUM SORTING = “);
for(i=0;i<arr.length;i++){
System.out.print(” ” + arr[i]);
}
}
}

private static void fnSortHeap(int[] array, int dr) {
int i, ar;
int ab, ac, ad, akar, temp;
akar = (dr-1)/2;
for(ar=akar;ar>=0;ar–){
for(i=akar;i>=0;i–){
ab=(2*1)+1;
ac=(2*i)+2;
if((ab<=dr)&&(ac<=dr)){
if(array[ac]>=array[ab])
ad=ac;
else
ad=ab;
}else{
if(ac>dr)
ad=ab;
else
ad=ac;
}
if(array[i]<array[ad]){
temp=array[i];
array[i]=array[ad];
array[ad]=temp;
}
}
temp=array[0];
array[0]=array[dr];
array[dr]=temp;
return;
}
}

}

Berikut hasil outputnya :

array

Met Mencoba ^_^

Tinggalkan Balasan

Isikan data di bawah atau klik salah satu ikon untuk log in:

Logo WordPress.com

You are commenting using your WordPress.com account. Logout / Ubah )

Gambar Twitter

You are commenting using your Twitter account. Logout / Ubah )

Foto Facebook

You are commenting using your Facebook account. Logout / Ubah )

Foto Google+

You are commenting using your Google+ account. Logout / Ubah )

Connecting to %s