Pages

Minggu, 30 Oktober 2011

Quick Sort Object Descending

Kelas Larik
package LarikSortObject;

/**
 *
 * @author Theo
 */
public class QuickDescendingObject {
     public static void quickSort(Object a[]) {
        quickSortHelper(a, 0, a.length - 1);
    }
   
    private static void quickSortHelper( Object x[], int awal, int akhir) {
        if (awal < akhir) {
            //langkah 1
            int i = awal + 1;
            int j = akhir;
            //langkah 2
            while (i <= akhir && (((Comparable) x[i]).compareTo(x[awal]) >= 0)){
                i = i + 1;
            }
            //langkah 3 >
            while (j > awal && (((Comparable) x[j]).compareTo(x[awal]) <= 0)) {
                j = j - 1;
            }
            //langkah 4
            while (i < j) {
                // langkah 5
                Object temp = x[i];
                x[i] = x[j];
                x[j] = temp;

                // langkah 6
                while (i <= akhir && (((Comparable)x[i]).compareTo(x[awal]) >= 0 )) {
                    i = i + 1;
                }
                // langkah 7
                while (j > awal && (((Comparable)x[j]).compareTo( x[awal]) < 0)) {
                    j = j - 1;
                }
            }
            // langkah 8
            Object temp = x[awal];
            x[awal] = x[j];
            x[j] = temp;

            // langkah 9
            quickSortHelper(x, awal, j - 1);

            // langkah 10
            quickSortHelper(x, j + 1, akhir);
        }
    }
   
}

Kelas Mahasiswa

package LarikSortObject;

/**
 *
 * @author Theo
 */
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 LarikSortObject;

/**
 *
 * @author theo
 */
public class MainObject {

    /**
     * @param args the command line arguments
     */
    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");
        }

        QuickAscendingObject.quickSort(mhs);
        System.out.println("\n\ndata mahasiswa setelah diurutkan secara quick sort ascending:");
        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());
        }
       
        QuickDescendingObject.quickSort(mhs);
        System.out.println("\n\ndata mahasiswa setelah diurutkan secara quick sort descending:");
        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