Heapsort (java)

Telkom University Creating The Future

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 :

output java

 

source : http://nurulcollection.wordpress.com/2013/03/03/heap-sort/

Telkom University Creating The Future

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

Skip to toolbar