我正在尝试使用特定方法在数组中查找值。
public void findElement(int valueToFind)
{
FindElementInArray(valueToFind, 0, _array.Length - 1);
}
public void FindElementInArray(int value, int lb, int rb)
{
int currIndex = (rb - lb) / 2;
if (_array[currIndex] == value)
{
//Value found
Console.WriteLine("Value found");
return;
}
//If found value is smaller than searched value
else if (_array[currIndex] < value)
{
FindElementInArray(value, currIndex+1, rb);
}
//If found value is bigger than searched value
else
{
FindElementInArray(value, lb, currIndex - 1);
}
所以我搜索数组的中间,如果值大于我们要找的值,我们搜索左半边的中间,依此类推......
但即使它们在数组中,它也找不到一些值。有人能看出错误吗?