TUTORIAL · Algorithm
Linear Search
This tutorial introduces the Linear Search Algorithm.
On this page
Linear search, also called sequential search, checks array elements in order until it finds the target or reaches the end. It works with unsorted data and is useful when the collection is small, changes often, or does not support efficient indexed lookup. A search that stops at its first match returns that match’s index; a search for every match must continue scanning. If no element matches, an implementation can return -1 as a sentinel. Linear search may take longer than binary search on a large sorted array, but binary search requires sorted data and a suitable access pattern, so neither method is always the right choice.
Linear Search Algorithm
Suppose an unsorted array A contains n elements and the target is X. Start at index 0, compare A[i] with X, and advance i while they differ. For a single-result search, return i immediately on a match; this makes the first matching position the result when duplicates exist. If the scan reaches n without a match, return -1. The valid array indices are 0 through n - 1, so -1 cannot be confused with a successful index. The caller should check for that sentinel before using the result to access the array.
To collect every occurrence, record each matching index and continue until all n elements have been checked. This distinction matters: returning immediately is correct for the first-match task but would miss later duplicates. For an array of length n, either version performs no more than n comparisons. An array with no elements naturally produces -1 for the single-result version and an empty collection for the all-results version.
Linear Search Example
Consider the array (5, 3, 4, 2, 1, 6), whose indices run from 0 to 5. Searching for 5 matches at index 0, so a first-match scan stops after one comparison. Searching for 1 reaches index 4 after checking the earlier elements; this is a later-position example, not a claim about the average case. Searching for 9 checks every element and returns the not-found sentinel -1. If the array instead contains (2, 4, 2, 1, 2) and the target is 2, a first-match search returns index 0, while an all-match search collects indices 0, 2, and 4.
These examples show why a returned index is meaningful only alongside the chosen return convention. The first-match version answers “where is the first occurrence?”; the all-match version answers “which positions contain this value?”
Linear Search Algorithm Implementation
Linear Search Algorithm for Single Match
This self-contained C++ program returns the first matching index or -1 when the target is absent. Its headers are standard C++ headers.
Run this example:
#include <iostream>
int search(const int values[], int length, int target) {
for (int i = 0; i < length; ++i) {
if (values[i] == target) {
return i;
}
}
return -1;
}
int main() {
const int values[] = {2, 4, 0, 1, 9};
const int index = search(values, 5, 1);
std::cout << index << '\n';
return 0;
}
3
For these inputs, search returns the index of the first 1. Change the target to a value not in the array to exercise the -1 return path. Pass the array length explicitly so the function does not read beyond the supplied elements.
Linear Search Algorithm for Multiple Matches
The all-match version uses std::vector<int> to store every matching index. It deliberately does not return early, so repeated values are all included. The following is a separate, self-contained C++ example:
#include <cstddef>
#include <iostream>
#include <vector>
std::vector<int> findAll(const int values[], int length, int target) {
std::vector<int> matches;
for (int i = 0; i < length; ++i) {
if (values[i] == target) {
matches.push_back(i);
}
}
return matches;
}
int main() {
const int values[] = {2, 4, 2, 1, 2};
const std::vector<int> matches = findAll(values, 5, 2);
for (std::size_t i = 0; i < matches.size(); ++i) {
if (i != 0) {
std::cout << ' ';
}
std::cout << matches[i];
}
std::cout << '\n';
return 0;
}
0 2 4
The vector contains indices in scan order. When there is no match, it remains empty; callers can test matches.empty() before displaying or using its contents.
Linear Search Algorithm Complexity
For a first-match search, the best case is O(1): the first element matches. The worst case is O(n): the target is absent or appears at the final index, so the scan checks all n elements. The average number of comparisons is also O(n) when targets are absent or their positions are spread across the array under a reasonable distribution. The exact average depends on assumptions about whether the target is present and how likely each position is; a particular example at index 4 does not establish an average case. An all-match search always checks the whole array, so its time is O(n) regardless of where matches occur.
The first-match scan uses O(1) auxiliary space: it keeps only the loop index and current result. The all-match version uses O(k) additional space to retain k matching indices, with k at most n. If the caller only needs to process matches, it can handle each index during the scan rather than retaining the full collection.