1. Gyanastra
  2. Algorithms
  3. Searching Algorithms
  4. Linear Search Algorithm and Implementation
Free lesson, no account needed 5 min read 7 practice questions after it
Asked in past papers1 time
  • GATE CSE2025

Algorithms · Searching Algorithms

Linear Search Algorithm and Implementation

Overview

Linear search, also called sequential search, finds a target value in a list by checking elements one after the other in a linear fashion. It is the simplest search method and, unlike binary search, works on both sorted and unsorted data.

Key concepts

  • Linear search (sequential search): Sequentially checks each element of the list for the target value until a match is found, or until all elements have been searched.
  • Works on both sorted and unsorted data. In contrast, binary search can be applied only on sorted data.
  • Problem statement: Given a list LL of nn elements with values/records L0,L1,L2,…,Ln−1L_0, L_1, L_2, \dots, L_{n-1} (each element has an index from 00 to n−1n-1) and a target value TT, find the index of TT in LL.
    If TT is present, return the index where it is present.
  • C implementation parameters: The function linearsearch takes three parameters:
    • Parameter 1: the array — the address of the array is sent and captured in ARR (ARR points to the beginning address of the array).
    • Parameter 2: the size of the array.
    • Parameter 3: the target — the element to be searched for.

How it works

  1. Initially start with the zeroth element, i.e. set i=0i = 0.
  2. If L[i]==TL[i] == T (the current element equals the target), the search terminates there itself and returns that ii.
  3. Otherwise, increment ii by 1 (go to the next element).
  4. If i<ni < n, go back to step 2 (take the next element and keep checking against TT). Wherever TT is found, stop there; otherwise go to the next element and check again.

Pseudocode:

i = 0
while i < n:
    if L[i] == T:
        return i
    i = i + 1

C implementation:

int linearsearch(int ARR[], int size, int target) {
    for (int index = 0; index < size; index++) {
        if (ARR[index] == target)
            return index;
    }
}

The loop starts from the 0th element (index = 0), runs while index < size, and at each step checks whether ARR[index] == target; if not equal, index++ moves to the next element.

Formulas

  • L[i]==TL[i] == T : the current element equals the target; the search returns ii.
  • i=i+1i = i + 1 : move to the next element when no match is found.
  • Loop condition: i<ni < n, where nn is the size of the list.

Worked examples

Array ARR = [7, 5, 9, 2, 10], size = 5, target = 2. ARR points to the beginning address of this array.

StepindexARR[index]Compare with target 2Action
1077 == 2? Noindex++
2155 == 2? Noindex++
3299 == 2? Noindex++
4322 == 2? Yesreturn index

The function returns 3 (the index where 2 is present).

Comparison

Linear searchBinary search
Data requirementWorks on both sorted and unsorted dataCan be applied only on sorted data

Revision summary

  • Linear search checks elements one by one until a match is found or all elements are searched.
  • Start at index 0; return the index as soon as L[i]==TL[i] == T; otherwise increment ii and continue while i<ni < n.
  • It works on both sorted and unsorted data; binary search needs sorted data.
  • In the C implementation, the array parameter receives the base address of the array; size and target are passed separately.
  • In the worked example (array [7, 5, 9, 2, 10], target 2), the returned index is 3.

Previous-year exam insights

  • Linear search on n elements performs at most n comparisons (worst case, target absent or last) and at least 1 comparison (best case, target at index 0); the average number of comparisons for a successful search with uniform position probability is (n+1)/2, and for an unsuccessful search it is n.
    Asked inGATE 2025
  • Linear search runs in O(n)O(n) time overall (Θ(n)\Theta(n) worst case, Θ(1)\Theta(1) best case) and Θ(1)\Theta(1) auxiliary space; it needs no preprocessing and no sorted order, so it is the simplest option when data is unsorted or when the list is a linked list without random access.
  • Binary search requires sorted data and random access;
    it runs in O(log⁡n)O(\log n) time, and its recurrence is T(n) = T(n/2) + O(1)O(1), giving Θ(log⁡n)\Theta(\log n) comparisons in the worst case.
  • Finding the maximum or minimum of n elements by scanning requires exactly n−1 comparisons; finding an element that is not the largest can be done with fewer, since it suffices to compare only until some element is found smaller than another.
    Asked inGATE 2025
  • For a successful linear search, if the target is known to be present, the expected index under uniform distribution is (n−1)/2, so the expected number of comparisons is (n+1)/2; if each element has probability pip_{i} of being the target, the expected comparisons are Σ i·pip_{i} (1-indexed).
  • Linear search returns the first (smallest) index of the target when duplicates exist; to find all occurrences or the last occurrence, the scan must continue to the end of the array instead of returning early.
Pages 1–2 of 2