Created
May 17, 2017 13:04
-
-
Save theoyrus/d995070f3d785abc65d8152c518afc7d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Scanner; | |
class BinerSearchInputString | |
{ | |
public int pencarianBiner(String b[], String kunciPencarian, int low, int high) { | |
int i, middle; | |
while(low <= high) { | |
middle = (low+high)/2; | |
if(kunciPencarian.equals( b[middle] ) ) | |
return middle; | |
else if(kunciPencarian.compareTo( b[middle] ) < 0 ) | |
high = middle - 1; | |
else | |
low = middle + 1; | |
} | |
return -1; | |
} | |
public static void main(String[] args) | |
{ | |
Scanner input = new Scanner (System.in); | |
Scanner masukanstr = new Scanner (System.in); | |
BinerSearch biner = new BinerSearch(); | |
int i, hasil, jml; | |
String x; | |
System.out.print("Masukkan banyak data yang akan dimasukkan : "); | |
jml = input.nextInt (); | |
String data[] = new String[jml]; | |
for(i = 0; i < jml; i++) { | |
System.out.print("Masukkan data ke "+(i+1)+" : "); | |
data[i] = masukanstr.nextLine(); | |
} | |
// start sorting dulu | |
for ( i=0; i < data.length-1; i++){ | |
for(int h=0; h < data.length-1; h++){ | |
if( data[h+1].compareTo( data[h] )<0 ){ | |
String tmp_data = data[h+1]; | |
data[h+1] = data[h]; | |
data[h] = tmp_data; | |
} | |
} | |
} | |
//end sorting | |
//int data[] = {1,2,3,4,4,5,6,7,8,9}; | |
System.out.print("Masukkan data yang dicari : "); | |
x = input.nextLine (); | |
hasil = biner.pencarianBiner(data, x, 0, data.length-1); | |
if(hasil >= 0) | |
System.out.println("Data tersebut terdapat pada posisi ke "+(hasil+1)+" : "); | |
else | |
System.out.println( " data tidak ditemukan" ); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment