Pages

Minggu, 30 Oktober 2011

Sorting Object

Kelas Larik dengan 3 Metode
package Object;


public class Larik {

 public static void bubblesortobj(Object larik[]) {
        for (int iterasi = 1; iterasi <= larik.length - 1; iterasi++) {
            for (int i = 0; i <= larik.length - 1 - iterasi; i++) {
               if (((Comparable)larik[i]).compareTo(larik[i+1]) > 0){                 
                   Object temp = larik[i];
                   larik[i]=larik[i+1];
                   larik[i+1]=temp;                
               }
            }
        }
    }

  public static void selectionsortobj(Object larik[]){
        for (int iterasi = 0; iterasi <= larik.length - 2; iterasi++) {
            int minIndek = iterasi;
            for (int i = iterasi + 1; i <= larik.length - 1; i++) {
                if (((Comparable)larik[i]).compareTo(larik[minIndek])<0){
                    minIndek = i;
                }
                Object temp = larik[iterasi];
                larik[iterasi]=larik[minIndek];
                larik[minIndek]=temp;
            }
        }
    }

   public static void insertionsortobj(Object larik[]){
        for (int iterasi=1; iterasi <= larik.length-1; iterasi++){
            for (int i=iterasi; i >=1; i--){
                if  (((Comparable)larik[i]).compareTo(larik[i-1])<0){
                   Object temp = larik[i];
                   larik[i]=larik[i-1];
                   larik[i-1]=temp;
                 }
            }
        }
    }

}

Kelas Mahasiswa
package Object;

public class Mahasiswa implements Comparable {

    private int nim;
    private String nama;

    public int getNim() {
        return nim;
    }

    public void setNim(int nim) {
        this.nim = nim;
    }

    public String getNama() {
        return nama;
    }

    public void setNama(String nama) {
        this.nama = nama;
    }

    public int compareTo(Object o) {
       if (nim == ((Mahasiswa) o).nim) {
            return 0;
        } else if (nim < ((Mahasiswa) o).nim) {
            return -1;
        } else {
            return 1;
        }
    }
}


Kelas Main
package Object;

public class Main {

    public static void main(String[] args) {
        Mahasiswa[] mhs = new Mahasiswa[3];
        mhs[0] = new Mahasiswa();
        mhs[0].setNim(105314078);
        mhs[0].setNama("Benediktus");
        mhs[1] = new Mahasiswa();
        mhs[1].setNim(105314079);
        mhs[1].setNama("Theo");
        mhs[2] = new Mahasiswa();
        mhs[2].setNim(105314080);
        mhs[2].setNama("Kejut");
       
        System.out.println("NIM sebelum diurutkan : ");
        for (int i = 0; i < mhs.length; i++) {
            System.out.print((i+1)+". "+mhs[i].getNim()+"\n");
        }
      
        Larik.bubblesortobj(mhs);
        System.out.println("\n\ndata mahasiswa setelah diurutkan secara bublesort :");
        for (int i = 0; i < mhs.length; i++) {
            System.out.print((i+1)+". NIM       : "+mhs[i].getNim()+"\n");
            System.out.println("   NAMA      : "+mhs[i].getNama());
        }
        Larik.selectionsortobj(mhs);   
        System.out.println("\n\ndata mahasiswa setelah diurutkan secara selectionsort :");
        for (int i = 0; i < mhs.length; i++) {
            System.out.print((i+1)+". NIM       : "+mhs[i].getNim()+"\n");
            System.out.println("   NAMA      : "+mhs[i].getNama());
        }
        Larik.insertionsortobj(mhs);
         System.out.println("\n\ndata mahasiswa setelah diurutkan secara insertionsort :");
       for (int i = 0; i < mhs.length; i++) {
            System.out.print((i+1)+". NIM       : "+mhs[i].getNim()+"\n");
            System.out.println("   NAMA      : "+mhs[i].getNama());
        }
    }
}
   

0 komentar:

Posting Komentar