/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author Administrator
*/
public class binsearch {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
String input=JOptionPane.showInputDialog(null, "Masukkan Inputan");
String []y=input.split(",");
String z=JOptionPane.showInputDialog(null, "Cari Nilai Data");
int awal = 0;
int n=y.length;
int lihat=Integer.parseInt(z);
int akhir = n - 1;
int tengah = (awal + akhir)/2;
while( awal <= akhir ){
if ( Integer.parseInt(y[tengah]) < lihat)
awal=tengah + 1;
else if ( Integer.parseInt(y[tengah]) == lihat ) {
System.out.println("Nilai "+lihat+ " Ada di Index " + (tengah + 1));
break;
}
else
akhir = tengah - 1;
tengah = (awal + akhir)/2;
}
if ( awal > akhir ){
System.out.println("Nilai "+lihat+" Not Found");
}
}
}
Posted by 13.27 and have
0
komentar
, Published at
Tidak ada komentar:
Posting Komentar