| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 906 人关注过本帖
标题:二分查找法
只看楼主 加入收藏
sunnvya
Rank: 5Rank: 5
等 级:贵宾
威 望:17
帖 子:1094
专家分:0
注 册:2005-11-23
收藏
 问题点数:0 回复次数:4 
二分查找法

#include <stdio.h>
#include <string.h>
#define max 10
#define SIZE sizeof(int)/sizeof(char)
void sort(int input[], int n);
int main()
{int a[max],i;
printf("================================================\n");
for(i=0;i<max;i++)
{scanf("%d",&a[i]);}
for(i=0;i<max;i++)
printf("%4d",a[i]);
printf("\n\n\n");
sort(a,max);
for(i=0;i<max;i++)
printf("%4d",a[i]);
printf("\n");
getch();
return 0;
}

void sort(int input[], int n)
{
int current, pos;
int low, high, mid;
int x;

for (current = 1; current < n; current++) {
x = input[current];
pos = -1; /* pos=-1 means DON'T move */
if (x < input[0]) /* insert before head ? */
pos = 0; /* YES, set pos to 0. */
else if (x <= input[current-1]) { /* bin search */
low = 0; /* low index. */
high = current - 1; /* high index */
while (high - low > 1) { /* stop? */
mid = (low + high) / 2; /* the mid pt */
if (x >= input[mid])
low = mid; /* try right */
else
high = mid; /* try left */
}
pos = low + 1;
}
if (pos >= 0) { /* move a block */
memmove((void *) &input[pos+1], (void *) &input[pos],
SIZE*(current-pos));
input[pos] = x;
}
}
}


搜索更多相关主题的帖子: int sort void input define 
2006-03-18 22:16
haishanglang
Rank: 1
等 级:新手上路
帖 子:378
专家分:0
注 册:2006-3-2
收藏
得分:0 
楼主的程序是用来按从小到大的顺序排序吧,那里是查找哦

2006-03-18 22:50
梦想中国
Rank: 2
等 级:新手上路
威 望:5
帖 子:539
专家分:0
注 册:2006-2-26
收藏
得分:0 
while()循环

2006-03-19 00:33
sunnvya
Rank: 5Rank: 5
等 级:贵宾
威 望:17
帖 子:1094
专家分:0
注 册:2005-11-23
收藏
得分:0 
呵呵
打错字了!
是排序!

http://www. 第二站>>>提供源码下载
2006-03-19 07:45
oヤ偽妳變壞
Rank: 2
等 级:新手上路
威 望:4
帖 子:2251
专家分:0
注 册:2006-3-19
收藏
得分:0 
2006-03-19 18:59
快速回复:二分查找法
数据加载中...
 
   



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

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