QuickAscendingSort
Kelas Larik
package LarikSortObject;
/**
*
* @author theo
*/
public class QuickAscendingObject {
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 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;
}
}
}
Minggu, 30 Oktober 2011
Quick Sort Object Ascending
Label:
program java(netbeans)
Langganan:
Posting Komentar (Atom)
0 komentar:
Posting Komentar