#include<iostream>
using namespace std;
int binarysearch(int a[],int n,int x);
int main()
{
int a[11],x;
for(int i=0;i<10;i++)
a[i]=i;
cout<<endl;
cin>>x;
cout<<binarysearch(a,10,x)<<endl;
return 0;
}
int binarysearch(int a[],int n,int x)
{
int j=-1,low=0,high=n-1,mid;
while((low<=high)&&(j==-1))
{
mid=(low+high)/2;
if(a[mid]==x)j=mid;
else if(a[mid]<x)low=mid+1;
else high=mid-1;
}
return j+1;
}
(算法设计技巧与分析)BinarySearch
最新推荐文章于 2021-12-04 17:07:29 发布