Searching – Binary Search

public class BinarySearch {
public static void main(String[] args) {
int [] c = {3, 5, 9, 11, 17, 33};
int awal = 0;
int akhir = 0;

int nt = 0;
int flag = 0;
int cari =Integer.parseInt(JOptionPane.showInputDialog(“Masukkan angka : “));

akhir = c.length-1;
while(awal<=akhir && flag == 0){
nt = Math.abs((awal+akhir)/2);
if(c[nt]==cari)
flag = 1;
else if(cari < c[nt])
akhir =nt-1;
else
awal = nt+1;
}
if(flag == 1)
System.out.println(“Data ditemukan “+”pada indeks ke ” +nt);
else
System.out.println(“Data tidak ditemukan”);
System.exit(0);
}
}

Berikut hasil outputnya :

jika data inputan ada dlm dftar maka :

array

Met Mencoba ^_^

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