Here you will get the program for linear search in C++.
In linear search calculation, we contrast focused on the component and every component of the cluster. On the off chance that the component is discovered, at that point, its position is shown.
The most pessimistic scenario time unpredictability for linear search is O(n).
Program for Linear Search in C++
#include<iostream>
using namespace std;
int main()
{
int a[20],n,x,i,flag=0;
cout<<"How many elements?";
cin>>n;
cout<<"\nEnter elements of the array\n";
for(i=0;i<n;++i)
cin>>a[i];
cout<<"\nEnter element to search:";
cin>>x;
for(i=0;i<n;++i)
{
if(a[i]==x)
{
flag=1;
break;
}
}
if(flag)
cout<<"\nElement is found at position "<<i+1;
else
cout<<"\nElement not found";
return 0;
}
Output
How many elements?4
Enter elements of the array
5 9 12 4
Enter element to search:9
Element is found at position 2