验证折半查找算法
来源:互联网 发布:windows微信是什么 编辑:程序博客网 时间:2024/05/25 20:01
#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;
#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="http://img.blog.csdn.net/20151201185436714" alt="" />
x=90时
x=100时
0 0
- 验证折半查找算法
- 验证折半查找算法
- 验证折半查找算法
- 第十三周-验证折半查找的算法
- 验证算法——折半查找
- 第十四周 折半查找算法的验证
- 【验证算法——折半查找】
- 验证算法(递归的折半查找)
- 第14周 项目1 - 验证算法 - 折半查找算法
- 第十四周 项目一-验证算法(折半查找算法)
- 验证算法——递归的折半查找算法
- 【第十三周项目1】验证算法--折半查找算法
- 第14周项目(1)-验证折半查找算法
- 十四周 项目一 验证算法(折半查找)
- 十四周 项目一 验证算法(递归折半查找)
- 第十四周项目1-(1)验证折半查找算法
- 第十四周项目1-验证折半查找算法
- 第14周 项目1-(1)验证折半查找算法
- java基础(包)
- Android之异步任务AsyncTask解析Json数据
- .NET框架
- JavaScript 正在蚕食着开发领域
- 封装canvas的插件-KineticJs
- 验证折半查找算法
- 快速排序
- HDOJ 5597 GTW likes function (欧拉函数)
- v4l2采集MJPG然后保存为JPG
- 主从表的创建,查询代码待增加
- scala并发编程第8章习题1-3
- [Python标准库]re——正则表达式[三]
- uva 10271——Chopsticks
- 第十三周实践项目~Kruskal算法的验证