Linear search in an array | Linear search program in c

 

Sometimes, you may need to search for an element in an array. For example, given an array {2,4,6,8,10}, you may want to know if 10 is present in the array.

Linear search is a simple technique to search for an element. We iterate over every element of the array to check if it matches with the number we’re looking for.

Linear search in C

Here is the source code for the program


 #include <stdio.h>
 int main()
 {
  int array[100], search, c, n;
  printf("Enter number of elements to be entered in array\n");
  scanf("%d", &n);
  printf("Enter %d integer(s)\n", n);
  for (c = 0; c < n; c++)
    scanf("%d", &array[c]);
  printf("Enter a number to search\n");
  scanf("%d", &search);
  for (c = 0; c < n; c++)
   {
    if (array[c] == search)    // If required element is found
    {
      printf("%d is present at location %d.\n", search, c+1);
      break;
    }
   }
  if (c == n)
   printf("%d isn't present in the array.\n", search);
  
  return 0;
 }
 
 Output :
 Enter the number of elements to be entered in array
 5
 Enter 5 integer(s) 
 2
 4
 6
 8
 10
 Enter a number to search 
 10
 10 is present at location 5
 

Linear search C program for multiple occurrences

In the code below we will print all locations at which required element is found and also the number of times it occurs in the list.


 #include <stdio.h>
 int main()
 {
   int array[100], search, c, n, count = 0; 
   printf("Enter number of elements present in array\n");
   scanf("%d",&n);
   printf("Enter %d numbers\n", n);
   for (c = 0; c < <n; c++)
      scanf("%d", &array[c]);
   printf("Enter a number to be searched\n");
   scanf("%d", &search);
   for (c = 0; c < <n; c++) 
   {
      if (array[c] == search)
      {
         printf("%d is present at location %d.\n", search, c+1);
         count++;
      }
   }
   if (count == 0)
      printf("%d isn't present in the array.\n", search);
   else
      printf("%d is present %d times in the array.\n", search, count);
     
   return 0;
}

 Output:
 Enter number of elements present in array
 8
 Enter 8 numbers
 2
 1
 3
 1
 4
 5
 1
 1
 Enter a number to be searched
 1
 1 is present at location 2
 1 is present at location 4
 1 is present at location 7
 1 is present at location 8
 1 is present 4 times in the array
 

C program for linear search using a function

Here is the source code below we will print all locations at which required element is found and also the number of times it occurs in the list for the linear search program using a function using arrays


 #include <stdio.h>
 int main()
 {
   int array[100], search, c, n, count = 0;
   
   printf("Enter the number of elements in the array\n");
   scanf("%d", &n);
   
   printf("Enter %d numbers\n", n);
   
   for (c = 0; c < n; c++)
      scanf("%d", &array[c]);
     
   printf("Enter a number to search\n");
   scanf("%d", &search);
   
   for (c = 0; c < n; c++) 
   {
    if (array[c] == search) 
      {
         printf("%d is present at location %d.\n", search, c+1);
         count++;
      }
   }
   if (count == 0)
      printf("%d isn't present in the array.\n", search);
   else
      printf("%d is present %d time(s)  in the array.\n", search, count);
     
   return 0;
 }
 
 Output :
 Enter the number of elements in the array
 5
 Enter 5 numbers
 2
 4
 6
 8
 10
 Enter a number to search
 8
 8 is present at location 4
 8 is present 1 time(s) in the array
 

Linear search function using pointers

Below is a function for linear search using pointers in c which we can use in the program


  long linear_search(long *p, long n, long find)
  {
   long c;
   for (c = 0; c < n; c++) 
   if (*(p+c) == find)
   return c;
   }
   return -1;
   

The time required to search an element using the algorithm depends on the size of the list. In the best case, it's present at the beginning of the list, in the worst-case, element is present at the end. Its time complexity is O(n).

Post a Comment

0 Comments