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

## 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
8. exit ## C Code:-

```#include <stdio.h>  //header files
int main () //main function
{
int a = {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
{
}
return 0;
}
```
```Output:-
Enter Item  which is to be find
23
Item found at location  2
Enter Item  which is to be find
3
```

### Pros

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

### Cons

2. Binary Search gives better, efficient and faster results O(1)

O(n)

O(n)

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 –

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 ## 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
5     ### 3 comments on “C Program for finding number in Linear search”

• Jaspreet 0 0
• Care

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 0 0
• Anugya Saxena

thanks for these study material in summary with pictorial representation. 1 0