数据结构与算法(C++)之顺序查找

顺序查找:

#include <iostream>
//顺序查找,速度慢,只能按顺序去查找
using namespace std;

int shunxu(int *a,const int n,const int x);
int main()
{
int a[]={2,4,6,8,0,1,3,5,7,9};
int num=7;
int f=shunxu(a,10,num);
if(f<0){
cout<<"对不起,没有找到查询的结果"<<endl;
}
else{
cout<<"在a["<<f<<"]中找到"<<num<<endl;
}
return 0;
}
int shunxu(int *a,const int n,const int x)
{
int i;
for(i=0;i<n;i++){
if(a[i]==x){
return i;
}
if(i==n){
return -1;
}
}
}

猜你喜欢

转载自www.cnblogs.com/wslQAQ/p/12325013.html