![]() |
VOOZH | about |
Linear Search is a sequential searching algorithm in C that is used to find an element in a list. Linear Search compares each element of the list with the key till the element is found or we reach the end of the list.
Example
Input: arr = {10, 50, 30, 70, 80, 60, 20, 90, 40}, key: 30
Output: Key Found at Index: 2
Explanation: Start from index 0, compare each element with the key (30). When index 2 is reached, the element (30) matches the target value.
To search for the given element using linear search, follow the below approach:
Key Found at Index: 2
The working of linear search is very simple. Let's take an array arr = {10, 50, 30, 70, 80, 60, 20, 90, 40} and the key as 30.
Just like most of the algorithms, linear search can also be implemented using recursion:
Key Found at Index: 2
Time Complexity: O(n), where n is the number of elements in the list.
Auxiliary Space: O(n)
Note: If the compiler performs tail call optimization, then Auxiliary Space for recursive approach can be reduced to O(1).