您的位置:首页 > 编程语言

折半查找实现算法二(递归办法)PS:编译后有一个warning,但不影响结果,代码设计上应该还有些问题

2009-08-12 10:36 836 查看
#include<iostream>
using namespace std;
#define NIL 1000;
void main()
{ int BinarySearch(int *A,int begin,int end ,int elem);
int A[8]={1,3,5,7,8,10,12,14};
int index=BinarySearch(A,0,7,10);
cout<<index<<endl;

char f;
cin>>f;
}

int BinarySearch(int *A,int begin,int end,int elem)
{
int l=begin;
int h=end;
int mid=(l+h)/2;
if(A[mid]==elem)
return mid;
else
{
if(A[mid]<elem)
{
l=mid;
if(h-l>1)
BinarySearch(A,l,h,elem);
else
return NIL;
}
else
{
h=mid;
if(h-l>1)
BinarySearch(A,l,h,elem);
else
return NIL;
}
}

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