//二分法查找数组toBinary
public static int search(int[] array,int key)
{
int min = 0;
int max = array.length-1;
int mid = (min + max)/2;
while(key != array[mid])
{
if (key > array[mid])
{
min = mid+1;
}else
{
max = mid-1;
}
mid = (min + max)/2;
if (min > max)
{
return -1;
}
}
return mid;
}
|
|