public class arrayTest04 {
public static void main(String[] args) {
int []arr2=new int[] {-98,-34,2,34,56,66,79,105,210,333};
int dest1=79;
int head=0;//初始的首索引
boolean isFlag1=true;
int end=arr2.length-1;
while(head<=end) {
int middle=(head+end)/2;
if(dest1==arr2[middle]) {
System.out.println("找到了指定的元素"+dest1+",位置:"+middle);
isFlag1=false;
break;
}else if(arr2[middle]>dest1) {
end=middle-1;
}else {
//arr2[middle]<dest1
head=middle+1;
}
}
if(isFlag1) {
System.out.println("没有找到");
}
}
}
Java数组二分法查找
最新推荐文章于 2024-06-22 20:15:42 发布