Kelas Larik
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package search;
public class LarikInterpolation {
public static int Interpolasi(int data[], int cari) {
int indexAwal = 0;
int indexAkhir = data.length - 1;
int i;
while (indexAwal <= indexAkhir) {
i = indexAwal + ((cari - data[indexAwal]) * (indexAkhir - indexAwal) /
(data[indexAkhir] - data[indexAwal]));
if (cari > data[i]) {
indexAwal = i + 1;
} else if (cari < data[i]) {
indexAkhir = i - 1;
} else if (cari == data[i]) {
System.out.println("bilangan " + cari + " ketemu di index ke ");
return i;
}
}
System.out.println("bilangan " + cari + " tidak ketemu");
return -1;
}
}
Kelas Main
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package search;
import java.util.Arrays;
public class MainInterpolation {
public static void main(String[] args) {
int[] data = {10, 20, 30, 40, 50, 60};
Arrays.sort(data);
System.out.println(LarikInterpolation.Interpolasi(data, 1));
}
}
Minggu, 30 Oktober 2011
Langganan:
Posting Komentar (Atom)
0 komentar:
Posting Komentar