您的位置:首页 > 其它

折半查找

2013-12-18 13:07 375 查看
#include <iostream>
using namespace std;
const int SIZE=50;

int main()
{
int arr[SIZE];
int no,key;
cin>>no;
for(int i=0;i<no;i++)
cin>>arr[i];
int min=0,max=no-1;
int	mid=(min+max)/2;
cin>>key;
while(key!=arr[mid])
{
if(key>arr[mid])
min=mid+1;
else
max=mid-1;
if(max<min)
{
cout<<"no find"<<endl;
break;
}
mid=(min+max)/2;
}
if(max>=min)
{
cout<<mid<<endl;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  折半查找