import javax.swing.JOptionPane;
public class passselection {
public static void main(String[] args) {
String nilai;
nilai=JOptionPane.showInputDialog(null,"jumlah data ?");
int n =Integer.parseInt(nilai);
int[] a=new int[n];
for(int i=0;i<=n-1;i++){
a[i]=(int)(Math.random()*10);
}
System.out.println("banyak data = "+n);
System.out.println("data sebelum di urut");
for(int i=0;i<=n-1;i++){
System.out.print(a[i]+" ");
}
System.out.println();
//select sorting
int j,k,tempx,imaks,nn=1;
for(j=n-1;j>=1;j--){
imaks=1;
for(k=0;k<=j;k++){
if(a[k]>a[imaks]){
imaks=k;
}
}
tempx=a[j];
a[j]=a[imaks];
a[imaks]=tempx;
System.out.print("langkah ke-"+(nn++)+" : ");
for(int i=0;i<=n-1;i++){
System.out.print(a[i]+" ");
}
System.out.println();
}
System.out.println("Hasil selection sort : ");
for(int i=0;i<=n-1;i++){
System.out.print(a[i]+" ");
}
}
}
public class passselection {
public static void main(String[] args) {
String nilai;
nilai=JOptionPane.showInputDialog(null,"jumlah data ?");
int n =Integer.parseInt(nilai);
int[] a=new int[n];
for(int i=0;i<=n-1;i++){
a[i]=(int)(Math.random()*10);
}
System.out.println("banyak data = "+n);
System.out.println("data sebelum di urut");
for(int i=0;i<=n-1;i++){
System.out.print(a[i]+" ");
}
System.out.println();
//select sorting
int j,k,tempx,imaks,nn=1;
for(j=n-1;j>=1;j--){
imaks=1;
for(k=0;k<=j;k++){
if(a[k]>a[imaks]){
imaks=k;
}
}
tempx=a[j];
a[j]=a[imaks];
a[imaks]=tempx;
System.out.print("langkah ke-"+(nn++)+" : ");
for(int i=0;i<=n-1;i++){
System.out.print(a[i]+" ");
}
System.out.println();
}
System.out.println("Hasil selection sort : ");
for(int i=0;i<=n-1;i++){
System.out.print(a[i]+" ");
}
}
}
0 komentar:
Posting Komentar