This is the algorithm:
// Precondition: n > 0
l = -1;
r = n;
while (l+1 != r) {
m = (l+r)/2;
// I && m == (l+r)/2
if (a[m] <= x) {
l = m;
} else {
r = m;
}
}
// Postcondition: -1 <= l < n
I have done some research and narrowed the invariant down to if x is in a[0 .. n-1] then a[l] <= x < a[r]
.
I have no idea how to progress from there though. The precondition seems too broad, so I'm having trouble showing that P -> I
.
Any help is extremely appreciated. These are the logic rules that can be used to prove the algorithm's correctness: