Searching for an algorithm is basically considered as an essential stage in computer science that includes using a step-by-step procedure for locating a specific piece of data among a large set of data.

In order to finish the procedure, all search algorithms utilize a search key.Â

There are different types of search algorithms available in computer science, and how they are employed determines the performance and efficiency of the data provided (the manner in which the data is being used).

These algorithms are divided into two groups based on the type of search operations they perform.Â

- Sequential Search: This method traverses the list or array consecutively, checking each element. (Same as in the case of a Linear Search.)
- Interval Search: These algorithms were created with the goal of searching sorted data structures. These types of search algorithms are more efficient than Linear Search as they continually target the center of the search structure and divide the search space in half. Example: Binary Search.

## Types of Searching Algorithms

- Linear Search
- Binary Search
- Jump Search
- Interpolation Search
- Exponential Search
- Sublist Search (Searching a linked list inside another list)
- Fibonacci Search
- The Ubiquitous Binary Search

## Linear Search

A Linear Search, which is also popularly known as a sequential search, is a process for finding an item in a list of items. This sort of searching algorithm checks each element of the list one by one until a match is found or the entire list is searched.

In worst-case scenarios, the Linear Search takes linear time and performs at most n comparisons, where n is the list's length.

If each element has an equal chance of being searched, Linear Search has an average case of n+1/2 comparisons, but the average case can be influenced if the search probability for each element differs. Linear Search is rarely feasible as other search algorithms and schemes (such as the binary search algorithm and hash tables) that provide significantly faster search operation for all lists.

The following are the steps involved in implementing Linear Search:

- We must first use a for loop to traverse the array elements.
- In the process of searching an element in a list of elements, we should start by comparing the search element with the current list of elements in each iteration of the loop, and if the elements match, return the index of the relevant array element.
- If the element we're looking for doesn't match, move on to the next one.

Return -1, if there is no match or the search element is not found in the given array.

## What is Linear Search in C Programming?

In C, we perform a Linear Search to see if a number is present in an array. It is also known as sequential search in which we compare each element with the one we're looking for until we find it or when the list runs out.Â

### Algorithm of Linear Search (Approach)

Searching is the method of finding a certain item in a list of items. If the element is found in the list, the process is considered successful, and the location of that element is returned; otherwise, the search is considered unsuccessful.

In Linear Search, the index or search location in the specified array is found. It starts the search by comparing the search key to the array/first list's element. If the first element does not match the search key, the next element will be compared, and so on until the match is discovered or the array ends. If a match is discovered, the index is returned; otherwise, it reaches the end of the array or list, indicating that the search key is not available.

For a better understanding of the Linear search, we are going to see an example.

### Example:Â

Given array = {50, 90, 30, 70, 60};

Assume the search key is 30. Next, scan the array and compare each element to the search key. The array's first element is 50, but 50 is not equal to 30, therefore continue on to the next element. The next element is 90, but it isn't equal to 30, so it's time to move on to the next one. The array's next element is 30, which is the same as the search key 30, thus returning the index of the array's current element.

The above example was the case where the search key was present in the array. Now consider a case where the search key is not present. Letâ€™s assume that the search key is equal to 10. Compare each element in the array with the search element. It fails to match with 50, 90, 30, 70, 60, and eventually reaches the array's conclusion. As a result, return -1 or print element is not present in the array, indicating that the search key is not available.

## Implementing C Program for Linear Search

Code for Linear Search in C program is given below:

#include <stdio.h> void main() {Â Â Â Â Â Â int num; Â Â Â Â int i,Â key, element_found = 0; Â Â Â Â printf("Enter number of elements you would like to take as input: "); Â Â Â Â scanf("%d", &num); Â Â Â Â int arr[num]; Â Â Â Â printf("\nEnter all the elements of your choice:"); Â Â Â Â for (i = 0; i < num; i++) Â Â Â Â { Â Â Â Â Â Â Â Â scanf("%d", &arr[i]); Â Â Â Â } Â Â Â Â printf("\nEnter the key element that you would like to be searched: "); Â Â Â Â scanf("%d", &key); Â Â Â Â /*Â Linear search starts */ Â Â Â Â for (i = 0; i < num ; i++) Â Â Â Â { Â Â Â Â Â Â Â Â if (key == arr[i] ) Â Â Â Â Â Â Â Â { Â Â Â Â Â Â Â Â Â Â Â Â element_found = 1; Â Â Â Â Â Â Â Â Â Â Â Â break; Â Â Â Â Â Â Â Â } Â Â Â Â } Â Â Â Â if (element_found == 1) Â Â Â Â Â Â Â Â printf("we got the element at index %d",i+1); Â Â Â Â else Â Â Â Â Â Â Â Â printf("we havenâ€™t got element at any index in the array\n"); } |

