I am new to the idea of invariants and hope to find more information about it.
It can make an algorithm or loop show a high level of certainty / confidence that the code is correct, such as in the case of a binary search, as described in the book Programming Pearls, 2nd Ed.
Are invariants true at most part of the loop and can be untrue at some part? That is, in the middle of the loop:
while a <= b
if (...)
// Point A
else
// Point B
end
// Point C
end
is it true that at Point A or Point B, something can change that cause the invariants to break, and it depends on the next iteration to "correct the invariants"? The same with Point C, something can change that can cause the invariants to break?
Maybe Point C is more likely the place where the invariants may break, as it is getting ready for the next iteration? So if it is a for-loop:
for(i = 0; i < n; i++) {
// ...
}
then the i++
is a place where the invariant may break? I am looking if there is any rule that says what time may invariants not hold true, or should they always hold true?
It seems for binary search, the invariants are always true, while for mergesort or quicksort, the invariants are only true at a certain point, such as for mergesort, the array is divided into 2, and it is in sorted order, but this invariant is only true only after the code recursively calls itself to sort both the left and right subarrays. But this is recursion, so I don't know if there is any difference between that or just a loop.