package textbookcode;
public class BinarySearch {
public static void main(String[] args) {
//Integer [] a = {1,2,3,4,5,6};
//String [] a = {"a","b","c","d","e","f","g"};
Double [] a = {2.0,3.0,4.0,4.3};
System.out.println(binarySearch(a,6.0));
}
public static <AnyType extends Comparable<? super AnyType>> int binarySearch(AnyType [] a,AnyType x){
int low = 0;
int high = a.length-1;
while(low<=high){
int mid = (low+high)/2;
if(a[mid].compareTo(x)<0)
low = mid+1;
else if(a[mid].compareTo(x)>0)
high = mid-1;
else
return mid;
}
return -1;
}
}