C Program to linear search for a number

What is a linear search algorithm?

Linear search in C is used to find whether an element is present in a list of elements. It is also known as a sequential search. We are using the Array concept to enter the list of values in the C language. The linear search algorithm is one of the simple search algorithms we use in C programming. The Linear search algorithm works in such a way that it compares the search element with each and every element in the given array until it finds a match or the array gets completed.

For example, let us take some numbers "1,2,3,4,5" in an Array 'A'. if we want to search number '5' using the Linear search. The search algorithm starts searching the number '5' by comparing the number with a[0], which is '1' then it moves through 2, 3, 4, and lastly it gets a result at 'a[4]' which is '5', match found, and algorithm gets completed. 

  • The best case in Linear search is when the item found first of the array
  • The worst is the search element found last in the Array.

How is linear search implemented in C?

In this Linear search C program, after including the header libraries, and inserting all the user values into the array using a for loop. We need the user to enter the number that needs to search using a linear search algorithm.

After assigning that search element to a variable, compare each element of the array with that variable using the 'for loop' till we find the search element or it reaches the end of the array and displays output with a suitable message.

ALGORITHM

STEP 1: Includes the Header Libraries into the C program to use the built-in functions.

STEP 2: Start the main() function to start the execution of the C program.

STEP 3: Initialize the Array and the variables to be used in the program.

STEP 4: Accept the number of elements needed from the user using printf and scanf functions and store that integer value to a variable.

STEP 5: Accept and add the elements into the Array using a 'for loop'.

STEP 6: Display the elements in the Array using a for loop and printf.

STEP 7: Accept the element to be searched from the user and save that to a variable.

STEP 8: Open a for loop from zero to the number of terms and increment it by one for Linear Search with each element.

STEP 9: Compare the search element with the Array elements using the if condition and make the found one, when the number in the Array

STEP 10: After the loop check, if the 'found = 1', the element found else print not found.

C Source Code

                                          #include <stdio.h>

void main() 
{
   int array[10];
   int i, N, keynum, found = 0;                   
 
   printf("Enter the value of N\n");                 /* enters the number of values needed  */
   scanf("%d", & N);
   printf("Enter the elements one by one\n");            /* enter the values into a array and print the numbers */
   for (i = 0; i < N; i++)                 /*entering the elements in to the array */
   {
      scanf("%d", & array[I]);
   }
   printf("Enter the element to be searched\n");         /* enters the search element by the user */
   scanf("%d", & keynum);

   for (i = 0; i < N; i++)        /*using linear search we compare the number with other elements in array */ 
   {
      if (keynum == array[I]) 
      {
         found = 1;
         break;
      }
   }
   if (found == 1)
       printf("SUCCESSFUL SEARCH\n");
   else
       printf("Search is FAILED\n");
} 
                                      

OUTPUT

RUN 1

Enter the value of N
5

Enter the elements one by one
23
12
56
43
89

Enter the element to be searched
56
SUCCESSFUL SEARCH

RUN 2

Enter the value of N
3

Enter the elements one by one
456
213
879

Enter the element to be searched
1000

Search is FAILED