第十四周项目(1):验证算法1
2015-11-30 17:06
357 查看
问题及代码:
运行结果:
知识点及总结:
折半查找要求线性表必须是有序的,注意逻辑序号和物理序号的不同。
/* *烟台大学计算机与控制工程学院 *作 者:杨宁 *完成日期:2015年11月30日 *问题描述:认真阅读并验证折半查找算法。请用有序表{12,18,24,35,47,50,62,83,90,115,134}作为测试序列, 分别对查找90、47、100进行测试。 */ #include <stdio.h> #define MAXL 100 typedef 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[]= {12,18,24,35,47,50,62,83,90,115,134},x=100; 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; }
运行结果:
知识点及总结:
折半查找要求线性表必须是有序的,注意逻辑序号和物理序号的不同。
相关文章推荐
- 模糊语意变数、规则和模糊运算--AForge.NET框架的使用(二)
- [转]JSONObject与JSONArray的使用
- 第十四周知原理--3
- 读 《周爱民--大道之简》 笔记
- 第13周项目3-Dijkstra算法的验证
- 第14周项目2——二叉树排序树中查找的路径
- 文章标题
- 第13周项目4平衡二叉树
- Java抽象类和接口概念和特点理解
- 51nod 1105 第K大的数 (二分答案)
- 【实战Java高并发程序设计 3】带有时间戳的对象引用:AtomicStampedReference
- Android listview加载列表时,无数据,显示默认页
- Android Emoji表情方案
- 模糊集合和隶属度函数--AForge.NET框架的使用(一)
- android 源码角度全方位理解filter
- Axiom3D学习日记 0.Axiom基础知识
- 移动端屏幕尺寸自适应
- jquery尺寸:宽度与高度
- 在AdMob中介内创建横幅广告自定义事件
- 第14周—项目1(2)分块查找