天天看点

数据结构与算法_顺序查找

#include<iostream>
using namespace std;


int SequentialSearch(int *array, const int n, const int k)
{
	 for(int i = 0; i < n; i++)
	 {
	 	if(array[i] == k)
	 		return i;
	 }
	 return -1;
}


int main()
{
	int array[] = {2, 4, 6, 8, 0, 1, 3, 5, 7, 9};
	int num = 5;
	int ret  = SequentialSearch(array, 10, num);
	if(ret == -1)
	{
		cout << "没有找到" << endl;
	}
	else 
	{
		cout << "在array["<<ret<<"]里找到" <<  num << endl; 
	}
	
	return 0;
}
           
数据结构与算法_顺序查找