Linear search is a very simple search algorithm. In this type of search, we start at the beginning and inspect each element. If a match is found then that particular location is returned, otherwise the search continues.

The following code shows the implementation of linear search algorithm

```
#include<iostream>
using namespace std;
int linear_search(int a[],int key,int n){
for(int i=0;i<n;i++){
if(a[i]==key) return i;
}
return -1;
}
int main(){
int a[]={1,2,3,4,5,6,7,8,9};
int size = sizeof(a)/sizeof(int);
int z = linear_search(a,5,size);
if(z!=-1){
cout<<"Element is at "<<z;
}
else {
cout<<"Element is not present";
}
return 0;
}
```

We get the following output.

If the value is not present, the function returns -1 and the program displays that the element is not present.

## 2 Replies to “Linear Search Algorithm”