通常在查找数组的时候,会发现一个一个查找很浪费资源,因此通过二分查找法,实现

算法学习:_第1张图片

(上面是思维导图)

#include
#include 
//while、do while 、goto、for、递归

void digui(int shang,int xia,int zhong,int num)
{
    zhong = (shang + xia) / 2;
    if (shang <= xia)
    {
        if (num == zhong)
        {
            printf("递归:find.%d\n", zhong);
            return;
        }
        else if (num < zhong)
        {
            xia = zhong - 1;
            printf("shang.%d,zhong.%d,xia.%d \n", shang, zhong, xia);
        }
        else
        {
            shang = zhong + 1;
            printf("shang.%d,zhong.%d,xia.%d \n", shang, zhong, xia);
        }

    }
    return;
}

int main()
{
    int a[1024];
    for (int i = 0; i<1024; i++) {
        a[i] = i;
        //printf("%d\n",a[i]);
    }
    int shang = 0;
    int xia = 1023;
    int zhong = (shang + xia) / 2;
    int num = 102;
    while (shang <= xia)//一、while
    {
        zhong = (shang + xia) / 2;
        if (num == zhong)
        {
            printf("while:find.%d\n", zhong);
            break;
        }
        else if (num < zhong)
        {
            xia = zhong - 1;
            printf("shang.%d,zhong.%d,xia.%d \n", shang, zhong, xia);

        }
        else
        {
            shang = zhong + 1;
            printf("shang.%d,zhong.%d,xia.%d \n", shang, zhong, xia);
        }

    }
    printf("==================\n");
    printf("==================\n");
    printf("==================\n");

    shang = 0;
    xia = 1023;
    zhong = (shang + xia) / 2;
    num = 102;
    do {//二、do while
        zhong = (shang + xia) / 2;
        if (num == zhong)
        {
            printf("do while:find.%d\n", zhong);
            break;
        }
        else if (num < zhong)
        {
            xia = zhong - 1;
            printf("shang.%d,zhong.%d,xia.%d \n", shang, zhong, xia);
        }
        else
        {
            shang = zhong + 1;
            printf("shang.%d,zhong.%d,xia.%d \n", shang, zhong, xia);
        }

    } while (xia>=shang);

    printf("==================\n");
    printf("==================\n");
    printf("==================\n");

    shang = 0;
    xia = 1023;
    zhong = (shang + xia) / 2;
    num = 988;
AAA://三、goto
    zhong = (xia + shang) / 2;
    if (num == zhong)
    {
        printf("goto:find.%d\n", zhong);
        goto BBB;
    }
    else if (num > zhong)
    {
        shang = zhong + 1;
        printf("shang.%d,zhong.%d,xia.%d \n", shang, zhong, xia);
    }
    else
    {
        xia = zhong - 1;
        printf("shang.%d,zhong.%d,xia.%d \n", shang, zhong, xia);
    }

    if (shang<=xia)
    {
        goto AAA;
    }
BBB:

    printf("==================\n");
    printf("==================\n");
    printf("==================\n");

    shang = 0;
    xia = 1023;
    zhong = (shang + xia) / 2;
    num = 288;
    for (int i; shang <= xia ;zhong = (shang + xia) / 2)
    {
        if (num == zhong)
        {
            printf("for:find.%d\n", zhong);
            break;
        }
        else if (num < zhong)
        {
            xia = zhong - 1;
            printf("shang.%d,zhong.%d,xia.%d \n", shang, zhong, xia);
        }
        else
        {
            shang = zhong + 1;
            printf("shang.%d,zhong.%d,xia.%d \n", shang, zhong, xia);
        }
    }

    printf("==================\n");
    printf("==================\n");
    printf("==================\n");

    shang = 0;
    xia = 1023;
    zhong = (shang + xia) / 2;
    num = 288;

    digui(shang,xia,zhong,num); //递归

    getchar();
    getchar(); 
    Sleep(5000);

    return 0;
}