您的位置:澳门402永利com > 编程应用 > 二分查找

二分查找

发布时间:2019-09-23 20:45编辑:编程应用浏览(52)

    正常的二分查找,头尾不断替换,每次砍掉一半

    二分查找,二分查找算法

    正常的二分查找,头尾不断替换,每次砍掉一半

    图片 1

    #define _CRT_SECURE_NO_WARNINGS
    #include<stdlib.h>
    #include<stdio.h>
    #define N 1024
    
    void search(int a[N],int num) //二分查找法
    {
        int tou = 0;
        int wei = N - 1;
        int zhong;
        int flag = -1;  
        int ci = 0;
        while (tou<=wei)
        {    
            zhong = (tou + wei) / 2;
            if (num==a[zhong]) 
            {    
                ci++;
                printf("找到,a[%d]=%d", zhong, num);
                flag = 1;
                break;
            }
            else if (num > a[zhong])
            {
                tou = zhong + 1;
            }
            else 
            {
                wei = zhong - 1;
            }
    
        }
    
        if (flag == -1)
        {
            printf("没有找到");
        }
    }
    

     

    拉格朗日二分查找,每次砍掉一大半

    void searchL(int a[N], int num) //拉格朗日查找法
    {
        int tou = 0;
        int wei = N - 1;
        int zhong;
        int flag = -1;
        int ci = 0;
        while (tou <= wei)
        {
            zhong = tou+(wei-tou)*1.0*(num-a[tou])/(a[wei]-a[tou]);
            if (num == a[zhong])
            {
    
                printf("找到,a[%d]=%d,%d", zhong, num,++ci);
                flag = 1;
                break;
            }
            else if (num > a[zhong])
            {
                tou = zhong + 1;
            }
            else
            {
                wei = zhong - 1;
            }
    
        }
    
        if (flag == -1)
        {
            printf("没有找到");
        }
    }
    void main()
    {
        int a[N];
        for (int i = 0; i < N; i++)
        {
            a[i] = i;
            printf("%d", i);
        }
    
        int num;
        scanf("%d", &num);
        searchL(a, num);
        system("pause");
    }
    

     

    正常的二分查找,头尾不断替换,每次砍掉一半 #define _CRT_SECURE_NO_WARNINGS #includestdlib.h #includestdio.h #define N 1024 vo...

    图片 2

    #define _CRT_SECURE_NO_WARNINGS#include<stdlib.h>#include<stdio.h>#define N 1024void search(int a[N],int num) //二分查找法{    int tou = 0;    int wei = N - 1;    int zhong;    int flag = -1;      int ci = 0;    while (tou<=wei)    {            zhong = (tou + wei) / 2;        if (num==a[zhong])         {                ci++;            printf("找到,a[%d]=%d", zhong, num);            flag = 1;            break;        }        else if (num > a[zhong])        {            tou = zhong + 1;        }        else         {            wei = zhong - 1;        }    }    if (flag == -1)    {        printf("没有找到");    }}
    

    拉格朗日二分查找,每次砍掉一大半

    void searchL(int a[N], int num) //拉格朗日查找法{    int tou = 0;    int wei = N - 1;    int zhong;    int flag = -1;    int ci = 0;    while (tou <= wei)    {        zhong = tou+*1.0*(num-a[tou])/(a[wei]-a[tou]);        if (num == a[zhong])        {                printf("找到,a[%d]=%d,%d", zhong, num,++ci);            flag = 1;            break;        }        else if (num > a[zhong])        {            tou = zhong + 1;        }        else        {            wei = zhong - 1;        }    }    if (flag == -1)    {        printf("没有找到");    }}void main(){    int a[N];    for (int i = 0; i < N; i++)    {        a[i] = i;        printf("%d", i);    }    int num;    scanf("%d", &num);    searchL;    system("pause");}
    

    本文由澳门402永利com发布于编程应用,转载请注明出处:二分查找

    关键词:

上一篇:没有了

下一篇:没有了