Bubble Sort Menggunakan Method

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

package bubblesort;

/**
*
* @author Angga
*/
public class bubbleSort {

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here

int unsortedArray[] = {10, 97, 6, 23, 0, -45, 697, -1000, 1, 0};
int i;

System.out.println(“Sebelum sorting, elemen-elemenya adalah :”);
for(i=0; i<unsortedArray.length; i++) { // perulangan untuk mencetak array
System.out.print(unsortedArray[i] + " "); // mencetak array sebelum disorting
}

System.out.println("\n"); // membuat baris kosong

bubbleSort(unsortedArray, unsortedArray.length);
System.out.println("Setelah sorting,elemen-elemenya menjadi : ");
for(i=0; i<unsortedArray.length; i++) {
System.out.print(unsortedArray[i] + " ");
}
System.out.println("\n"); // membuat baris kosong
}

//———————————————————————–
private static void bubbleSort(int[] unsortedArray, int length) {
int temp, counter, index;
for(counter=0; counter<length-1; counter++) {
for(index=0; index unsortedArray[index+1]) {
temp = unsortedArray[index];
unsortedArray[index] = unsortedArray[index+1];
unsortedArray[index+1] = temp;
}
}
}
}
}

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 )

Foto Google

You are commenting using your Google 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 )

Connecting to %s