- Prepare
All Platforms Programming Aptitude Syllabus Interview Preparation Interview Exp. Off Campus - Prime Video
- Prime Mock

- Interview Experience
- Prime VideoNew
- Prime Mock
- Discussion Forum
- Nano Degree
- Prime Video
- Prime Mock

# 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.**

**Linear search or sequential search****Binary search**

Time Complexity | O(n) |

Best Case | O(1) |

Worst Case | O(n) |

Space Complexity | O(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
, as mentioned in the image, so value**item**, is compared with all the elements of the array starting from 0th element.**item** - Step 4- As soon as any index contains the same value, we stop further comparisons and change
value from 0 to 1**flag** - Step 4- If the value is not found until the end of the array then stop and print value not found

**Algorithm:-**

- i=1
- if i>n,go to step 7
- if A[i]=x,go to step 6
- i=i+1
- Go to step 2
- print element x found at i
- print element not found
- exit

**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

- Very easy to understand and implement
- Quick to write the code

### Cons

- Time complexity is bad
- Binary Search gives better, efficient and faster results

## 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

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

- (n-1)/2
- Log n
- (n+1)/2
- (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

- The element in the middle of an array
- Item present in the last
- Item present in the starting
- 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

### Asked in the following exams/interviews

## 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.**

awesome

nice

Really helpful

Thanks a ton, for the appreciation jaspreet, you must visit our Linked List section in the Data Structure Page, you will find some good content there too – https://prepinsta.com/data-structures/#linked

nice

thanks for these study material in summary with pictorial representation.