Linear Search
1Introduction
This lecture explains the procedure, correctness, and complexity of linear search, which compares array elements in order from the beginning. For an arbitrary array, when the algorithm uses only equality comparisons and assumes neither sorted order nor an auxiliary lookup structure, a comparison with one element cannot eliminate other elements collectively. Each element must therefore be examined individually.
2Problem statement and terminology
Let
be an array of length , and let be a target value. If some index satisfies , the algorithm returns the smallest such index. If no such index exists, it reports failure.
Linear search compares with the target in this order and terminates at the first match. The number of comparisons is the number of evaluations of the equality .
3Procedure
For in order, perform the following steps.
- Evaluate .
- If the equality holds, return and terminate.
- Otherwise, proceed to the next index.
If no index produces a match, report failure. This procedure does not depend on whether the array is sorted.
4Correctness by a loop invariant
A
4.1Initialization
Immediately before the first comparison, . No element has been compared, so the invariant holds over the empty set of preceding indices.
4.2Maintenance
Assume that the invariant holds immediately before the comparison at index . If , no smaller index contains a match, so is the smallest index satisfying the specification. If , none of equals . The invariant therefore holds immediately before the comparison at index .
4.3Termination
If the algorithm terminates at a match, the maintenance argument shows that the returned index is the first matching position. If it reaches without a match, the invariant shows that none of equals . Reporting failure is therefore correct.
Initialization, maintenance, and termination together prove that linear search satisfies its specification.
5Complexity
Assume . In the best case, the target equals the first element . The algorithm performs one comparison, so its best-case running time is .
If the target occurs only at the final element or does not occur in the array, the algorithm performs comparisons. Its worst-case running time is therefore . Apart from its index and return value, it uses no storage proportional to the input length, so its auxiliary-space complexity is .
When , the algorithm performs no comparisons and reports failure immediately.
6Applicability and connection to binary search
Linear search requires only sequential access to elements and equality comparison; it does not require sorted order. In a sorted array, however, order information can eliminate multiple candidates at once. The next lecture explains binary search, which uses this property.
data/lecture/information/algorithm/search/binary-search.lecture.n.md7Summary
- Linear search compares array elements with the target in order from the beginning.
- Initialization, maintenance, and termination of the loop invariant prove that the algorithm correctly returns the first matching position or reports failure.
- Its best-case running time is , its worst-case running time is , and its auxiliary-space complexity is .