算法之二分查找(c语言版实现+测试)

递归算法(deep是为了测试,实际生产不需要):
核心算法:

//递归算法
int rank(Type key,Type a[],int lo,int hi,int deep)
{
    if(lo > hi) return -1;
    int mid = lo + (hi - lo) / 2;
    printf("deep: %d lo: %d hi: %d\n",deep,lo,hi);
    if (key > a[mid])
    {
        ++deep;
        return rank(key,a,mid + 1,hi,deep);
    }
    else if (key < a[mid])
    {
        ++deep;
        return rank(key,a,lo,mid - 1,deep);
    }
    else
        return mid;
}
int BinarySearch(Type key,Type a[],int length)
{
    int deep = 0;
    return rank(key,a,0,length,deep);
}

辅助排序函数(快速排序):

void QuickSort(Type a[],int left,int right)
{
    Type temp;
    if(left < right)
    {
        int i = left;
        int j = right;
        temp = a[left];
        do
       {
          while(i < j && a[j] > temp)
            --j;
          if(i < j)
            a[i++] = a[j];
          while(i < j && a[i] < temp)
              ++i;
          if(i < j)
              a[j--] = a[i];
        }while(i != j);
        a[i] = temp;
        QuickSort(a,left,i-1);
        QuickSort(a,i + 1,right);
    }
}

测试代码:

void test(char *mode,char *file)
{
    //得到test 数组
    FILE *fp;
    if ((fp = fopen(file,"r")) == NULL)
    {
        return;
    }
    int count = 0;
    int temp;
    while (fscanf(fp,"%d",&temp) != EOF)
        ++count;
    fseek(fp,0,SEEK_SET);
    Type a[count];
    int pos = 0;
    while(fscanf(fp,"%d",&a[pos++]) != EOF);
    QuickSort(a,0,count - 1);
    Type key;
    if(strcmp(mode,"exist") == 0)
    {

        while(scanf("%d",&key) != EOF)
        {
            if(BinarySearch(key,a,sizeof(a)/sizeof(Type)-1) != -1)
            {
                printf("the key %d is exist in %s\n",key,file);
            }
        }
    }
    if(strcmp(mode,"noexist") == 0)
    {
        while(scanf("%d",&key) != EOF)
        {
            if(BinarySearch(key,a,sizeof(a)/sizeof(Type)-1) != -1)
            {
                printf("the key %d is not exist in %s\n",key,file);
            }
        }
    }
}

main.c

int main()
{
    test("exist","test.txt");
    return 0;
}

这里写图片描述
非递归算法:

//非递归算法
int BinarySearch(Type key,Type a[],int hi)
{
    int lo = 0;
    while(lo <= hi)
    {
        int mid = lo + (hi - lo) / 2;
        if (a[mid] > key)
            hi = mid - 1;
        else if (a[mid] < key)
            lo = mid + 1;
        else
            return mid;
    }
    return -1;
}

辅助排序函数(快速排序):

void QuickSort(Type a[],int left,int right)
{
    Type temp;
    if(left < right)
    {
        int i = left;
        int j = right;
        temp = a[left];
        do
       {
          while(i < j && a[j] > temp)
            --j;
          if(i < j)
            a[i++] = a[j];
          while(i < j && a[i] < temp)
              ++i;
          if(i < j)
              a[j--] = a[i];
        }while(i != j);
        a[i] = temp;
        QuickSort(a,left,i-1);
        QuickSort(a,i + 1,right);
    }
}

测试代码:

void test(char *mode,char *file)
{
    //得到test 数组
    FILE *fp;
    if ((fp = fopen(file,"r")) == NULL)
    {
        return;
    }
    int count = 0;
    int temp;
    while (fscanf(fp,"%d",&temp) != EOF)
        ++count;
    fseek(fp,0,SEEK_SET);
    Type a[count];
    int pos = 0;
    while(fscanf(fp,"%d",&a[pos++]) != EOF);
    QuickSort(a,0,count - 1);
    Type key;
    if(strcmp(mode,"exist") == 0)
    {

        while(scanf("%d",&key) != EOF)
        {
            if(BinarySearch(key,a,sizeof(a)/sizeof(Type)-1) != -1)
            {
                printf("the key %d is exist in %s\n",key,file);
            }
        }
    }
    if(strcmp(mode,"noexist") == 0)
    {
        while(scanf("%d",&key) != EOF)
        {
            if(BinarySearch(key,a,sizeof(a)/sizeof(Type)-1) != -1)
            {
                printf("the key %d is not exist in %s\n",key,file);
            }
        }
    }
}

main.c

int main()
{
    test("exist","test.txt");
    return 0;
}

这里写图片描述

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值