静态查找

<span style="font-size: 13.3333px;">1.顺序查找</span><p style="font-size: 13.3333px;"></p><p style="margin-top: 0px; margin-bottom: 1.6842em; padding-top: 0px; padding-bottom: 0px; box-sizing: inherit; border: 0px; font-family: 'Noto Serif', serif; font-size: 19px; outline: 0px; vertical-align: baseline; color: rgb(51, 51, 51); line-height: 31.9998px;">2.折半查找</p>#include “stdafx.h”
#include “malloc.h”
#define OVERFLOW -2
#define OK 1
#define LIST_INIT_SIZE 100
#define EQ(a,b) ((a)==(b))
#define LT(a,b) ((a)<(b))
#define LQ(a,b) ((a)>(b))
typedef int KeyType;
typedef struct {
KeyType key;
}ElemType;

typedef struct{
ElemType *elem;
int length;
}SSTable;
int Init(SSTable &S){
S.elem=(ElemType *)malloc(LIST_INIT_SIZE*sizeof(ElemType));
if(!S.elem) return OVERFLOW;
S.length=0;
S.length=LIST_INIT_SIZE;
return OK;
}
int Create(SSTable &ST,int n){
for(int i=1;i<=n;i++){
scanf(“%d”,&ST.elem[i].key);
}
return OK;
}
int Search_Bin(SSTable ST,KeyType key){
int low=1,high=ST.length,mid;
while(low<=high){
mid=(low+high)/2;
if(EQ(key,ST.elem[mid].key)){
printf(“折半查找到的数所在位置是:%d\n”,mid);
return 1;
}else if(LT(key,ST.elem[mid].key))
high=mid-1;
else low=mid+1;
}
return 0;
}
int Search_Seq(SSTable ST,KeyType key){
int i;
ST.elem[0].key=key;
for(i=ST.length;!EQ(ST.elem[i].key,key);–i){
}
printf(“顺序查找得到的顺序是:%d\n”,i);
return OK;
}
int _tmain(int argc, _TCHAR* argv[])
{
int key;
SSTable ST;
Init(ST);
printf(“输入表长\n”);
scanf(“%d”,&ST.length);
printf(“输入元素\n”);
Create(ST,ST.length);
printf(“输入待查元素\n”);
scanf(“%d”,&key);
Search_Seq(ST,key);//顺序查找
Search_Bin(ST,key);//折半查找
return 0;
}


 

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值