在一列给定的值中进行搜索,从一端开始逐一检查每个元素,直到找到所需元素的过程。
线性查找又称为顺序查找
如果查找池是某种类型的一个表,比如一个数组,简单的查找方法是从表头开始,一次将每一个值与目标元素进行比较,最后,或者查找到目标,或者达到表尾,而目标不存在于组中,这个方法称为线性查找。
#include <iostream>
using namespace std;
int display(int [],int,int);
int main()
{
const int i=10;
int a[i];
int number;
for(int j=0;j <=i;j++)
a[j]=j*2;
for(int j=0;j <=i;j++)
cout < <a[j] < <' ';
cout < <"enter the number:";
cin>>number;
int result=display(a,i,number);
if(result==1)
cout < <"can find this number;" < <endl;
else
cout < <"number miss" < <endl;
system("pause");
return 0;
}
int display(int array[],int b,int conter)
{
for(int i=0;i <=b;i++)
{
if(array[i]==conter)
return 1;
}
return 0;
}