如题,一下代码结果为什么不是-1而是角标越界,key应该是元素值啊
class ArrayTest4
{
public static void main(String[] args)
{
int[] arr ={1,2,3,4,5,6};
int index = halfSearch_1(arr,10);
System.out.println("index="+index);
}
public static int halfSearch_1(int[] arr, int key)
{
int Max=arr.length, Min=0, Mid;
while (Min<=Max)
{
Mid =(Min+Max)>>1;
if (key>arr[Mid])
Min = Mid + 1;
else if (key<arr[Mid])
Max = Mid - 1;
else
return Mid;
}
return -1;
}
} |