遞迴二分法
阿新 • • 發佈:2018-11-14
遞迴二分法
/// <summary>
/// 二分查詢法
/// </summary>
private int DivSearch(int num, int low, int high, int [] arr)
{
int middle = (low + high) / 2 ;
while (low <= high)
{
if (num < arr[middle])
{
return DivSearch (num, low, middle - 1, arr);
}
else if (num > arr[middle])
{
return DivSearch(num, middle + 1, high, arr);
}
else
{
return middle;
}
}
return middle;
}