#### Output:

### Explanation of Code:Â

- In Linear Search, we look for an element or value in an array by traversing it from the beginning to the end until the required element or value is discovered.
- The array is searched progressively, and if the key element to be searched is found in the array, the position is returned; otherwise, -1 is returned.
- We haven't created a function particularly for Linear Search in this C program; instead, we can check for the presence of an element in an array in the main function.
- We traverse the array starting at the 0th index and increasing in order of index, breaking the loop there and printing the element's position in the array, but if the element requested is not present in the array, we simply display "Element is not present in the array."
- If we had written a separate Linear Search function and the element could not be found in the array, we would have returned -1 or print â€śElement is not present in arrayâ€ť indicating that the element was not present.

## Linear Search for Multiple Occurrences

The code below prints all the locations where the needed element can be found, as well as the number of times it appears in the list of the program.

#include <stdio.h> int main() { Â Â Â int arr[100], key, i, no, count = 0, search; Â Â Â printf("Enter number of elements you would like to take as input\n"); Â Â Â scanf("%d", &no); Â Â Â printf("Enter %d numbers\n", no); Â Â Â for (i= 0; i < no; i++) Â Â Â Â Â Â scanf("%d", &arr[i]);Â Â Â Â Â printf("Enter a number that you would like to search\n"); Â Â Â scanf("%d", &search);Â Â Â Â Â for (i = 0; i < no; i++) { Â Â Â Â Â Â if (arr[i] == search) { Â Â Â Â Â Â Â Â Â printf("%d is present at position %d.\n", search, i+1); Â Â Â Â Â Â Â Â Â count++; Â Â Â Â Â Â } Â Â Â } Â Â Â if (count == 0) Â Â Â Â Â Â printf("%d key is not present in the array.\n", search); Â Â Â else Â Â Â Â Â Â printf("%d is present %d times in the array.\n", search, count);Â Â Â Â Â Â return 0; } |

#### Output:

## Linear Search Using a Function

Code for implementing Linear Search in C program by using function is provided below:

#include <stdio.h> int linear_search_function(int a[], int n, int key) { Â Â Â for (long i = 0 ;i < n ; i++ ) { Â Â Â Â Â Â if (a[i] == key) Â Â Â Â Â Â Â Â Â return i; Â Â Â } Â Â Â return -1; } int main() { Â Â int arr[100], key, k, n, key_position; Â Â printf("Enter number of elements in the array\n"); Â Â scanf("%d", &n); Â Â printf("Enter %d integer(s)\n", n); Â Â for (k= 0; k < n; k++) Â Â Â Â scanf("%d", &arr[k]); Â Â printf("Enter a number you would like to search in the array\n"); Â Â scanf("%d", &key); Â Â key_position = linear_search_function(arr, n, key); Â Â Â if (key_position == -1) Â Â Â Â Â Â printf("%d isn't present in the array.\n", key); Â Â Â else Â Â Â Â Â Â printf("%d is present at location %d.\n", key, key_position+1); Â Â Â return 0; } |

#### Output:

## Linear Search in C Using Recursion

The recursive function is a function that contains a call to itself. Recursion is a method of defining the recursive function. It allows us to break down a big problem into easily manageable single basic situations. Divide and conquer is also a power and a popular computer programming strategy that is widely used for programming.

