注册 登录
编程论坛 数据结构与算法

分块查找算法调试不出来

a450631266 发布于 2013-12-07 14:16, 520 次点击
#include<iostream>
#define MAXL 50
#define MAXI 50
typedef int KeyType;
typedef struct
{
    KeyType key;      
}NodeType;                          //定义顺序表
typedef NodeType SeqList[MAXL];     //顺序表类型为SeqList

typedef struct
{
    KeyType key;                    //KeyType为关键字类型
    int link;                       //指向对应块的起始下标
}IdxType;
typedef IdxType IDX[MAXI];          //索引表类型

int IdxSearch(IDX I,int b,SeqList R,int n,KeyType k)
{
    int low=0,high=b-1,mid,i;
    int s=n/b;                        //s为每块元素的个数,应该n/b的向上取整
    while(low<=high)                //在索引表中进行折半查找,找到的位置为high+1
    {
        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+s-1 && R[i].key!=k)
        i++;
    if(i<=I[high+1].link+s-1)
        return i+1;
    else
        return 0;
}

void main()
{
    int a;
    IDX I;
    SeqList R={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};
    a=IdxSearch(I,5,R,25,46);
    printf("%d",a);
}




没有错误,但是一执行就关闭了,就解释。。。
2 回复
#2
yuccn2013-12-11 18:58
IDX I 都没有初始化
#3
pangshch2013-12-11 22:06
typedef NodeType SeqList[MAXL];     //顺序表类型为SeqList
 typedef IdxType IDX[MAXI];  

IDX I;
 SeqList R
这几句有没有问题?
1