Consider you have an array of 10 numbers and you want to find out position of a particular number. Linear Search can help you with that and it is one of the best criteria to search when you have un-ordered list of number.
Linear Search is a method for finding a particular in an array or list, which consists of checking every element of that array, one at a time and in sequence, until the desired value is found. In this tutorial, I will show you how to implement Linear Search using C language to search for a particular number in a list of n numbers (in my case n can have any positive value less than 9).
Algorithm:
- Let we have an array of 10 elements in variable
arrayand every element can be accessed usingarray[0], array[1], . . ., array[9]. i.e. size of array is 10 and index is starting from 0 (not 1). - Let we have to find value stored in variable
numandtempis a temporary variable which will be used for iteration purpose. - Initialize
tempwith 0. - Check whether value of
tempis less than size of array or not. If yes, continue to next step, otherwise go to step 7. - Now check whether
array[temp]is equal tonumor not. If yes, go to step number 7, otherwise continue to next step. - Increment value of variable
tempby 1 and move to 4th step. - Check if value of temporary variable is equal to size of array. If yes, then no match is found and move to step 9, otherwise move to next step.
- Match is found at index
tempor at positiontemp + 1. i.e.array[temp + 1] == num. - Exit the program.
Instructions:
- For this program, we need only a single header file which is mentioned below.
#include <stdio.h>
- In function
maindeclare an array of 10 numbers, a temporary variabletempfor iteration purpose,numvariable for storing the value to be found andsizevariable which will be used to store size of the array. Now ask user to enter size of the array and then ask him/her to enter all the number for the array.int array[10], size, num, temp; printf("Enter size of array : "); scanf("%d", &size); for(temp = 0; temp < size; temp++) { printf("Enter Element #%d : ", temp + 1); scanf("%d", &array[temp]); } - Now ask the user to enter the number he/she wants to search in the list.
printf("\n Enter the number you want to search : "); scanf("%d", &num); - Now using a
forloop (which will run at max equal to size input by user) check whether array contains an element that user wants to search or not using algorithm explained earlier. If any time, it founds a match, the loop will break.for(temp = 0; temp < size; temp++) { if(array[temp] == num) break; } - Now check whether value of
tempis equal to size of array or not. If yes, then it means for loop finished completely and break command is not triggered i.e no match is found. But if it is less than the size of array, then we indeed found a match at positiontemp + 1(becausetempstarts at 0).if(temp == size) printf("Number is not present in the array..."); else printf("Number found at #%d", temp + 1);
Here are two screenshots of the working sample code.
You can also download sample code from here.

