Quick Enquiry Form

×

    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

      python-training-in-chennai

      100% Placement Support

      Live Online & Classroom
      Python Programming

      Discover your best career by learning on-demand Python Programming

      data-science-training-in-chennai

      Real Time Hands-on Training

      Live Online & Classroom
      Data Science Training

      Our training course will give you the required skills..

      machine learning in chennai

      Learn From Industry Experts

      Live Online & Classroom
      Machine Learning Training

      Machine learning is one of the buzzwords recently and this can be attributed…

      rpa-training-in-chennai

      No Cost EMI Option

      Live Online & Classroom
      RPA Training

      Discover your best career by learning on-demand RPA technology…

      software-testing-in-chennai

      Value-Based Certification

      Live Online & Classroom
      Software Testing Course

      Want to get career-oriented Software Testing Training in Chennai Then think about SLA Institute…

      java-training-in-chennai

      Lifetime Career Guidance

      Live Online & Classroom
      Java Training

      Our training course will give you the required skills to be one of the best picks by the IT employers…

      selenium-training-in-chennai

      Flexible Learning Hours

      Live Online & Classroom
      Selenium Testing

      Our training course will give you the required skills to be one of the best picks by the IT employers…

      dotnet-training-in-chennai

      Convenient Training Modes

      Live Online & Classroom
      Dot Net Training

      Discover the great opportunities in Dot Net by practicing on real-life implementation strategies for delivering…

      ccna-training-in-chennai

      Convenient Training Modes

      Live Online & Classroom
      CCNA Training

      The CCNA certification helps you in becoming a sound, well-equipped network engineer…

      php-course-in-chennai

      Advanced Course Curriculum

      Live Online & Classroom
      PHP Training

      Being a language for general purposes, PHP can develop both static and dynamic websites…

      full-stack-developer-training

      Experiential Full Stack Course

      Live Online & Classroom
      Full Stack Development

      Learn Full Stack Developer course from SLA Institute…

      1
      Softlogic-Academy