public class bubblesort {
//property
private int [] data;
//konstruktor
public bubblesort (int n){
this.data=new int[n];
}
//fungsi set data
public void SetData(int index, int value){
if (index >= 0 && index < this.data.length){
this.data[index]=value;
}
}
//fungsi print data
public void PrintData(){
String out="Data = ";
for(int a=0; a<this.data.length; a++){
out +=this.data[a]+",";
}
System.out.println(out);
}
//fungsi tukar nilai
private void Tukar(int i, int j){
int temp = this.data[i];
this.data[i]=this.data[j];
this.data[j]=temp;
}
//fungsi bubble sort
public void Sort(){
for(int a=this.data.length-1; a>0; a--){
for(int b=0; b<a; b++){
if(this.data[b]>this.data[b+1]){
Tukar(b, b+1);
}
}
}
}
public static void main(String[]args){
//buat objek
bubblesort bs=new bubblesort(10);
//input isi
bs.SetData(0, 5);
bs.SetData(1, 3);
bs.SetData(2, 7);
bs.SetData(3, 6);
bs.SetData(4, 9);
bs.SetData(5, 4);
bs.SetData(6, 8);
bs.SetData(7, 2);
bs.SetData(8, 1);
bs.SetData(9, 4);
//print
System.out.println("bubble sort");
System.out.println("-----------");
bs.PrintData();
//sort
bs.Sort();
//print
bs.PrintData();
}
}
Selasa, 24 Juni 2014
Langganan:
Posting Komentar (Atom)
0 komentar:
Posting Komentar