Misalkan data sebagai berikut :
12 29 17 56 11 23
Berikut adalah source codenya:
public class SelectionSort {
public static void main(String[]args){
int[]data={12,29,17,56,11,23};
System.out.print("Data awal : ");
for(int i=0;i<data.length;i++){
System.out.print(data[i]+" ");
}
System.out.println();
for(int i=0;i<data.length;i++){
int tukar=i;
for(int j=i;j<data.length;j++){
if(data[tukar]>data[j]){
tukar=j;
}
}
int temp=data[tukar];
data[tukar]=data[i];
data[i]=temp;
}
System.out.print("Data terurut : ");
for(int i=0;i<data.length;i++){
System.out.print(data[i]+" ");
}
System.out.println();
}
}
Output:
Data awal : 12 29 17 56 11 23
Data terurut : 11 12 17 23 29 56
Tags:
Java