索引查找(分块查找)

来源:互联网 发布:网络流行语mgt什么意思 编辑:程序博客网 时间:2024/05/21 17:25

索引查找思想是分块查找,它其实是顺序查找和二分查找的结合,即时间复杂度在两者之间。

索引查找表两部分构成

  • 索引表存储的是各块记录中的最大关键字值和各块的其实存储地址
  • 块表中存储查找表中所有的记录并按块有序,建议用链式存储

这里写图片描述

这里写图片描述

        #include <stdio.h>        #include <malloc.h>        typedef int keytype;        typedef struct bnode{//块链结点类型            keytype key;            struct bnode *next;        }Bnode,*Bnode2;        typedef struct{//索引表            keytype Maxkey;            Bnode *head;        }Snode;        typedef struct{            Snode r[100];            int length;        }Stable;        int FindBlock(Stable ST,keytype key)//利用二分确定在哪一块        {            int low,high,mid;            low=1;high=ST.length;            while(low<=high)            {                mid=(low+high)/2;                if(key==ST.r[mid].Maxkey)                    return mid;                else if(key<ST.r[mid].Maxkey)                    high=mid-1;                else                    low=mid+1;            }            return high+1;        }        void FindRec(Stable ST,keytype key)//顺序查找块链        {            Bnode2 p;            int Bno;            int count=0;            Bno=FindBlock(ST,key);            p=ST.r[Bno].head->next;            while(p&&p->key!=key)            {                p=p->next;                ++count;            }            if(p)                printf("%d找到,在第%d块,从大到小第%d个数\n",p->key,Bno,count);            else                printf("找不到\n");        }        Bnode2 Init_head(Bnode2 head,int n)//头插法创建块链        {            Bnode2 p;            int i=0;            head=(Bnode2)malloc(sizeof(Bnode));            head->next=NULL;            while(i<n)            {                p=(Bnode2)malloc(sizeof(Bnode));                scanf("%d",&p->key);                p->next=head->next;                head->next=p;                i++;            }            return head;        }        int main()        {            Stable ST;            Bnode2 T;            Bnode2 p;            int key2;            int get;            int i;            //赋值索引表长度            ST.length=6;            //索引表的值            ST.r[1].Maxkey=4;            //该值的地址,方便找到这个块的内容元素,2代表该块中元素个数            ST.r[1].head=Init_head(T,2);            ST.r[2].Maxkey=9;            ST.r[2].head=Init_head(T,2);            ST.r[3].Maxkey=14;            ST.r[3].head=Init_head(T,3);            ST.r[4].Maxkey=20;            ST.r[4].head=Init_head(T,3);            ST.r[5].Maxkey=26;            ST.r[5].head=Init_head(T,2);            ST.r[6].Maxkey=33;            ST.r[6].head=Init_head(T,2);            for(i=1;i<=ST.length;i++)            {                printf("第%d块数据最大关键字  ",i);                printf("%d",ST.r[i].Maxkey);                printf("\n第%d块数据  ",i);                p=ST.r[i].head->next;                while(p)                {                    printf("%d  ",p->key);                    p=p->next;                }                printf("\n");            }                printf("input KEY\n");                while(1)                {                    scanf("%d",&key2);                    FindRec(ST,key2);                }        }

运行结果

这里写图片描述