public class BinarySearch {
public static int binarySearch(int a[],int value){
int low = 0;
int high = a.length-1;
int middle;
while(low<=high){
middle = (low + high)/2;
if(a[middle]==value){
return middle;
}
if(a[middle]>value){
high = middle - 1;
}
if(a[middle]<value){
low = middle + 1;
}
}
return -1;
}
public static void main(String[] args) {
System.out.println(binarySearch(new int[]{1,2,3,4,5,6,7,8,9,}, 3));
}
}
public static int binarySearch(int a[],int value){
int low = 0;
int high = a.length-1;
int middle;
while(low<=high){
middle = (low + high)/2;
if(a[middle]==value){
return middle;
}
if(a[middle]>value){
high = middle - 1;
}
if(a[middle]<value){
low = middle + 1;
}
}
return -1;
}
public static void main(String[] args) {
System.out.println(binarySearch(new int[]{1,2,3,4,5,6,7,8,9,}, 3));
}
}