标题:折半查找 帮忙找错
只看楼主
sun_shang001
Rank: 2
等 级:论坛游民
帖 子:103
专家分:78
注 册:2011-10-27
结帖率:84.62%
已结贴  问题点数:20 回复次数:1 
折半查找 帮忙找错
源码如下:
#include<stdio.h>
#include<stdlib.h>

#define MAX_LENGTH  100

typedef int KeyType;

typedef struct
{
    int key;
}ElemType;
 
typedef struct
{
    ElemType elem[MAX_LENGTH];     
    int length;
}SSTable;


int Search_Bin(SSTable ST,KeyType key)
{   
    int low,high,mid;
    low = 1;high = ST.length;
      while(low <=high)
     {
          mid = (low+high)/2;
          if(key ==ST.elem[mid].key)
              return mid;
          else
              if(key<ST.elem[mid].key)
                  high = mid-1;
              else
                  low=mid+1;
      }
      return 0;
}

void main()
{
    int i,result;
    SSTable ST;
    KeyType key;
    printf("please input length:");
    scanf("%d",&ST.length);
    for(i=1;i<=ST.length;i++)
    {
        printf("please input ST.elem:");
        scanf("%d",&ST.elem[i]);
    }
    printf("please input keyword:");
    scanf("%d",&key);
    result=Search_Bin(ST,key);
    if(result==0)
        printf("Don't find\n");
    else
        printf("Find the key,the position is %d\n",result);
}
搜索更多相关主题的帖子: while 源码 include return 
2012-05-30 22:03
beyondyf
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
等 级:贵宾
威 望:103
帖 子:3282
专家分:12654
注 册:2008-1-21
得分:20 
折半查找的前提是集合已排序。

重剑无锋,大巧不工
2012-05-30 22:08



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-368310-1-1.html




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

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