求讲解下递归的思路。
class Program{
static void Main(string[] args)
{
int[] a = new int[] { 3, 12, 24, 36, 55, 68, 75, 88 };
Program p = new Program();
//调用方法(数组a,查找75,起始下标0,结束下标为数组最后值的下标)
int b = p.Binsearch(a, 75, 0, a.Length - 1);
}
/// <summary>
///
/// </summary>
/// <param name="a">待查找数组,需有序排列,升降都可</param>
/// <param name="key">待查找的值</param>
/// <param name="low">数组起始下标</param>
/// <param name="high">数组结束下标</param>
/// <returns>待查找值的索引</returns>
public int Binsearch(int[] a, int key, int low, int high)
{
if (low > high || high < 0)
{
return -1;
}
int mid = (low + high) / 2;
if (key < a[mid])
{
return Binsearch(a, key, low, mid - 1);
}
else if (key > a[mid])
{
return Binsearch(a, key, mid + 1, high);
}
else
{
return mid;
}
}
}
求版主讲解下递归思路