// 32 66 90
//15,32,9,22,18 43,66,49,35,53 90,78,71,86
//分析:分块查找由索引查找和子表查找两步完成。设n个数据元素的查找表分为m个子表,且每个子表
//均为t个元素,则t=n/m 。这样,分块查找的平均查找长度为:
// ASL=ASL索引表+ASL子表=1/2(m+1)=1/2(m+n/m)+1
//可见,平均查找长度不仅和表的总长度n有关,而且和所分的子表的个数m有关。
#include <stdio.h>
#define LEN 15
typedef struct
{
int key,low;
}index;
int block_search(int *a,index *inx,int k,int bn)
{
int low1=0,i,mid,high;
int high1=bn-1;
int find=0;
while((low1<=high1) && !find) //在索引表中查找k所在的块
{
mid=(low1+high1)/2;
if(k<inx[mid].key)
high1=mid-1;
else if(k>inx[mid].key)
low1=mid+1;
else
{
high1=mid-1;
find=1;
}
}
if(low1<bn)
{
i=inx[low1].low;
high=i+LEN/bn;
}
while(i<high && a[i]!=k)
i++; //在块中进行查找
if(a[i]!=k)
i=-1;
return i;
}
void main()
{
int a[15]={90,43,15,32,78,9,66,49,35,71,22,86,18,53};
index b[3]={32,1,66,6,90,11};
int key,bn,result;
printf("数据a是:");
printf("90,43,15,32,78,9,66,49,35,71,22,86,18,53\n");
key=35;
bn=3;
printf("key=%d\n",key);
result=block_search(a,b,key,bn); //查找k
if(result!=-1)
printf("查找成功!该数位置是:%d\n",result+1);
else
printf("查找失败!\n");
}
运行结果: