Linear search Algorithm explained

Searching algorithms are used to find a character or a number in a given array. When comes to searching algorithms, Linear search is the most easiest to implement as the logic behind this is quite simple. In this tutorial we are going to look at the logic behind Linear search, how to implement it in your code and what are its advantages and limitations. Here are some of the algorithms we have covered earlier in our website.

Linear Search Algorithm:

In this Algorithm search for a particular value will be done in a linear fashion. It means the code will search for the element user is looking for by comparing it with every number in the array starting from arr[0], arr[1]…..arr[n] until the element is found. If the element is found, algorithm will cease searching, if not search will continue till the last number or character in the sequence.

Explanation:

Let us consider below array.

linear-search-algorithmLet us consider the number to be searched in this array is “12” and we have to start the search of numbers by comparing with the first element “7”

Since the number “12” is not equal to first element in array, comparison is shifted to the next number in the array.

Now, in the second comparison the number is not a match again. So the comparison is shifted to third number in the array.

Now, the comparison is shifts to the next number in the array. A match occurs when comparing both the values. So the position of this element in the array will be returned and the search will be stopped.

How to implement Insertion Algorithm :

  1. Obtain the numbers or characters in the array
  2. Get the number or character to be searched
  3. Run comparison with each element from the array starting from arr[0] and run the search sequentially.
  4. And if number is found return the position of the element in the array
  5. If not run comparison till last element in the array and print the message “Number not found”.

CODE:

#include<stdio.h>
#include<conio.h>
#define SIZE 5

int linear_search(int a[],int tosearch,int size)
{
int i=0,found = 0,location;
while (found!=1 && i < size)	 //Searching for the number in array by comparison
{
if (a[i] == tosearch)
found = 1;
else
++i;
}

if(found)                      //If the number is found return the location index
location = i;
else
location = -1;
return location;
}

int main() {
int list[SIZE], tosearch, index, i;
printf("\nEnter %d numbers : ",SIZE);
for(i = 0; i < SIZE; i++)
scanf("%d", &list[i]); 	//Reading the characters in to the array

printf("Enter an element to be searched: ");
scanf("%d", &tosearch);	//Reading the number or character to be searched
index = linear_search(list,tosearch,SIZE);
if (index != -1)
printf("Your Number was found at index: %d ",index);
else
printf("Sorry, your number was not found");
getch();
return 0;
}

Where to use Linear search Algorithm :

Linear search algorithm is quite useful for small arrays. Since it runs comparison with every element in the array execution time tends to be high. Therefore it will be inefficient to use with arrays with high number of elements. On the bright side Linear search is quite easy to implement in your code, occupies less space and less complex comparing to other search algorithms.

Try out this Activity :

In order to understand this algorithm better, try out the following activity

  • Write a program that takes 10 number input and store it in an array, it should also ask for user input for the number to search in array. The program should perform linear search using given data and display the output accordingly.

Hope this tutorial was helpful to you. Do try the above activity and post your code below. If you have any questions, feedback post them in the comments section below.


Leave a Comment

Your email address will not be published. Required fields are marked *