Linear Search

  1. As per name we understand that it means it will be searched in Linear order.
  2. Search the particular element one by one until not find.
  3. Linear Search not a more efficient searching algorithm.
  4. Time Complexity of the Linear Search is O(n), where n is the number of elements.
  5. For Example :
An array Store the some element
arr[5]= {1,5,4,2,6}
In this we have to search element 6.
So Linear search first check arr[0],
check second element arr[1] ... arr[4].After 4 comparison with element of array it will get element 6.
#include<iostream>
using namespace std;
int main()
{
int arr[5]={1,5,4,2,6};
int search = 6;
for(int i =0;i<5;i++)
{
if(arr[i]==search)
{
cout<<"Index of array " <<i;
}
}
return 0;
}
Index of array 4  

For More about Linear Search . For More Searching algorithm Binary Search

Leave a Reply

%d bloggers like this: