C Program for Linear Search
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.
Linear Search Algorithm
To search for the given element using linear search, follow the below approach:
- Start traversing from the start of the dataset.
- Compare the current element with the key (element to be searched).
- If the element is equal to the key, return index.
- Else, increment the index and repeat the step 2 and 3.
- If we reach the end of the list without finding the element equal to the key, return some value to represent that the element is not found.
C Program to Implement Linear Search
// C program to implement linear search using loop
#include <stdio.h>
int linearSearch(int* arr, int n, int key) {
// Starting the loop and looking for the key in arr
for (int i = 0; i < n; i++) {
// If key is found, return key
if (arr[i] == key) {
return i;
}
}
// If key is not found, return some value to indicate
// end
return -1;
}
int main() {
int arr[] = { 10, 50, 30, 70, 80, 60, 20, 90, 40 };
int n = sizeof(arr) / sizeof(arr[0]);
int key = 30;
// Calling linearSearch() for arr with key = 43
int i = linearSearch(arr, n, key);
// printing result based on value returned by
// linearSearch()
if (i == -1)
printf("Key Not Found");
else
printf("Key Found at Index: %d", i);
return 0;
}
Output
Key Found at Index: 2
Complexity Analysis of Linear Search
- Time Complexity: O(n), where n is the number of elements in the list.
- Auxiliary Space: O(1)
How Linear Search Works?
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.






Recursive Implementation of Linear Search
Just like most of the algorithms, linear search can also be implemented using recursion:
- Define the recursive function that takes an array, its size, and the key as parameters.
- Check if the current last element of the array is equal to the key.
- If it is equal, return the index of it.
- Otherwise, recursively call the linearSearch() with same array but with size decreased by one.
- If the size reaches is 0, return -1 indicating the element is not found.
C Program to Implement Recursive Linear Search
// C Program to implement linear search using recursion
#include <stdio.h>
int linearSearch(int* arr, int n, int key) {
// Base Case: if there are no elements, return -1
if (n == 0)
return -1;
// If the element at (n - 1) index is equal to key,
// return (n - 1)
if (arr[n - 1] == key) {
return n - 1;
}
// If element is not at n - 1, call linear search for
// same array arr but reducing the size by a single
// element
return linearSearch(arr, n - 1, key);
}
int main() {
int arr[] = { 10, 50, 30, 70, 80, 60, 20, 90, 40 };
int n = sizeof(arr) / sizeof(int);
int key = 30;
// Calling linearSearch function
int i = linearSearch(arr, n, key);
if (i == -1)
printf("Key Not Found");
else
printf("Key Found at Index: %d", i);
return 0;
}
Output
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).
Linear Search in C – FAQs
When should I use a linear search?
Use linear search for small lists or when dealing with unsorted data. It is also useful when the simplicity of implementation is a priority.
What is the main drawback of linear search?
The main drawback is its inefficiency with large lists, as it requires checking each element individually, resulting in O(n) time complexity.
How can I improve the performance of linear search?
For sorted lists, consider using more efficient algorithms like binary search. For unsorted lists, if searches are frequent, consider using data structures that support faster search operations (e.g., hash tables).
Can linear search be used for linked lists?
Yes, linear search can be used for linked lists by traversing each node sequentially until the target value is found or the end of the list is reached.