public static int binarySearch(int[] arr, int key)
{
int min=0;
int max = arr.length-1;
while(min<=max)
{
int mid = (min + max)/2;
if (arr[mid]<key)
min = mid +1;
else if(arr[mid ]>key)
max = mid -1;
else
return mid;
}
return -min-1;
}
当min <= max 时候,每次都会int mid ,为什么不会报错,求分析一下while的内存运行结构 |
|