二分搜索技术代码:(先得排序)
template <class type>
int BinarySearch(type a[],const type& x,int n)
{
//在a[0]<=……a[n-1]中搜素x
//找到X时的返回其在数组中得位置,否则返回-1
int left=0;int right=n-1;
while(left<=right)
{
int middle=(right+left)/2;
if(x==a[middle]) return middle;
if(x>a[middle]) left=middle+1;
else right=middle-1;
}
return -1;//没找到
};
template <class type>
int BinarySearch(type a[],const type& x,int n)
{
//在a[0]<=……a[n-1]中搜素x
//找到X时的返回其在数组中得位置,否则返回-1
int left=0;int right=n-1;
while(left<=right)
{
int middle=(right+left)/2;
if(x==a[middle]) return middle;
if(x>a[middle]) left=middle+1;
else right=middle-1;
}
return -1;//没找到
};