第十三周项目1(2)

来源:互联网 发布:pycharm安装教程linux 编辑:程序博客网 时间:2024/06/15 17:03
/* 烟台大学计算机学院  文件名称:xiangmu.cpp  作者:于琛  完成日期:2017年11月26日  问题描述:验证分块查找算法。请用22,4,23,11,20,2,15,13,30,45,26,34,29,35,26,36,55,98,56, 74,61,90,80,96,127,158,116,114,128,113,115,102,184,211,243,188,187,218,195,210,279,307,492,452,408,361,421,399,856,523,704,703,697,535,534,739(共n=56个数据,每块数据个数s=8)作为数据表,自行构造索引表,分别对查找61、739、200进行测试。   输入描述:无 输出描述:输出查找位置和结果 */ #include <stdio.h>#define MAXL 100    //数据表的最大长度#define MAXI 50     //索引表的最大长度typedef int KeyType;typedef char InfoType[10];typedef struct{    KeyType key;                //KeyType为关键字的数据类型    InfoType data;              //其他数据} NodeType;typedef NodeType SeqList[MAXL]; //顺序表类型typedef struct{    KeyType key;            //KeyType为关键字的类型    int link;               //指向对应块的起始下标} IdxType;typedef IdxType IDX[MAXI];  //索引表类型int IdxSearch(IDX I,int m,SeqList R,int n,KeyType k){    int low=0,high=m-1,mid,i;    int b=n/m;              //b为每块的记录个数    while (low<=high)       //在索引表中进行二分查找,找到的位置存放在low中    {        mid=(low+high)/2;        if (I[mid].key>=k)            high=mid-1;        else            low=mid+1;    }    //应在索引表的high+1块中,再在线性表中进行顺序查找    i=I[high+1].link;    while (i<=I[high+1].link+b-1 && R[i].key!=k) i++;    if (i<=I[high+1].link+b-1)        return i+1;    else        return 0;}int main(){    int i,n=56,m=7,j;    int j1;    int j2;    SeqList R;    IDX I= {{23,0},{45,8},{98,16},{158,24},{243,32},{492,40},{856,48}};    KeyType a[]= {22,4,23,11,20,2,15,13,30,45,26,34,29,35,26,36,55,98,56,74,                  61,90,80,96,127,158,116,114,128,113,115,102,184,211,243,188,                  187,218,195,210,279,307,492,452,408,361,421,399,856,523,704,                  703,697,535,534,739};    KeyType x=61;    KeyType y=739;    KeyType z=200;    for (i=0; i<n; i++)        R[i].key=a[i];    j=IdxSearch(I,m,R,n,x);    if (j!=0)        printf("%d是第%d个数据\n",x,j);    else        printf("未找到%d\n",x);    j1=IdxSearch(I,m,R,n,y);    if (j1!=0)        printf("%d是第%d个数据\n",y,j1);    else        printf("未找到%d\n",y);    j2=IdxSearch(I,m,R,n,z);    if (j2!=0)        printf("%d是第%d个数据\n",z,j2);    else        printf("未找到%d\n",z);    return 0;}

运行结果:

学习心得:

学到了分块查找算法。

原创粉丝点击