二分搜索
2016-12-21 本文已影响0人
MingweiLee
Template <Class Type>
int binarySearch(Type a[] , const T&x , int left , int right , int &i , int &j)
{
int middle;
while(left <= right)
{
middle = (left + right) / 2;
if(x == a[middle])
{
i = j =middle;
return 1;
}
if(x < a[middle]) right = middle - 1;
else left = middle + 1;
}
return 0 ;
}