# Implementation of Linear Search

Linear Search algorithm, Implementation of Linear Search algorithm
Implementation of Linear Search algorithms in C language with Online compiler link:

In Linear search algorithm, we tried to find out the search element sequentially from the start to the end of the Linear array ( whether sorted or unsorted) by comparing each element. If at any specific position search element is found then we can say that search is successful otherwise search is not successful or element not found in the linear array. In the best case the time complexity of the linear search algorithm is O(1) and in the worst case, the time complexity is O(n), where n is the size of the linear array.

Below is the step which we follow to implement Linear Search are:
1. First, declare all the header file you want in your program ( according to language in which you want to implement the algorithm).
2. Declare an array of any size ( i.e arr[15]) with the different variable you need (like search_element, i and j for loop variable).
3. Enter element into the array arr[15].
4. Enter the search element search_element
5. Start comparing array element with the search element from the start of the array.
6. If at any array index if the array element and search_element are equal, then we say the search is successful.
7. If no element of the array is equal to search_element, then we say the search is not successful.
8. End of the program.

Implementation of Linear Search Algorithm in C:

#include<stdio.h>
int main()
{
int arr[10], i, num, n, pos;
scanf("%d",&n);
for(i=0; i<n; i++)
{
scanf("%d",&arr[i]);
}
//printf("/n Enter the number to be search : ");
scanf("%d",&num);
int flag=0;
for(i=0; i<n; i++)
{
if(arr[i]==num)
{
flag=1;
pos=i+1;
printf("\n Element is found at position %d ",pos);
break;
}
}
if(flag==0)
return 0;
}

Online Compiler Link for the above code: Run the above program online

Thanks
Mukesh Rajput

Mukesh Rajput

I am a Computer Engineer, a small amount of the programming tips as it’s my hobby, I love to travel and meet people so little about travel, a fashion lover and love to eat food, I am investing a good time to keep the body fit so little about fitness also..