VOOZH about

URL: https://www.geeksforgeeks.org/c/c-c-program-for-linear-search/

⇱ C Program for Linear Search - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

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.

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


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


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).

Comment
Article Tags: