#include <iostream>
#include <string>
using namespace std;
const int MAXSIZE = 100;
typedef char KeyType;
typedef struct
{
KeyType key;
} DataType;
typedef struct
{
DataType list[MAXSIZE];
int length;
}SSTable;
//顺序表查找1
int SeqSearch(SSTable S, DataType x)
{
int i = 0;
while(i < S.length && S.list[i].key != x.key)
i++;
if(S.list[i].key == x.key)
return i + 1;
else
return 0;
}
//哨兵项的静态查找
int SeqSearch2(SSTable S, DataType x)
{
int i = S.length;
S.list[0].key = x.key;
while(S.list[i].key != x.key)
{
i--;
}
return i;
}
//有序折半查找
int BinarySearch(SSTable S, DataType x)
{
int low, high, mid;
low = 0;high = S.length;
while(low < high)
{
mid = (high + low) / 2;
if(S.list[mid].key == x.key)
return mid + 1;
else if(S.list[mid].key < x.key)
{
low = mid + 1;
}
else high = mid - 1;
}
return 0;
}
int main(
初学数据结构---查找代码之静态查找
最新推荐文章于 2022-02-14 01:11:32 发布
本文介绍了三种静态查找算法:顺序查找、带有哨兵项的静态查找以及有序数组的折半查找。通过示例代码展示了它们在不同场景下的应用,并在main函数中进行了测试,输出了查找结果。
摘要由CSDN通过智能技术生成