Loop invariant of linear search
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
A loop invariant is a property that holds true before each iteration of a loop and after the loop terminates. For linear search, the loop invariant is: "the target element is not in the portion of the array already examined." This property is used to formally prove that the algorithm is correct — it finds the target if it exists and correctly reports absence if it does not. Understanding loop invariants is fundamental to algorithm analysis and is commonly tested in computer science courses and interviews.
Linear Search Algorithm
The algorithm checks each element sequentially. If it finds the target, it returns the index. If it exhausts the array, it returns -1.
The Loop Invariant
Statement: At the start of iteration i, the target is not present in arr[0..i-1].
The three parts of a loop invariant proof:
Initialization (Before the First Iteration)
Before the loop starts, i = 0. The subarray arr[0..-1] is empty. The target is trivially not in an empty subarray. The invariant holds.
Maintenance (Each Iteration Preserves the Invariant)
Assume the invariant holds at the start of iteration i: the target is not in arr[0..i-1]. During iteration i:
- If
arr[i] == target, the function returnsi— the loop terminates with the correct answer - If
arr[i] != target, the target is not inarr[0..i]. At the start of the next iteration (i+1), the invariant holds forarr[0..i]
Termination (After the Loop Ends)
The loop ends when i == len(arr). By the invariant, the target is not in arr[0..len(arr)-1], which is the entire array. Returning -1 is correct.
Formal Pseudocode with Invariant
Python Implementation with Assertions
The assertions verify the invariant at each step. In production code, remove them — they serve as a proof aid.
Comparing with Binary Search Invariant
Linear search and binary search have different loop invariants:
| Property | Linear Search | Binary Search |
| Invariant | Target not in arr[0..i-1] | If target exists, it is in arr[lo..hi] |
| Requires sorted input | No | Yes |
| Time complexity | O(n) | O(log n) |
| Narrows search space by | 1 element per iteration | Half per iteration |
Loop Invariants for Other Algorithms
Loop invariants are used to prove correctness of many algorithms:
Common Pitfalls
- Confusing the invariant with the termination condition: The loop invariant describes what is true throughout the loop, not when the loop stops. The termination condition (
i == n) combined with the invariant proves the post-condition. - Not checking all three parts: A valid loop invariant proof requires initialization, maintenance, and termination. Skipping any part leaves the proof incomplete and may hide bugs.
- Choosing a trivially true invariant: "True" is a valid invariant but proves nothing about correctness. The invariant must be strong enough to imply the post-condition when combined with the loop termination condition.
- Forgetting the early return case: In linear search, the loop may terminate early via
return i. The invariant proof for maintenance must handle both the "found" case (early return) and the "not found" case (loop continues). - Off-by-one in the invariant range: The invariant says target is not in
arr[0..i-1], notarr[0..i]. At the start of iterationi, elementarr[i]has not been checked yet. Getting this boundary wrong invalidates the proof.
Summary
- The linear search loop invariant is: "the target is not in
arr[0..i-1]" at the start of iterationi - Prove correctness via three steps: initialization (true before loop), maintenance (preserved each iteration), termination (implies correct result)
- The invariant combined with the termination condition (
i == n) proves that returning -1 is correct - Loop invariants are the standard technique for formally proving algorithm correctness
- Linear search invariant narrows the unsearched space by one element per iteration; binary search narrows by half
- Practice identifying invariants for insertion sort, selection sort, and binary search to build intuition

