Interpolasi Searching

public class InterpolasiSearch {
static  int [] a = {3, 9, 11, 17, 25, 29, 31};
public static void main(String[] args) {
int posisi, cari;

cari =Integer.parseInt(JOptionPane.showInputDialog(“Masukkan angka : “));
posisi = Search(cari,a.length);
if(posisi==-1){
System.out.println(“Data tidak ditemukan !!!”);
JOptionPane.showMessageDialog(null, “Data tidak ditemukan !!!”);
}else
System.out.println(“Data ditemukan pada indeks = “+posisi);
JOptionPane.showMessageDialog(null, “Data ditemukan !!!”+”\npada indeks = “+posisi);
}

private static int Search(int b, int n) {
int flag, awal = 0, akhir, nt, pos = 0;
akhir = n-1;
do{
//    nt=(int)((b-a[awal])/(a[akhir]-a[awal])*(akhir-awal)+awal);
nt=(int)((b-a[awal])/(a[akhir]-a[awal])*(akhir-awal)+awal);
if(a[nt]==b)
return nt;
else if(b<a[nt]) akhir = nt-1;
else awal =nt+1;
}
while (b>=a[awal]&&b<=a[akhir]);
return-1;
}
}

Tinggalkan Balasan

Isikan data di bawah atau klik salah satu ikon untuk log in:

Logo WordPress.com

You are commenting using your WordPress.com account. Logout / Ubah )

Gambar Twitter

You are commenting using your Twitter account. Logout / Ubah )

Foto Facebook

You are commenting using your Facebook account. Logout / Ubah )

Foto Google+

You are commenting using your Google+ account. Logout / Ubah )

Connecting to %s