索引查找(分块查找)
来源:互联网 发布:网络流行语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); } }
运行结果
阅读全文
0 0
- 索引查找(分块查找)
- 分块索引(查找)
- 分块查找(索引顺序表查找)
- 索引查找(索引查找、分块查找) C语言实现
- 分块查找\索引顺序查找
- 索引顺序表(分块查找)
- 【查找算法】——顺序查找、折半查找、分块查找(索引查找)
- 索引顺序表查找算法(分块查找算法)
- 索引顺序表查找算法(分块查找)
- (2)分块查找
- 创建索引顺序表和分块查找
- 分块查找
- 分块查找
- 分块查找
- 分块查找
- 分块查找
- 分块查找
- 分块查找
- BZOJ3835: [Poi2014]Supercomputer
- Python3 CookBook | 数据结构和算法(一)
- 【TensorFlow】MNIST(使用softmax)
- fresco加载图片+EventBus Activity之间跳转传值+GreenDAo数据库+retrofit请求数据+recyclerview展示数据+ButterKnife找控件
- Aerospike 的索引内存管理--as_index->dim
- 索引查找(分块查找)
- 3
- 第一周 枚举 例题4.称硬币 [POJ1013]
- 关于线索二叉树恢复成正常二叉树
- C#实现自动升级(附源码)
- 图像连通域分析(Two-Pass & DFS )
- codeforces 894C
- Aerospike的bin内存管理--即列内存管理
- Linux 静态库使用