C Program for Linear Search
Last Updated :
23 Jul, 2025
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:
- 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
// 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;
}
OutputKey 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
// 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;
}
OutputKey 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).
Explore
C Basics
Arrays & Strings
Pointers and Structures
Memory Management
File & Error Handling
Advanced Concepts