折半查询
public int Search(int[] array, int key)
{
int low = 0;
int high = array.Length;
int middle;
while (low <= high)
{
middle=(low+high)/2;
if (key == array[middle])
return middle;
else if (key < array[middle])
high = middle - 1;
else
low = middle + 1;
}
return -1;
}
{
int low = 0;
int high = array.Length;
int middle;
while (low <= high)
{
middle=(low+high)/2;
if (key == array[middle])
return middle;
else if (key < array[middle])
high = middle - 1;
else
low = middle + 1;
}
return -1;
}