递归折半查找法 c语言程序,数据结构与算法----3.1 实现递归形式的折半查找算法...

[c++]代码库#include

using namespace std;

typedef int KeyType;

struct ElemType {KeyType key;};

struct SList {ElemType *r;int length;};

int Search_Bin(SList &L,KeyType k);//有序表的折半查找算法

bool ListCreate(SList &L,int n,KeyType a[]);

int ReSea_Bin(SList L,KeyType k,int a,int b);

int ReSea_Bin(SList L,KeyType k);//递归

int main()

{

SList L; int length=11;

//KeyType k=1;

//KeyType k=67;

KeyType k=67;

KeyType a[11]={01,03,11,15,17,26,33,45,53,67,72};

ListCreate(L,length,a);

cout<

for(int j=0;j

int b=ReSea_Bin(L,k);

if(b>=0)

cout<

else

cout<

return 0;

}

int ReSea_Bin(SList L,KeyType k,int a,int b)

{

int m;

if(b

m=(a+b)/2;

if(L.r[m].key==k)return m;

if(L.r[m].key>k)return ReSea_Bin(L,k,a,m-1);

elsereturn ReSea_Bin(L,k,m+1,b);

}

int ReSea_Bin(SList L,KeyType k)

{

int a=0,b=L.length-1;

return ReSea_Bin(L,k,a,b);

}

int Search_Bin(SList &L,KeyType k)

{

int a,m,b;

a=0;b=L.length;

while(a<=b)

{

m=(a+b)/2;

if(L.r[m].key==k)return m;

else if(L.r[m].key

else b=m-1;

}

return -1;

}

bool ListCreate(SList &L,int n,KeyType a[])

{

int i;

L.r=new ElemType[n];

if(!L.r)return false;

L.length=n;

for(i=0;i

{

L.r[i].key=a[i];

}

return true;

}

694748ed64b9390909c0d88230893790.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值