二分检索

int BinarySearch(int a[], const int &x, int lef, int rig)
{
if(lef <= rig)
{
int mid = (lef + rig) /2;
 
if(x == a[mid])
return mid;
else if(x > a[mid])
return BinarySearch(a, x, mid+1, rig);
else
return BinarySearch(a, x, lef, mid-1);
}
}
posted @ 2018-07-23 21:04  雪卿狂  阅读(137)  评论(0编辑  收藏  举报