I'm trying to prove the correctness of the Selection sort, in which I should use only the mathematical predicate logic to prove program correctness, I'm finding it difficult to write the English statements given below as Predicates and proceed through the proof of correctness following Inference rules,
void sort(int [] a, int n) {
for (int i=0; i<n-1; i++) {
int best = i;
for (int j=i; j<n; j++) {
if (a[j] < a[best]) {
best = j;
}
}
swap a[i] and a[best];
}
}
The statements I have to write in Predicates are,
a[0...i-1]
is sorted
all entries in a[i..n-1]
are larger than or equal to the entries in a[0..i-1]
.
A statement about a subarray like a[0...i-1]
is really a statement about all elements in that subarray, so you will need to use universal quantifiers to translate it into a statement about individual members.
To say that a subarray is sorted, we can say something like: "for any pair of indices j < k in the subarray, the values at those indices are in order."
For all 0 <= j < i-1, for all j < k <= i-1, arr[j] <= arr[k].
The second property is already written as a statement about "all entries" of two subarrays, but let's make it more explicit: "for any pair of indices j in the first subarray and k in the second subarray, the value in the first subarray is less than or equal to the value in the second subarray."
For all 0 <= j <= i-1, for all i <= k <= n-1, arr[j] <= arr[k].