线性搜索:遍历,在一个数组中找到某个数的位置(或确认是否存在)
#include<stdio.h>
int search(int key, int a[], int len){
int ret = -1;
for(int i = 0; i<len; i++)
{
if(key==a[i])
{
ret = i;
break;
}
}
return ret;
}
int main(){
int a[] = {10,543,6,4,18,40,55,322,4,12,8,5};
int r = search(55, a, sizeof(a)/sizeof(a[0]));
printf("%d", r);
return 0;
}