/**
* 二分查找 查找数字81
*/
public class TwoFenSearch2 {
public static void main(String[] args) {
int a[] = { 3,5,11,17,21,23,28,30,32,50,64,78,81,95,101};
int target = 81;
int start = 0;
int end = a.length-1;
int result = search(start,end,target,a);
System.out.println(a[result]);
}
public static int search(int start,int end,int target,int a[]){
while(start <= end){
int mid = (start + end)/2;
if(target == a[mid]){
return mid;
}else if(target < a[mid]){
end = mid -1;
}else{
start = mid + 1;
}
}
return -1;
}
}
打印结果:
81