二叉搜索技术的实现
int Binary_Search(int *ans,int n,int k)
{
int left=0;
int right=n-1;
int mid = (left+right)/2;
while(left < right)
{
if(ans[mid] > k){right = mid;mid = ((left+right)/2);}
else if(ans[mid] < k){left=mid;mid = ((left+right)/2);}
else return mid+1;
}
return -1;
}
实现思路
为将所要查询的数据按照特定的规则一分为二,在二部分中再次分为二部分来寻找