Linear Search in C

Linear Search in C
Satyam Chaudhary
Data Structure Feb 07, 2024

In Searching there are two popular methods which are Linear Search and Binary Search . In this blog article, we will explore the concept of linear search in the C programming language. Linear search is a simple but basic algorithm used to find a specific element in a collection of data. Whether you're a beginner or an experienced programmer, understanding how linear search works and how to implement it in C can greatly improve your problem solving skills.

So let's dive into the world of linear search and discover its importance in C programming.

What is Linear Search?

Linear search, also known as sequential search, is a direct algorithm used to find a specific element in a collection. This method involves sequentially scanning each element until a match is found or the entire dataset is traversed.

Importance in data structures

  1. Simplicity: Linear search is a straightforward and easy-to-understand algorithm that is essential for learning and basic understanding of data structures.

  2. Unordered Lists: Excels at searching unordered lists and provides a simple solution without relying on ordered layouts.

  3. Direct Implementation: The simplicity of the algorithm lends itself to easy implementation, especially for scenarios where a quick and simple search is sufficient.

  4. Resource efficiency for small data sets: Linear search can be more resource efficient for small datasets or when the target is close to the start, avoiding the overhead of more complex algorithms.

  5. Baseline Comparison: Serves as a basis for comparing the effectiveness of other search algorithms and helps to understand trade-offs in more advanced techniques.

How Linear Search Works?

  1. Start from the beginning: Begin the search from the first element in the list or array.

  2. Compare element: Compare the current element with the target value you're searching for.

  3. Match found: If a match is found, return the index of the element.

  4. Move to the next: If no match is found, move to the next element in the list.

  5. Repeat: Repeat steps 2-4 until either a match is found or the end of the list is reached.

  6. End of list: If the end of the list is reached without finding a match, conclude that the element is not present in the list.

Linear search has a time complexity of O(n), where 'n' is the number of elements in the list.

Time Complexity Analysis

The time complexity of linear search is O(n), where 'n' represents the number of elements in the collection. This linear time complexity means that the execution time increases linearly with the size of the input.

Linear Search Program in C

Output: Element Found

Fig 1:Linear Search Output when "Element found"

Output: Element not Found

Fig 2:Linear Search Output when "Element not found"

Code Explanation

  1. Declaration and Initialization

    An array a of size 50 is declared to store integers.

    Variables size, j, ele, and found are declared. size holds the size of the array, j is a loop counter, ele is the element to be searched, and found is a flag to track whether the element is found.

  2. Input

    The user is prompted to enter the size of the array.

    The elements of the array are then inputted.

  3. Search

    The user is prompted to enter the element (ele) whose location is to be found.

    A for loop iterates through the array elements to check if any element matches the user-provided element (ele).

    If a match is found (a[j] == ele), the loop sets found to 1, prints the index j where the element was found, and breaks out of the loop.

    If no match is found, the loop completes, and found remains 0.

  4. Output

    If found is still 0 after the loop, it means the element was not found, and "Not found" is printed.

    If found is 1, the index of the found element is printed.

This Program can also be done using function click here for Linear Search Program using Function

Advantages of linear search

  • Linear search is simple to understand and implement.

  • It is effective for searching unordered collections.

Disadvantages of linear search

  • Linear search becomes inefficient as dataset size increases.

  • May not be the best choice for searching sorted arrays.

Scenarios where linear search is effective

  • Small datasets Linear search works well for small collections.

  • Unordered Lists: It is advantageous when the order of the elements does not matter.

Situations to avoid using linear search

  • Large datasets: More efficient algorithms should be considered for large datasets.

  • Sorted Collections: Binary search can be a better alternative for sorted datasets.


In summary, linear search provides a basic understanding of search algorithms with their simplicity and applicability in certain scenarios. the linear search algorithm in C is a simple but effective method to find the target value in a given array. By looping through each element in the array and comparing it to the target value, we can determine whether the value exists or not.

However, it is important to note that linear search has a time complexity of O(n), which means that as the size of the array increases, the time required to search increases proportionally. Therefore, for larger data sets, alternative search algorithms such as binary search may be more efficient. Nevertheless, linear search remains a fundamental concept in computer science and serves as a building block for more advanced search algorithms.

Main Banner Image Credit:

Linear Search
C Programming
Data Structure

Satyam Chaudhary

Satyam is a brilliant Engineering Undergraduate and proud Indian. He is passoinate towards web development and acquiring new skills.

He help students in understanding the concepts with a correct approach and solve their academic problems.

We are here to clear your doubts with an easy step by step approach.