package day04;
public class ArrTest2 {
/*
数组的二分法查找: 查找注意事项:
1.对于一个有序的数组从中间查找,如果目标值比中间的索引值大,则说明值在右边,否则值在左边,继续往下查找
*/
public static void main(String[] args) {
int [] arr = new int[] {-34,-31,1,5,8,34,67,78,98,145,234,321};
int target =146;
int index = -1;
int end = arr.length-1;
int begin = 0;
boolean isFlage =true; //找到标志位
while (begin <=end){
int mid = (end+begin)/2;
if(target==arr[mid]){
System.out.println("找到了");
isFlage =false;
break;
}else if(arr[mid] > target){
end = mid-1;
}else {
begin =mid+1;
}
}
if(isFlage){
System.out.println("没有找到哦");
}
}
}