初学数据结构(顺序,折半,分块查找)

初学数据结构(顺序,折半,分块查找)

《数据结构教程》第6版上机实验题,P364
顺序查找(exp9-1)

//
// Created by Snow on 2023/5/11.
//
#include<cstdio>
#define MAXL 100
typedef int KeyType;
typedef char InfoType;
typedef struct
{
    KeyType key;//关键字项
    InfoType data;//其他数据项
}RecType;
//创建顺序表
void CreateList(RecType R[],const KeyType keys[],int n)
{
    for(int i=0;i<n;i++)
        R[i].key=keys[i];
}
//输出顺序表
void DispList(RecType R[],int n)
{
    for(int i=0;i<n;i++)
        printf("%d ",R[i].key);
    printf("\n");
}
//查找关键字
int SeqSearch(RecType R[],int n,KeyType k)
{
    int i=0;
    while(i<n&&R[i].key!=k)
        i++;
    if(i>=n)
        return 0;
    else
        return i+1;
}
int main()
{
    KeyType keys[]={3,6,2,10,1,8,5,7,4,9},k;
    int n=10;
    RecType R[MAXL];
    CreateList(R,keys,n);
    printf("查找表:\n");
    DispList(R,n);
    k=5;
    printf("R[%d]=%d\n", SeqSearch(R,n,k),k);
    return 0;
}

折半查找(exp9-2)

//
// Created by Snow on 2023/5/11.
//
#include<cstdio>
#define MAXL 100
typedef int KeyType;
typedef char InfoType;
typedef struct
{
    KeyType key;
    InfoType data;
}RecType;
void CreateList(RecType R[],const KeyType keys[],int n)
{
    int i;
    for(i=0;i<n;i++)
        R[i].key=keys[i];
}
void DispList(RecType R[],int n)
{
    int i;
    for(i=0;i<n;i++)
        printf("%d ",R[i].key);
    printf("\n");
}
int BinSearch(RecType R[],int n,KeyType k)
{
    int right=0,left=n-1,mid;
    while(right<=left)
    {
        mid=(right+left)/2;
        if(k==R[mid].key)
            return mid+1;
        else if(k<R[mid].key)
            left=mid-1;
        else
            right=mid+1;
    }
    return 0;
}
int main()
{
    KeyType keys[]={1,2,3,4,5,6,7,8,9,10},k;
    int n=10;
    RecType R[MAXL];
    CreateList(R,keys,n);
    printf("查找表:\n");
    DispList(R,n);
    k=9;
    printf("R[%d]=%d\n", BinSearch(R,n,k),k);
    return 0;
}

分块查找(exp9-3)

//
// Created by Snow on 2023/5/13.
//
#include<cstdio>
#define MAXL 100
#define MAXI 20
typedef int KeyType;
typedef char InfoType;
typedef struct
{
    KeyType key;
    InfoType data;
}RecType;
typedef struct
{
    KeyType key;
    int link;
}IdxType;
void CreateList(RecType R[],const KeyType keys[],int n)
{
    int i;
    for(i=0;i<n;i++)
        R[i].key=keys[i];
}
void DispList(RecType R[],int n)
{
    int i;
    for(i=0;i<n;i++)
        printf("%d ",R[i].key);
    printf("\n");
}
int IdxSearch(IdxType I[],int b,RecType R[],int n,KeyType k)
{
    int s=(n+b-1)/b;
    int low=0,high=b-1,mid,i;
    while(low<=high)
    {
        mid=(low+high)/2;
        if(I[mid].key>=k)
            high=mid-1;
        else
            low=mid+1;
    }
    i=I[high+1].link;
    while(i<=I[high+1].link+s-1&&R[i].key!=k)
        i++;
    if(i<=I[high+1].link+s-1)
        return i+1;
    else
        return 0;
}
int main()
{
    int n=25,b=5,j;
    RecType R[MAXL];
    IdxType I[MAXI]={{14,0},{34,5},{66,10},{85,15},{100,20}};
    KeyType 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};
    KeyType k=85;
    CreateList(R,a,n);
    DispList(R,n);
    j=IdxSearch(I,b,R,n,k);
    if (j!=-1)
        printf("R[%d]=%d\n",j,k);
    else
        printf("未找到该数%d\n",k);
    return 1;
}
posted @ 2023-05-15 12:21  SnowDreamXUE  阅读(9)  评论(0编辑  收藏  举报  来源