Here you will get program for linear search in C++.
在这里,您将获得使用C ++进行线性搜索的程序。
In linear search algorithm, we compare targeted element with each element of the array. If the element is found then its position is displayed.
在线性搜索算法中,我们将目标元素与数组的每个元素进行比较。 如果找到该元素,则显示其位置。
The worst case time complexity for linear search is O(n).
线性搜索的最坏情况时间复杂度为O(n)。
C ++中的线性搜索程序 (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&