有序数组的二分查找
public class BinarySearch {
public static int serch(String[] s,String target) {
int lo=0;
int hi=s.length-1;
while(lo<=hi) {
int mid=(lo+hi)/2;
int cmp=s[mid].compareTo(target);
if(cmp>0) hi=mid-1;
else if(cmp<0) lo=mid+1;
else return mid;
}
return -1;
}
public static void main(String[] args) {
String[] s = { "A", "B", "C", "D", "E", "F", "G" };
System.out.println(serch(s,"B"));
}
}