第十三周项目1 验证算法
来源:互联网 发布:hibernate怎么修改数据 编辑:程序博客网 时间:2024/06/05 20:26
1、验证折半查找算法
#include <stdio.h>#define MAXL 100typedef int KeyType;typedef char InfoType[10];typedef struct{ KeyType key; //KeyType为关键字的数据类型 InfoType data; //其他数据} NodeType;typedef NodeType SeqList[MAXL]; //顺序表类型int BinSearch(SeqList R,int n,KeyType k){ int low=0,high=n-1,mid; while (low<=high) { mid=(low+high)/2; if (R[mid].key==k) //查找成功返回 return mid+1; if (R[mid].key>k) //继续在R[low..mid-1]中查找 high=mid-1; else low=mid+1; //继续在R[mid+1..high]中查找 } return 0;}int main(){ int i,n=10; int result; SeqList R; KeyType a[]= {1,3,9,12,32,41,45,62,75,77},x=75; for (i=0; i<n; i++) R[i].key=a[i]; result = BinSearch(R,n,x); if(result>0) printf("序列中第 %d 个是 %d\n",result, x); else printf("木有找到!\n"); return 0;}
2、验证分块查找算法
#include <stdio.h>#define MAXL 100 //数据表的最大长度#define MAXI 20 //索引表的最大长度typedef int KeyType;typedef char InfoType[10];typedef struct{ KeyType key; //KeyType为关键字的数据类型 InfoType data; //其他数据} NodeType;typedef NodeType SeqList[MAXL]; //顺序表类型typedef struct{ KeyType key; //KeyType为关键字的类型 int link; //指向对应块的起始下标} IdxType;typedef IdxType IDX[MAXI]; //索引表类型int IdxSearch(IDX I,int m,SeqList R,int n,KeyType k){ int low=0,high=m-1,mid,i; int b=n/m; //b为每块的记录个数 while (low<=high) //在索引表中进行二分查找,找到的位置存放在low中 { mid=(low+high)/2; if (I[mid].key>=k) high=mid-1; else low=mid+1; } //应在索引表的high+1块中,再在线性表中进行顺序查找 i=I[high+1].link; while (i<=I[high+1].link+b-1 && R[i].key!=k) i++; if (i<=I[high+1].link+b-1) return i+1; else return 0;}int main(){ int i,n=25,m=5,j; SeqList R; IDX I= {{14,0},{34,5},{66,10},{85,15},{100,20}}; KeyType a[]= {8,14,6,9,10,22,34,18,19,31,40,38,54,66,46,71,78,68,80,85,100,94,88,96,87}; KeyType x=85; for (i=0; i<n; i++) R[i].key=a[i]; j=IdxSearch(I,m,R,n,x); if (j!=0) printf("%d是第%d个数据\n",x,j); else printf("未找到%d\n",x); return 0;}
阅读全文
0 0
- 第十三周项目1-验证算法1
- 第十三周项目1Prim 算法验证
- 第十三周项目1-Prim算法验证
- 第十三周--项目--验证算法(1)
- 第十三周项目-验证算法(1)
- 第十三周项目1-验证算法2
- 第十三周项目1-验证算法3
- 第十三周 项目1-验证算法
- 第十三周项目1-验证算法4
- 第十三周项目1验证算法
- 数据结构第十三周项目1--验证算法
- 第十三周项目1验证算法
- 第十三周项目1 验证算法
- (第十三周项目)验证算法
- 第十三周 项目 验证算法
- 第十三周 项目 验证算法
- 第十三周项目-验证算法(1-Prim算法)
- 第十三周 项目 1 - 验证算法之Prim算法
- 控件抛物线轨迹的实现
- 吴恩达【深度学习工程师】学习笔记(十)
- java代码中java.util.Date和java.sql.Date区别?
- 如何使用OpenCV+Keras轻松破解验证码?
- 【阿里云产品使用教程】1. 阿里云VPC ECS SLB NAT初体验
- 第十三周项目1 验证算法
- confluence安装
- HTTP URLConnection 发送get请求
- Vue -- router-link(传参)
- 12-12作业
- Linux
- 以太坊开发文档10
- leetCode#141. Linked List Cycle
- 指针函数和函数指针