| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 513 人关注过本帖
标题:二分查找
只看楼主 加入收藏
无悔选择
Rank: 1
等 级:新手上路
威 望:1
帖 子:45
专家分:0
注 册:2005-12-25
收藏
 问题点数:0 回复次数:1 
二分查找
1.二分查找
在对线性表的操作中,经常需要查找某一个元素在线性表中的位置。此问题的输入是待查元素x和线性表L,输出为x在L中的位置或者x不在L中的信息。
搜索更多相关主题的帖子: 信息 线性表 元素 
2006-04-06 15:52
无悔选择
Rank: 1
等 级:新手上路
威 望:1
帖 子:45
专家分:0
注 册:2005-12-25
收藏
得分:0 
#include"stdio.h"
typedef struct node
{
int data[100];
int length;
}lnode;
int search(lnode L,int key,int low,int high)
{
int mid;
mid=(low+high)/2;
if(key==L.data[mid])
return(mid);
else
if(low==high)
return 0;
else
if(key>L.data[mid])
search(L,key,mid+1,high);
else
search(L,key,low,mid-1);
}
void main()
{
lnode L;
int i,m,frep;
printf("Input the number of the data:\n");
scanf("%d",&L.length);
for(i=1;i<=L.length;i++)
scanf("%d",&L.data[i]);
printf("Input the key to search:\n");
scanf("%d",&m);
frep=search(L,m,1,L.length);
if(frep==0)
printf("Not find the key!\n");
else
printf("The key is in the %d of the data!\n",frep);
}

2006-04-06 15:53
快速回复:二分查找
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.020417 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved