public class Sorting {
private int[] a;
private int nElems;
public Sorting(int max)
{
a = new int[max];
nElems = 0;
}
public void isi(int value)
{
a[nElems] = value;
nElems++;
}
public void tampil()
{
for(int j=0; j<nElems; j++){
System.out.print(a[j]+" ");
}
System.out.println("\n");
}
public void insertionSort()
{
int in, out;
for(out=1; out<nElems; out++)
{
int temp = a[out];
in = out;
while(in>0 && a[in-1] >= temp)
{
a[in] = a[in-1];
--in;
}
a[in] = temp;
}
}
}
public class SortingApp {
public static void main(String[] args){
int jml = Integer.parseInt(JOptionPane.showInputDialog("Masukkan Jumlah Data!"));
Sorting data = new Sorting(jml);
for(int i=0;i<jml;i++){
int isi = Integer.parseInt(JOptionPane.showInputDialog("Masukkan Angka ke-"+(i+1)));
data.isi(isi);
}
System.out.println("Data belum di sorting");
data.tampil();
System.out.println("InsertionSort");
data.insertionSort();
data.tampil();
}
}
Tidak ada komentar:
Posting Komentar