//<summary>
//二叉查找
//<param name="value">数字</param>
//</summary>
//<returns>int</returns>
代码
public static int BinSearch(int[] arr,int value)
{
int upperBound, lowerBound, searchNumber;
lowerBound = 0;
upperBound = arr.Length - 1;
while (lowerBound <= upperBound)
{
searchNumber = (upperBound + lowerBound) / 2;
if (arr[searchNumber] == value)
{
return searchNumber;
}
else
{
if (value < arr[searchNumber])
{
upperBound = searchNumber - 1;
}
else
{
lowerBound = searchNumber + 1;
}
}
}
return -1;
}
{
int upperBound, lowerBound, searchNumber;
lowerBound = 0;
upperBound = arr.Length - 1;
while (lowerBound <= upperBound)
{
searchNumber = (upperBound + lowerBound) / 2;
if (arr[searchNumber] == value)
{
return searchNumber;
}
else
{
if (value < arr[searchNumber])
{
upperBound = searchNumber - 1;
}
else
{
lowerBound = searchNumber + 1;
}
}
}
return -1;
}