Loop Invariant
Rule of Thumb.
- Characterize the state after an arbitrary number of iterations, either in English of in a diagram.
- Introduce a variable to record the subscript of each boundary expected to change independently during the iteration.
- where
- finished means that the array elements contain the correct final values,
- ? means that the array elements may or may not contain the correct final values,
- k contains the subscript of the first element in the ? section, and
- A[0..m] is a rearrangement of the original values A[0..m]