验证折半查找算法
2015-12-01 18:52
393 查看
#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; }
<p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; color: rgb(85, 85, 85); font-family: 'microsoft yahei'; font-size: 15px; line-height: 35px;">运行结果:</p><p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; color: rgb(85, 85, 85); font-family: 'microsoft yahei'; font-size: 15px; line-height: 35px;">x=47时</p><img src="https://img-blog.csdn.net/20151201185436714" alt="" />
x=90时
x=100时
相关文章推荐
- 苹果开发 手动内存管理MRC
- DNS主从服务器搭建
- 比尔·盖茨喝过“粪便水”后,再砸10亿美元研发清洁能源
- 针对大的sql文件删除行操作
- PagerSlidingTabStrip视图切换使用心得
- 各种数据类型的取值范围
- LeetCode 134 Gas Station
- error C2011: “XXX”:“class”类型重定义
- Android 虚线分割线
- 蓝桥杯OJ刷题日记——06-基础练习 杨辉三角形
- BZOJ 1489: [HNOI2009]双递增序( dp )
- 1.加载和运行
- 【思维】LightOj Fibsieve`s Fantabulous Birthday 1008
- Macromedia Fireworks V8.0 简体中文版
- 未排序正数数组中累加和为给定值的最长子数组长度
- 定义一个结构体变量,计算该日在本年中是第几天?
- Codeigniter集成PHPExcel
- 解决一些android studio 中的编码问题
- 通过OpenStack来管理VMware虚拟化集群
- Excel批量插入多行