ABSTRACT
A loop invariant is a property that remains true before and after each execution of a loop’s body. It is the primary tool used to prove the correctness of iterative algorithms through mathematical induction.
1. The Three-Step Proof Process
To prove an algorithm is correct using an invariant, you must follow this logical flow:
- State the Invariant: Define the property precisely in terms of the loop variables and iterations ().
- Prove the Invariant (Induction):
- Base Case: Show it is true before the loop starts ().
- Inductive Step: Show that if it’s true after iterations, it remains true after the iteration.
- Prove Correctness: Show that the invariant, combined with the loop termination condition, logically leads to the desired solution.
2. Selection Sort (MinSort) Example
Selection sort works by repeatedly finding the minimum element from the unsorted portion and moving it to the front.

The Invariants (after iterations)
- Sorted-ness: The first elements are in non-decreasing order.
- Minimality: The first elements are the smallest elements of the entire array.
Proof Sketch
- Base Case (): The first elements are vacuously true; no elements exist to be out of order or “not the smallest.”
- Inductive Step: Assume the first are sorted and smallest. In iteration , the algorithm finds the minimum of the remaining elements and places it at index . Since this new element is the previous elements (by minimality) but all remaining elements, the first elements are now sorted and smallest.
- Termination: After iterations, the first elements are sorted and smallest. This forces the element to be the largest, meaning the entire array is sorted.
3. Binary Search Decision Example
Binary search is a Decision Algorithm (returns TRUE/FALSE). To prove it correct, we use a Bi-Directional Proof.

Claim 1: If the algorithm returns TRUE, then is in the list.
- Proof: The algorithm only returns TRUE if it encounters a line where
target == list[mid]. By definition, is in the list.
Claim 2: If is in the list, then the algorithm returns TRUE.
This requires the Loop Invariant: (where and are the current search boundaries).
- Base Case: Initially . Since the list is sorted and is in the list, it must be that .
- Inductive Step:
- If , we move to . Since was and now , the invariant holds.
- If , we move to . Since was and now , the invariant holds.
- Termination: When the loop ends (), the invariant implies . Therefore , and the algorithm returns TRUE.
4. Key Definitions
| Term | Definition |
|---|---|
| Vacuously True | A statement that is true because its antecedent cannot be satisfied (e.g., “All elements in an empty set are purple”). |
| Decision Problem | A problem with a simple YES/NO (True/False) answer. |
| Iteration Variable () | A counter representing how many times the loop body has fully executed. |