C Program for finding number in Linear search

Linear Search in C

Searching, in normal ways, can be coined as” to find the hidden thing”. In data structure.the searching algorithm is used to find whether a given number is present and if it is present then at what location it occurs.

There are two types of searching algorithm present in data structure through which searching any data become more easy.

  1. Linear search or sequential search
  2. Binary search 
TCS NQT Registration Steps Large
Time ComplexityO(n)
Best CaseO(1)
Worst CaseO(n)
Space ComplexityO(1)
Avg. Comparisons(n+1)/2

Implementation:-

  • Step 1- The list given is the list of elements in the an unsorted array. 
  • Step 2- The element to be searched is called item, as mentioned in the image, so value item, is compared with all the elements of the array starting from 0th element.
  • Step 4- As soon as any index contains the same value, we stop further comparisons and change flag value from 0 to 1
  • Step 4-  If the value is not found until the end of the array then stop and print value not found

Algorithm:-

  1. i=1
  2. if i>n,go to step 7
  3. if A[i]=x,go to step 6
  4. i=i+1
  5. Go to step 2
  6. print element x found at i
  7. print element not found
  8. exit
Linear Search in C Finding a number in Linear Search

C Code:-

#include <stdio.h>  //header files 
int main () //main function
{ 
  int a[10] = {10, 23, 40, 1, 2, 0, 14, 13, 50, 9}; 
  int item, i, flag; 
  
  printf("\nEnter Item which is to be find\n"); 
  scanf("%d",&item); 

  //looping logic
  for (i = 0; i< 10; i++)  
  { 
    // if else condition
     if(a[i] == item) 
     { 
       flag = i+1; 
       break; 
     } 
     else 
       flag = 0; 
   }
 
   if(flag != 0) 
   { 
      //printing the element which is to find
      printf("\nItem found at location %d\n",flag);  
   } 
   else 
   { 
      printf("\nItem not found\n"); 
   } 
   return 0;
}
Output:-
Enter Item  which is to be find
23
Item found at location  2
Enter Item  which is to be find
3
Item not found

More about Linear Search

Pros

  1. Very easy to understand and implement
  2. Quick to write the code

Cons

  1. Time complexity is bad
  2. Binary Search gives better, efficient and faster results
Linear Search in C

Time Complexity
For Linear Search

Best

O(1)

Average

O(n)

Worst

O(n)

Space Complexity

O(1)

Average Comparisons

(n+1)/2

Quiz time

Quiz Time

Question 1. The average number of key comparisons done in a successful sequential search in a list of length it is –

  1. (n-1)/2
  2. Log n
  3. (n+1)/2
  4. (n)/2

(Amazon – Mettl Test)

Just say if you have to find a given element in a sequential search. It can be found in the first try, then 1 comparison is required similarly…total comparisons can be 1+2+…+n = n(n+1)/2

Avg will be n(n+1)/2 divided by n ( total elements) = (n+1)/2

Ans. Option C

Question 2. The worst-case occurred in the linear search algorithm when

  1. The element in the middle of an array
  2. Item present in the last
  3. Item present in the starting
  4. Item has the maximum value

(TCS NQT)

If the element situated at the end of the array, so it takes maximum time to search for that of the element.

Ans. Option B

Data Structures and Algorithm Learn Searching PrepInsta

Searching

Searching algorithms are easy to learn but are really important for college semester exams and companies offering package between 3 – 6 LPA would ask direct searching questions in online test/ interviews.

Summary
Review Date
Reviewed Item
Linear Search
Author Rating
51star1star1star1star1star

Please Login/Signup to comment

3 comments on “C Program for finding number in Linear search”