#include <stdio.h> int linear_search_with_recursion(int arr[], int value, int index, int n) { Â Â Â Â int position = 0; Â Â Â Â if(index >= n) Â Â Â Â { Â Â Â Â Â Â Â Â return 0; Â Â Â Â }Â Â Â Â Â else if (arr[index] == value) Â Â Â Â { Â Â Â Â Â Â Â Â position = index + 1; Â Â Â Â Â Â Â Â return position; Â Â Â Â }Â Â Â Â Â else Â Â Â Â { Â Â Â Â Â Â Â Â return linear_search_with_recursion(arr, value, index+1, n); Â Â Â Â } Â Â Â Â return position; }Â int main() { Â Â Â Â int n, value, position, m = 0, arr[100]; Â Â Â Â printf("Enter the total elements in the array that you would like to take:"); Â Â Â Â scanf("%d", &n); Â Â Â Â printf("Enter the elements of the array:\n"); Â Â Â Â for (int i = 0; i < n; i++) Â Â Â Â { Â Â Â Â Â Â Â Â scanf("%d", &arr[i]); Â Â Â Â } Â Â Â printf("Enter the element you would like to search in the array: "); Â Â Â Â scanf("%d", &value); Â Â Â Â position = linear_search_with_recursion(arr, value, 0, n); Â Â Â Â if (position != 0) Â Â Â Â { Â Â Â Â Â Â Â Â printf("Element found at position %d ", position); Â Â Â Â } Â Â Â Â else Â Â Â Â { Â Â Â Â Â Â Â Â printf("Element not found in the array"); Â Â Â Â } Â Â Â Â return 0; } |

#### Output:

### Explanation of Code:

- In Linear Search, we look for an element or value in an array by traversing it from the beginning until the element or value we want is discovered.
- The array is searched progressively, and if the key element to be searched is found in the array, the position is returned; otherwise, "Element not found in the array" is printed.
- In this C program, we've written a recursive function called linear_search_with_recursion() that takes four input arguments and returns the position of an element in an array that the user is searching.
- If the first element is found, the index is returned immediately.
- If it is not the first member of the array, we reduce the size of the array by 1 by removing the first element, which means the array size will be reduced by 1 when Linear Search-with_recursion() is called a second time (n-1). This will continue until the element has been discovered.

## Time Complexity of Linear Search in C Program

The time complexity of an algorithm is nothing but the amount of time it takes to run as an algorthm for the whole input. The number of operations to be performed by the algorithm is indicated by the length of input. In this case, it would not consider the algorithm's overall execution time. Instead, when the number of operations in an algorithm increases or decreases, it will provide data on the variation (increase or reduction) in execution time.

Let's look at the complexity of Linear Search in the best, average, and worst-case scenarios.Â

## Â Â Â Â Â Â Case |
## Â Â Â Time Complexity |

Â Â Â Â Â Â Best Case |
Â Â Â Â Â O(1) |

Â Â Â Â Â Â Average Case |
Â Â Â Â Â O(n) |

Â Â Â Â Â Â Worst case |
Â Â Â Â Â O(n) |

- Best Case Complexity in Linear Search: In Linear Search, best case occurs when the element we're looking for is at the top of the array. The time complexity of Linear Search in the best-case scenario is O (1).
- Average Case Complexity: In Linear Search, the average case time complexity is O(n).
- Worst Case Complexity: The worst case in Linear Search occurs when the element we're seeking for is at the end of the array. When the target element is not present in the specified array, the worst-case scenario is that we must traverse the entire array. The time complexity of Linear Search in the worst-case scenario is O(n).

Because each member in the array is only compared once, Linear Search has an O(n) time complexity.

## Space Complexity of Linear Search in C Program

According to their input size, space complexity is essentially a measurement of the total amount of memory that algorithms or operations require. It is the amount of memory space required by the algorithm to solve a specific instance of the computational problem as a function of the input characteristics.

As we know that the amount of extra data in the Linear Search is fixed, the space complexity is always O(1).

Advance your career as a MEAN stack developer with theÂ Full Stack Web Developer - MEAN Stack Master's Program. Enroll now!

## Conclusion

In C, Linear Search involves traversing a list or array sequentially to see if an entry is there. The goal is to begin traversing the array and compare items of the array one by one, starting with the first element, until a match is discovered or the array's end is reached.Â

We hope this article helped you gain knowledge of the C program for Linear Search. In this article, we discussed the uses of a Linear Search, time and space complexity, and approach to implement Linear Search in a code. We also learned how we can use it in our software development projects.

To know more about the Linear Search in C Program, you can enroll in the Post-Graduate Program in Full-Stack Web Development offered by Simplilearn in collaboration with Caltech CTME. This Web Development course is a descriptive online bootcamp that includes 25 projects, a capstone project, and interactive online classes. In addition to Linear Search in C Program and other related concepts, the course also details everything you need to become a full-stack technologist and accelerate your career as a software developer.

Simplilearn also offers free online skill-up courses in several domains, from data science and business analytics to software development, AI, and machine learning. You can take up any of these free courses to upgrade your skills and advance your career.