一、代码如下
//有序数组的查找
public void orderNumberHalfSearch(int[] numbers, int value) {
//int[] numbers= {1, 2, 3, 4, 5, 6};
//int value= 7;
int minIndex= 0;
int maxIndex= numbers.length - 1;
Boolean isOk= false;
//System.out.println(maxIndex);
while(minIndex <= maxIndex) {
int middleIndex= (minIndex+maxIndex)/2;
//System.out.println(middleIndex+"aa");
//数在右边
if(numbers[middleIndex] < value) {
minIndex= middleIndex + 1;
//System.out.println(minIndex);
//数在左边
}else if(numbers[middleIndex] > value) {
maxIndex= middleIndex - 1;
}
else if(numbers[middleIndex] == value) {
System.out.println("找到了该数"+ numbers[middleIndex]);
isOk= true;
break;
//System.out.println(""+ numbers[middleIndex]);
}
}
if(!isOk) {
System.out.println("该数不存在该数组中");
}
}