zoukankan      html  css  js  c++  java
  • 用c语言编写二分查找法

       二分法的适用范围为有序数列,这方面很有局限性。

    #include<stdio.h>
    
    //二分查找法
    void binary_search(int a[],int start,int mid,int end);
    
    int main()
    {
        int iLength,istars,i,iTimes,iNumber,n;
        int a[100];
        printf("please enter the length of the array:
     ");
        scanf("%d",&iLength);
        printf("please enter the number:
    ");
        for(i=0;i<iLength;i++)
        {
            scanf("%d",&a[i]);
        }
    
    
        printf("please enter the number that you want:
    ");
        scanf("%d",&istars);
        /*if(n==istars)
        {
            
        }
        else
        {
            printf("sorry ,without this number
    ");
        }*/
         binary_search(a,0,istars,iLength);
        
    }
    void  binary_search(int a[],int start,int iPut,int end)
    
    {
        int i,j,k,n;
        i=start;
        j=end;
        k=(i+j)/2;
        n=0;
        while(i<j)
        //while(i<=k && j>=k) 这个写的本身就有漏洞,就是对整个程序的不理解
        {
            n++;
            if(iPut>a[k])
            {
                i=k+1;
            }
            if(iPut<a[k])
            {
                j=k-1;
            }
            if(iPut==a[k])
            {
                printf("the number need %d times to find......a[%d]=%d
    ",n,k,iPut);
            
                break;
            }
            
        }

    if(n==0)
            printf("failed
    ");
        
    }

     这是开始参考书目,自己编写的程序,但是程序本身能准确运行范围内的数字,一旦输入范围外的数字则出现卡壳现象。因此,要在源代码中改进。



    #include<stdio.h>
    
    //二分查找法
    void binary_search(int a[],int start,int iPut,int end);
    
    int main()
    {
        int iLength,istars,i,iTimes,iNumber,n;
        int a[100];
        printf("please enter the length of the array:
     ");
        scanf("%d",&iLength);
        printf("please enter the number:
    ");
        for(i=0;i<iLength;i++)
        {
            scanf("%d",&a[i]);
        }
    
    
        printf("please enter the number that you want:
    ");
        scanf("%d",&istars);
        /*if(n==istars)
        {
            
        }
        else
        {
            printf("sorry ,without this number
    ");
        }*/
         binary_search(a,0,istars,iLength);
        
    }
    void binary_search(int a[],int start,int iPut,int end)
    
    {
        int i,j,k,n,m;
        m=0;
        i=start;
        j=end;
    
        n=0;
        while(i<j)
        //while(i<=k && j>=k) 这个写的本身就有漏洞,就是对整个程序的不理解
        {
            n++;
            k=(i+j)/2;//这个为关键!!!!!
            if(iPut>a[k])
            {
                i=k+1;
            }
            if(iPut<a[k])
            {
                j=k-1;
            }
            if(iPut==a[k])
            {
                printf("the number need %d times to find......a[%d]=%d
    ",n,k,iPut);
                m++;
                break;
            }
            
        }
        if(m==0)
            printf("failed
    ");
        
    }
    
    
    
    
    

    第一次代码错误的主要原因为中间值k的位置放错了。k要放在循环之中,要不然无法形成循环,中间值就永远都是中间值,无法利用其的逼近功能,

    与此同时编写代码多了,时常会把 if 和while用混,这就需要具体问题,if只执行一次,while则是一个循环,使用while的时候要写上跳出条件,要不然会无休止的运行下去,导致死循环。

  • 相关阅读:
    线程正常终止pthread_exit,pthread_join,pthread_kill,pthread_cancel,sigwait,sigaddset
    线程的创建,pthread_create,pthread_self,pthread_once
    线程和进程的关系
    改变进程的优先级,nice,getpriority,setpriority
    setuid和setgid
    等待进程结束wait,waitpid
    执行新程序以及环境变量
    进程退出exit、_exit、abort
    VBA 判断单元格是否为公式,可用于数组
    ADODB 调用存储过程
  • 原文地址:https://www.cnblogs.com/xiaochige/p/5946296.html
Copyright © 2011-2022 走看看