您的位置:首页 > 其它

二分法查找的实现 递归与非递归方法

2016-08-01 15:21 375 查看
void erfen(int a[],int low,int high,int N)
{
int cent;
while(low<=high)
{
cent=(low+high)/2;
if(a[cent]>N)                               //非递归实现方法
high=cent-1;
else if(a[cent]<N)
low=cent+1;

else
cout<<cent<<endl;
}
cout<<"not find\n"<<endl;




void erfen_digui(int a[],int low,int high,int N)
{
if(low>high)
{

cout<<"not find"<<endl;
}
int cent=(low+high)/2;
if(a[cent]==N)
cout<<cent<<endl;
else
{
if(a[cent]>N)
erfen_digui(a,low,cent-1,N);                   //递归实现方法
else
erfen_digui(a,cent+1,high,N);

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: