///
/// 二分查找
///
/// 數組
/// 開始索引 0
/// 結束索引
/// 要查找的對象
///
public static int BinarySearch(int[] arr ,int low, int hight,int key)
{
int mid = (low + hight) / 2;
if (low>hight)
{
return -1;
}
else
{
if (arr[mid] == key)
{
return mid;
}
else if(arr[mid] > key)
{
return BinarySearch(arr, low, mid - 1, key);
}
else
{
return BinarySearch(arr, mid + 1, hight, key);
}
}
}
int[] y = new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
int rr = BinarySearch(y, 0, y.Length - 1, 5);
Console.Write(rr);
Console.Read();