第十四周项目一 (2) 验证算法——分块查找
2015-11-30 16:59
239 查看
/* * Copyright (c) 2015, 烟台大学计算机与控制工程学院 * All rights reserved. * 文件名称: main.cpp * 作者:巩凯强 * 完成日期:2015年11月30日 * 版本号:codeblocks * * 问题描述: 用{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}作为数据表,自行构造索引表,对查找85进行测试。 * 输入描述: 无 * 程序输出: 见运行结果 */ #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; }
运行结果:
知识点总结:
分块查找又称索引顺序查找,性能介于顺序查找和折半查找之间的查找方法。
基本思路:首先查找索引表,因为索引表是有序表,故可采用折半查找或顺序查找,以确定待查的元素在哪一块;然后在已确定的块中进行顺序查找(因块内元素无序,只能用顺序查找)。
学习心得:
分块查找的主要代价是增加一个索引表的存储空间和延长建立索引表的时间。
相关文章推荐
- 如何选择前端框架:ANGULAR VS EMBER VS REACT
- 【第13周 项目3 - Dijkstra算法的验证】
- 初学Redis(1)——认识Redis
- 写下这篇东西,给我未来的爱人。【沫是女王】
- php页面操作超时
- python地理处理包——pyproj官方中文文档
- 十四周 项目1-2 分块查找
- iOS自定义UIAlertView的小tips
- 抗衰老,吃这些食物越来越年轻
- mysql prompt的用法详解
- mysql prompt的用法详解
- JavaScript数据结构 --- 字典
- pyproj官方中文文档
- latch: cache buffers chains 案例
- POJ2923 状态压缩01背包 xingxing在努力
- hdu 游乐场
- 第十四周项目5二叉树排序树中查找的路径
- 关于Android service的一些基础知识集合
- 【详解】从b站抓取信息并存入数据库
- 第13周项目3二叉排序树