//结构体
typedef struct
{
KeyType key; //关键字项
InfoType data; //其他数据项
}RecType; //索引表元素类型
typedef RecType SeqList[MAXL];
//顺序算法
int SeqSearch(SeqList R, int n, KeyType k)
{
int i=0;
R[n].key = k;
while(R[i].key!=k) //从表头往后
i++;
if(i==n)
return 0; //没找到
else
return i+1; //找到返回
}
//结构体
typedef struct
{
KeyType key; //关键字项
InfoType data; //其他数据项
}RecType; //查找元素类型
typedef RecType SeqList[MAXL];
//二分算法,极其经典,就不给注释了
int BinSearch(SeqList R, int n, KeyType k)
{
int low=0, high=n-1, mid;
while(low<=high){
mid=(low+high)/2;
if(k==R[mid].key)
return mid+1;
if(k<R[mid].key)
high=mid-1;
else
low=mid+1;
}
return 0;
}
//结构体(分块需要两个结构体)
typedef struct
{
KeyType key; //关键字项
InfoType data; //其他数据项
}RecType; //索引表元素类型
typedef RecType SeqList[MAXL];
typedef struct
{
KeyType key;
int link; //对应块的起始下标
}IdxType; //索引表元素类型
//分块算法可以说是二分与顺序的结合,但是我不太喜欢.
//因为要在主函数中定义索引表,我觉得太麻烦了
//当然它有它的优点
int IdxSearch(IdxType I[], int b, SeqList R, int n, KeyType k)
{
int s=(n+b-1)/b; //s是每块的元素个数
int low=0, high=b-1, 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+s-1&&R[i].key!=k)
i++;
if(i<=I[high+1].link+s-1)
return i+1;
else
return 0;
}
主函数中代码较为简单,只是为了确定是否成功,没有表现出不同算法的特点和其优劣性。
想要完全理解,应该自己将算法执行过程通过代码输出,以便于理解。
#include
#include
using namespace std;
#define MAXL 100
typedef int KeyType;
typedef char InfoType[10];
typedef struct
{
KeyType key; //关键字项
InfoType data; //其他数据项
}RecType; //索引表元素类型
typedef RecType SeqList[MAXL];
typedef struct
{
KeyType key;
int link; //对应块的起始下标
}IdxType; //索引表元素类型
int SeqSearch(SeqList R, int n, KeyType k)
{
int i=0;
R[n].key = k;
while(R[i].key!=k) //从表头往后
i++;
if(i==n)
return 0; //没找到
else
return i+1; //找到返回
}
int BinSearch(SeqList R, int n, KeyType k)
{
int low=0, high=n-1, mid;
while(low<=high){
mid=(low+high)/2;
if(k==R[mid].key)
return mid+1;
if(k<R[mid].key)
high=mid-1;
else
low=mid+1;
}
return 0;
}
int IdxSearch(IdxType I[], int b, SeqList R, int n, KeyType k)
{
int s=(n+b-1)/b; //s是每块的元素个数
int low=0, high=b-1, 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+s-1&&R[i].key!=k)
i++;
if(i<=I[high+1].link+s-1)
return i+1;
else
return 0;
}
int main()
{
SeqList R;
int n=10;
int a[] = {
3,6,2,10,1,8,5,7,4,9}; //顺序查找
for(int i=0; i<n; i++)
R[i].key = a[i];
printf("顺序关键字序列:");
for(int i=0; i<n; i++)
printf("%d ",R[i].key);
printf("\n");
KeyType k = 5;
printf("顺序查找%d所比较的关键字依次为:",k);
int i=SeqSearch(R, n, k);
for(int j=0; j<i; j++)
printf("%d ", a[j]);
if(i != 0) //顺序查找
printf("\n元素%d的位置是%d\n", k, i);
else
printf("\n元素%d不在表中\n", k);
printf("\n");
KeyType t = 9;
SeqList T;
sort(a, a+10);
for(int i=0; i<n; i++)
T[i].key = a[i];
printf("二分关键字序列:");
for(int i=0; i<n; i++)
printf("%d ",T[i].key);
printf("\n");
printf("元素%d的位置是%d", t, BinSearch(T, n, t));
printf("\n");
printf("\n");
IdxType B[MAXL];
int b=5;
SeqList A;
KeyType fun = 88;
int sun[] = {
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};
int index[]={
14,34,66,85,100};
int link[]={
0, 5, 10, 15, 20};
int len = sizeof(sun)/sizeof(sun[0]);
for(int i=0; i<len; i++){
A[i].key = sun[i];
}
for(int i=0; i<5; i++){
B[i].key=index[i];
B[i].link = link[i];
}
printf("分块关键字序列:\n");
for(int i=0; i<len; i++)
printf("%d ",A[i].key);
printf("\n");
printf("元素%d的位置是%d", fun, IdxSearch(B, b, A, len, fun));
return 0;
}