按数据大小来分,查找分为内部查找和外部查找,如果从另一角度分,又可分为静态查找和动态查找。
内部查找:数据量较小的文件,可以一次性全部加载到内存中进行查找。
外部查找:数据量大的文件,无法一次加载到内存中,需要使用辅助存储器来分次处理。
静态查找:在查找过程中,查找的表格或文件的内容不会被改动,如符号表查找。
动态查找:在查找过程中,查找的表格或文件的内容可能被改动,如B-tree查找。
顺序查找又称为线性查找,是一种最简单的查找方法,通常用于未经组织化的文件,查找的过程从文件第一项数据开始,按顺序比较每个键值。时间复杂度:O(n),平均查找长度=(n+1)/2。缺点:当n很大时,平均查找长度较大,效率低。除非可以预测要查找的数据大概位于文件的前端,否则当文件的数据过大时,顺序查找法的效率显然不如其它查找法。优点:对表中数据元素的存储没有要求,事前不需要经过任何处理和排序。
#include <iostream>
#include <cstdlib>
#include<iomanip>
using namespace std;
const int iCount = 30;
int iarr[iCount] = {0};
void fnRandArr()
{
for(int i = 0;i < iCount;i++)
{
iarr[i] = rand() % 100;
}
cout << "输出数组为:" << endl;
for(int k = 0;k < 6;k++)
{
for(int j = 0;j < 5;j++)
{
cout << setw(2) << k * 5 + j << "[" << setw(2) << iarr[k*5+j] << "] ";
}
cout << endl;
}
}
bool fnSequentialSearch(const int iKey)
{
int iFound = 0;
for(int i = 0;i < iCount;i++)
{
if(iKey == iarr[i])
{
cout << "第"<< i+1 << "个位置找到键值" << iKey << endl;
iFound++;
}
}
if(0 == iFound)
{
cout << "没有找到" << iKey << endl;
return false;
}
else
{
return true;
}
}
int fnRandKey()
{
int iRandValue = rand() % 100;
return iRandValue;
}
int main()
{
bool bflag = true;
int iKey;
fnRandArr();
while(bflag)
{
iKey = fnRandKey();
bflag = !fnSequentialSearch(iKey);
}
system("pause");
return true;
}