Sorting Data Menggunakan Metode BubbleSort

Dalam mempraktekan metode tersebut saya menggunakan java Programing.maaf  kalau penjelasannya menggunakan bahasa inggris biar gaul kata orang-orang wakawkakwakwkawkk……..

public class TestBubbleSort {

public static void main(String[] args) {

int unsortedArray[] = {10, 97, 6, 23, 0, -45, 697, -1000, 1, 0}; //Random set of numbers for example.

int i;

bubbleSort(unsortedArray, unsortedArray.length); //Pass the array to be sorted and its length.

System.out.println(“Setelah dilakukan penyortingan : “;); //Just to show you it worked. :)

for(i=0; i

System.out.print(unsortedArray[i] + ” “;);

}

}

private static void bubbleSort(int[] unsortedArray, int length) {

int temp, counter, index;

for(counter=0; counter<length-1; counter++) { //Loop once for each element in the array.

for(index=0; index<length-1-counter; index++) { //Once for each element, minus the counter.

if(unsortedArray[index] > unsortedArray[index+1]) { //Test if need a swap or not.

temp = unsortedArray[index]; //These three lines just swap the two elements:

unsortedArray[index] = unsortedArray[index+1];

unsortedArray[index+1] = temp;

}

}

}

}

}

hasil Compile :

Setelah dilakukan penyortingan :
-1000 -45 0 0 1 6 10 23 97 697 BUILD SUCCESSFUL (total time: 0 seconds)

warning !!!! simpanlah dengan TestBubbleSort.java

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