The two are independent
An algorithm can be partially correct and never stop, or terminate promptly with a wrong answer. Neither proof implies the other.
Partial correctness says the output is right if the algorithm stops. Termination says it stops. Loop invariants establish the first and a decreasing bounded quantity establishes the second.
Core ideas
This is “Loop Invariants and Program Correctness” in Induction and Recursion applied to algorithms, and the split is what makes the proofs short.
An algorithm can be partially correct and never stop, or terminate promptly with a wrong answer. Neither proof implies the other.
Naming the preserved property is more informative than describing what the loop does, and it is what closes the argument.
A quantity that strictly decreases on every iteration and cannot decrease forever. Usually an index or a remaining size.
The material
The two obligations, both discharged on three algorithms.
An algorithm is partially correct when, if it terminates, its output satisfies the specification. It is totally correct when it is partially correct and terminates on every valid input.
Initialisation. Before the first iteration the set is empty and the claim is vacuous. Maintenance. The iteration returns if aᵢ = x, so reaching the next iteration means x ≠ aᵢ, extending the invariant. Termination. If the loop finishes, the invariant says x is in none of a₁ … aₙ, so returning 0 is correct; if it returns i, then aᵢ = x.
Termination holds because n − i strictly decreases and is a non-negative integer.
The outer-loop invariant is that a[1..j−1] holds the original first j−1 elements in sorted order. Both halves are needed: "sorted" alone would be satisfied by an algorithm that replaced the array with zeros, and the specification says the output must be a permutation of the input.
Termination: the outer loop runs n − 1 times, and the inner loop decreases i each iteration with i ≥ 0 enforced by its own condition.
The invariant is gcd(a, b) = gcd(a₀, b₀), preserved because gcd(a,b) = gcd(b, a mod b). Termination holds because b strictly decreases and is a non-negative integer. At exit b = 0 and gcd(a, 0) = a, which the invariant identifies as the answer.
That invariant is the whole proof and it is one line, which is characteristic: a good invariant names the quantity the loop preserves, and the correctness then follows from the exit condition.
From n, repeat: if n is even, halve it; otherwise replace it by 3n + 1; stop at 1. It terminates for every starting value ever tested — beyond 10²⁰ — and no proof is known that it always does. Partial correctness is trivial; termination is an open problem.
That example separates the two obligations cleanly. It also previews “Decidability and the Halting Problem”: there is no general procedure that decides termination for arbitrary programs, which is why each one needs its own argument.
Interactive
Counts from running, not from quoting
Method
Invariant, then measure.
Precondition and postcondition. Without them there is nothing to prove.
Test it at exit first: the invariant plus the negated loop condition must give the postcondition. If it does not, strengthen it.
True before the first iteration, preserved by the body. This is base case and inductive step.
For termination. An index, a range size, a remaining count — strictly decreasing and bounded below.
Number Theory and Cryptography's algorithm, proved.
The invariant was chosen to be exactly what the exit condition needs. That is the technique: work backwards from the postcondition, substitute the loop variable for its final value, and the invariant usually appears.
Write a loop computing the sum of an array, state its invariant, and check that the invariant plus the exit condition gives exactly "the sum of all n elements". If it needs an extra sentence, strengthen the invariant.
Something to change about this lesson? Write it down, tick it off when it is handled, and export the list when you want to act on it. Kept in this browser only: nothing is sent anywhere, and nobody is notified.
No recommendations yet for this lesson.