折半查找的实现,实验室做的!#include<iostream>
using namespace std;
int BinSearch1(int r[],int n,int k)
{
int mid;
int low=1;int high=n;int count=0;
while(low<=high)
{
mid=(low+high)/2;
count++;
if(k<r[mid])
high=mid-1;
else
if(k>r[mid])
low=mid+1;
else
{
cout<<"比较次数是:"<<count;
return mid;
}
}
cout<<"比较次数是:"<<count;
return 0;
}