Quick Enquiry Form

×

    EnquiryEnquire Now

    Quick Enquiry

      How to Implement Linear Search in C?

      Linear Search in C
      Blog

      How to Implement Linear Search in C?

      To determine whether or not a number is present in an array, you can use a linear search in C. If it is, the question becomes where exactly it is present. A sequential search is another name for this method. It is simple, and the way it operates is as follows: we check each element with the element to find until we either discover it or the list runs out. Searching in a linear fashion for numerous occurrences while utilizing a function

      Linear search program in C

      #include <stdio.h>
      int main()
      {
        int array[100], search, c, n;
        printf(“Enter number of elements 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
      6
      Enter 6 numbers
      7
      3
      4
      1
      2
      5
      Enter the number to search
      1
      1 is present at position 4

      Program that does a linear search in C for multiple occurrences

      The following line of code will print out the total number of occurrences of the necessary element in the list, as well as all of the locations where it has been found.

      #include <stdio.h>
      int main()
      {
         int array[100], search, c, n, count = 0;
         
         printf(“Enter number of elements 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 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 times in the array.\n”, search, count);
           
         return 0;
      }

      Output
      Enter the number of elements
      8
      Enter 6 numbers
      7
      3
      4
      3
      2
      5
      3
      6
      Enter the number to search
      3
      3 is present at position 2
      3 is present at position 4
      3 is present at position 7
      3 is present 3 times in an array

      C program for linear search using a function

      #include <stdio.h>
       
      long linear_search(long [], long, long);
       
      int main()
      {
         long array[100], search, c, n, position;
       
         printf(“Input number of elements in array\n”);
         scanf(“%ld”, &n);
       
         printf(“Input %d numbers\n”, n);
       
         for (c = 0; c < n; c++)
            scanf(“%ld”, &array[c]);
       
         printf(“Input a number to search\n”);
         scanf(“%ld”, &search);
       
         position = linear_search(array, n, search);
       
         if (position == -1)
            printf(“%d isn’t present in the array.\n”, search);
         else
            printf(“%d is present at location %d.\n”, search, position+1);
       
         return 0;
      }
       
      long linear_search(long a[], long n, long find) {
         long c;
       
         for (c = 0 ;c < n ; c++ ) {
            if (a[c] == find)
               return c;
         }
       
         return -1;
      }

      Linear search in C using pointers

      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;
        }

      Conclusion

      The duration of time necessary to search for an element by utilizing the technique is reliant upon the volume of the list. The best-case scenario has it appearing at the start of the list, while the worst-case scenario has it appearing at the very end of the list. The degree of difficulty in terms of time is O(n).

      For Online & Offline Training

      Have Queries? Ask our Experts

      +91 88707 67784 Available 24x7 for your queries

      Quick Enquiry Form

        1
        Softlogic-Academy