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 of elements with values/records (each element has an index from to ) and a target value , find the index of in .
If is present, return the index where it is present. - C implementation parameters: The function
linearsearchtakes three parameters:- Parameter 1: the array — the address of the array is sent and captured in
ARR(ARRpoints to the beginning address of the array). - Parameter 2: the size of the array.
- Parameter 3: the target — the element to be searched for.
- Parameter 1: the array — the address of the array is sent and captured in
How it works
- Initially start with the zeroth element, i.e. set .
- If (the current element equals the target), the search terminates there itself and returns that .
- Otherwise, increment by 1 (go to the next element).
- If , go back to step 2 (take the next element and keep checking against ). Wherever 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
- : the current element equals the target; the search returns .
- : move to the next element when no match is found.
- Loop condition: , where 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.
| Step | index | ARR[index] | Compare with target 2 | Action |
|---|---|---|---|---|
| 1 | 0 | 7 | 7 == 2? No | index++ |
| 2 | 1 | 5 | 5 == 2? No | index++ |
| 3 | 2 | 9 | 9 == 2? No | index++ |
| 4 | 3 | 2 | 2 == 2? Yes | return index |
The function returns 3 (the index where 2 is present).
Comparison
| Linear search | Binary search | |
|---|---|---|
| Data requirement | Works on both sorted and unsorted data | Can 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 ; otherwise increment and continue while .
- 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 time overall ( worst case, best case) and 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 time, and its recurrence is T(n) = T(n/2) + , giving 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 of being the target, the expected comparisons are Σ 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.