数据结构——查找算法

一.顺序查找

#include <stdio.h>

void seqSearch(int a[],int t,int n)
{
    int i=0;
    while(i<n && a[i]!=t)
        i++;
    if(i>=n)
        printf("未查找到该数!");
    else
    {
        printf("查找到该数%d,该数在数组中的顺序为%d",t,i+1);

    }
}

int main()
{
    int a[]={535,367,14,64,431,413};
    int t;
    int n=sizeof(a)/sizeof(a[0]);
    for(int i=0;i<n-1;i++)
        printf("%d,",a[i]);
    printf("%d\n请输入您想查找的数为:",a[n-1]);
    scanf("%d",&t);
    seqSearch(a,t,n);
    return 0;
}

二.折半查找

要求待查序列为-----有序表

#include <stdio.h>

void binSearch(int a[],int t,int n)
{
    int i=0;
    while(i<n && a[i]!=t)
        i++;

    
    int low=0,high=n-1,mid;
    while(low<=high)
    {
        mid=(low+high)/2;
        if(k==a[mid])
        {
            printf("查找到该数%d,该数在数组中的顺序为%d",t,mid+1); 
            return mid+1;
        }
           
        if(k<a[mid])
            high=mid-1;
        else
            low=mid+1;
    }
    
      printf("未查找到该数!");
}

int main()
{
    int a[]={1,2,3,4,5,6,7,8};
    int t;
    int n=sizeof(a)/sizeof(a[0]);
    for(int i=0;i<n-1;i++)
        printf("%d,",a[i]);
    printf("%d\n请输入您想查找的数为:",a[n-1]);
    scanf("%d",&t);
    binSearch(a,t,n);
    return 0;
}

三.索引查找

#include <stdio.h>
//#define Max 50 //索引表的最大长度

typedef struct{
    int key;
    int link;//指向对应块的起始下标
}IdxType;//索引表元素的类型

void buildtable(IdxType I[],int a[],int b,int n)
{
    int count=0;//count是查找的数组中的个数
    int k=0;//索引表中的数值顺序
    int m=0;//是索引表中的第几个数
    while(count<n)
    {
        int i=0,max;
        max=-999;
        while(i<b)
        {
            if(max<a[i+k])
            {
                max=a[i+k];
                I[k].link=k;
                I[k].key=max;
            }
            i++;
            count++;
        }
        printf("k=%d,",k);
        k+=b;\
        m++;
    }
    printf("\n索引表为:\n");
    for(int t=0;t<n;)
    {
        printf("[%d,%d],",I[t].key,I[t].link);
        t+=b;
    }
}

void IdxSearch(IdxType I[],int b,int a[],int n,int t)//b为索引表的长度,t为待查找的数
{
    int s=(n+b-1)/b;
    printf("索引表一块中的元素为:s=%d\n",s);
    int low=0,high=b-1,mid,i=0;
    while(low<=high)//在索引表中进行折半查找,找到位置为high+1
    {
        mid=(low+high)/2;
         if(I[mid*b].key>=t)
            high=mid-1;
        else
            low=mid+1;

    }
    //应先在索引表的high+1中查找,再在主数据表中进行顺序查找
    high=high+1;
    printf("索引表对应块的序号为:high=%d\n",high);
    high=high*5;
    printf("该序号对应的索引表起始下标为high=%d\n",high);

    //printf("I[high].link=%d\n",I[high].link);

    i=I[high].link;
    while(i<=I[high].link+s-1 && a[i]!=t)
        i++;
    if(i<=I[high].link+s-1)
        printf("数%d已查找到,该数在序列中的次序是%d\n",t,i+1);
    else
        printf("该数没查找到\n");
}


int main()
{
    int a[]={8,14,6,9,10,22,34,18,19,31,40,38,54,66,46,71,78,68,80,85,100,94,88,96,87};
    int t,b=5;//b为索引表的长度
    int n=sizeof(a)/sizeof(a[0]);
    IdxType I[n];
    buildtable(I,a,b,n);
    printf("\n请输入您想查找的数为:");
    scanf("%d",&t);
   IdxSearch(I,b,a,n,t);
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

阿隆要成大牛

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值