高效搜索,二分查找算法

作者: 贝博体育app网络  发布:2019-11-09

二分查找,二分查找算法

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

图片 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");}

本文由贝博体育app发布于贝博体育app网络,转载请注明出处:高效搜索,二分查找算法

关键词: