0

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,

  1. a[0...i-1] is sorted

  2. all entries in a[i..n-1] are larger than or equal to the entries in a[0..i-1].

Badro Niaimi
  • 959
  • 1
  • 14
  • 28
Akash Tadwai
  • 100
  • 1
  • 9

1 Answers1

0

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].
kaya3
  • 47,440
  • 4
  • 68
  • 97
  • from the implementation of bubble sort in [this](https://www.geeksforgeeks.org/bubble-sort/) How can I write the following as a predicate, Element that I am currently looking at is bigger than previous **j-1** elements. – Akash Tadwai Nov 26 '19 at 08:59
  • Likewise, using a universal quantifier over the indices in the subarray. – kaya3 Nov 26 '19 at 15:14