Proofs: Quick Search algorithm
These are the human proofs of Quick Search algorithm, at full length.
Quick Search outputs exactly the valid shifts full
This proof shows: correctness, termination.
Claim. For a text and a pattern , QUICK-SEARCH stops and outputs exactly the shifts , , with , in increasing order.
For , line L2 outputs every shift, which is correct. Let . Since line L4 visits in increasing order, the last write for is its largest , so after line L4
Lemma (safe shift). Let and . No shift with and is valid.
Proof. Let with , so . The text position lies in the window at and faces the pattern position , with . If were valid, then , so is in the set defining , and , a contradiction.
Theorem. The claim holds.
Proof. Invariant at line L6: the output is the increasing list of the valid shifts smaller than . It holds for . In an iteration with , lines L7 and L8 end with exactly when the window equals , so line L9 outputs exactly when it is valid, after all smaller ones. If , line L10 stops, and every valid shift, being at most , has been output. Otherwise , line L11 reads an existing character, and by the lemma no valid shift lies strictly between and the new , so the invariant holds. Each iteration increases by at least 1, so the loop ends after at most iterations, either at line L10 or with ; in both cases all valid shifts have been output.