第十四周项目一(2)验证算法
2015-11-30 16:33
615 查看
/* Copyright(c)2015,烟台大学计算机学院 All rights reserved. 文件名称:test.cpp 作者:杜佳丽 完成日期:2015年11月30日 问题描述:分块查找 */ #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; }
运行结果:
相关文章推荐
- 第七周项目一:建立顺序环形算法库
- iOS开发拓展篇—应用之间的跳转和数据传
- 使用Navicat连接管理远程linux服务器上的mysql数据库
- Android AsyncTask完全解析,带你从源码的角度彻底理解
- css案例学习之span边框实现的特殊效果
- 12-5 NSSet
- 《需求分析与系统设计》读书笔记之三
- 全国新生赛——————————1001————————————————
- Masonry自动布局详解七:ScrollView循环布局
- 第十二周项目3-图遍历算法的实现
- 项目1.4—验证算法
- 学习的网站(视频)
- 十三周 项目一 运行所讲算法,观察结果并领会算法(Dijkstra)
- 【第12周 项目3 - 图遍历算法实现】
- 第十四周项目2-二叉树排序树中查找的路径
- (2)分块查找算法
- freeswitch对接ims vos 众方 鼎信通达等网关的模版
- 第13周项目4-Floyd算法的验证
- 十二周项目一 图的基本算法库
- 项目2 二叉树排序树中查找的路径