import javax.swing.JOptionPane;
public class selectionshort {
public static void main(String[] args){
String ukuran = JOptionPane.showInputDialog(null, "Ukuran Array");
//pesan tempat untuk array ukuran n (tipe integer)
int n = Integer.parseInt(ukuran);
//input array ukuran n - random integer
int a[]=new int[n];
for (int i=0; i<=n-1; i++ )
{
a[i]=(int) (Math.random()*10);
}
//display data array
System.out.println("Banyak data = " +n);
System.out.print("Data Sebelum diurutkan = ");
for (int i=0;i<=n-1; i++){
System.out.print(a[i]+" ");
}
//mengurutkan dengan selection short
for (int selection=n-1;selection>=1;selection--)
{
int imaks =1;
{
for (int j=1;j<=selection;j++)
{
if (a[j]>a[imaks])
{
imaks=j;
}
}
int temp=a[selection];
a[selection]=a[imaks];
a[imaks]=temp;
}
}
//display data setelah urut dengan selection sort
System.out.println(" ");
System.out.println("Data Setelah Urut dengan selection sort ");
for (int selection=0;selection<=n-1;selection++ )
{
System.out.print(a[selection]+" ");
}
}}
0 komentar:
Posting Komentar