2015年大二上-数据结构-查找-1-(2)-分块查找

时间:2023-01-15 22:10:13
/*
*Copyright (c) 2014,烟台大学计算机学院
*All rights reserved.
*文件名称:Annpion.cpp
*作者:王耀鹏
*完成日期:2016年3月3日
*版本号:v1.0
*
*问题描述:分块查找 
*输入描述:数值
*输出描述:分块查找 
*/
#include <stdio.h>
#define MAXL 100
typedef int KeyType;
typedef char InfoType[10];
typedef struct
{
    KeyType key;
    InfoType data;
} NodeType;
typedef NodeType SeqList[MAXL];

typedef struct
{
    KeyType key;
    int link;
} IdxType;
typedef IdxType IDX[MAXL];

int IdxSearch(IDX I,int m,SeqList R,int n,KeyType k)
{
    int low=0,high=m-1,b=n/m,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+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=25,m=5,j;
    SeqList R;
    IDX I= {{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 x=85;
    for (i=0; i<n; i++)
        R[i].key=a[i];
    j=IdxSearch(I,m,R,n,x);
    printf("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\n");
    if (j!=0)
        printf("%d是第%d个数据\n",x,j);
    else
        printf("未找到%d\n",x);
    return 0;
}

运行结果:

2015年大二上-数据结构-查找-1-(2)-分块查找