第14周项目1.2验证分块查找算法
2015-12-08 16:35
232 查看
问题及代码:
运行结果:
学习心得:
分块查找既方便又快速
/* 文件名称:项目1.2.cpp 作者:孙洁 完成日期: 2015.12.8 问题描述: 分块查找 输入描述: 程序输出: 相关数据 */ #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; }
运行结果:
学习心得:
分块查找既方便又快速
相关文章推荐
- MySql相关及如何删除MySql服务
- 解决Linux操作系统下AES解密失败的问题
- 将博客搬至CSDN
- echo %SystemRoot%命令
- NSArray , NSDictionary 的用法...
- ORA-12008
- HashedWheelTimer
- JVM GC原理
- MFC笔记8
- Logcat查看/输出日志信息
- javascript-逻辑与(&&)逻辑或(||)
- nyoj 517 最小公倍数【数学】大数处理
- 成功的背后!(给所有IT人)
- FZU1920 Left Mouse Button(dfs)
- c++中volatile关键字解释
- Jquery easyui从零单排之datagrid数据绑定
- Assembly x64 Intro - Nasm Syntax
- 用opencv检测人眼并定位瞳孔位置
- VintaSoft Imaging .NET使用教程:如何将OCR设置为只识别数字
- HttpWebRequest上传文件(Excel等)