0

The bubble-sort algorithm (pseudo-code):

Input: Array A[1...n]
for i <- n,...,2 do
    for j <- 2,...,i do
        if A[j - 1] >= A[j] then
            swap the values of A[j-1] and A[j];

I am not sure but my proof seems to work, but is overly convoluted. Could you help me clean it up?

Loop-invariant: After each iteration i, the i - n + 1 greatest elements of A are in the position they would be were A sorted non-descendingly. In the case that array A contains more than one maximal value, let the greatest element be the one with the smallest index of all the possible maximal values.

Induction-basis (i = n): The inner loop iterates over every element of A. Eventually, j points to the greatest element. This value will be swapped until it reaches position i = n, which is the highest position in array A and hence the final position for the greatest element of A.

Induction-step: (i = m -> i = m - 1 for all m > 3): The inner loop iterates over every element of A. Eventually, j points to the greatest element of the ones not yet sorted. This value will be swapped until it reaches position i = m - 1, which is the highest position of the positions not-yet-sorted in array A and hence the final position for the greatest not-yet-sorted element of A.

After the algorithm was fully executed, the remaining element at position 1 is also in its final position because were it not, the element to its right side would not be in its final position, which is a contradiction. Q.E.D.

Niki
  • 738
  • 8
  • 17

1 Answers1

3

I'd be inclined to recast your proof in the following terms: Bubble sort A[1..n]: for i in n..2 for j in 2..i swap A[j - 1], A[j] if they are not already in order

Loop invariant: let P(i) <=> for all k s.t. i < k <= n. A[k] = max(A[1..k])

Base case: initially i = n and the invariant P(n) is trivially satisfied.

Induction step: assuming the invariant holds for some P(m + 1), show that after the inner loop executes, the invariant holds for P(m).

Rafe
  • 5,237
  • 3
  • 23
  • 26
  • Thank you very much, that helped me out a lot. I have a question though: Why did you define i < k instead of i <= k? As far as I understand the variable i refers to the state of the program before each iteration. Doesn't this only proof that A[3..n] is sorted? How would I formally proof something like that? And another question, if you don't mind: I would proof the induction step with a paragraph of text. If I wanted to be really formal, is it right that I would need another induction inside the induction to proof that the inner loop moves the new maximum to its final position? – Niki Jul 11 '18 at 03:52
  • 1
    Hi, glad that was of some help. It's been a while and I probably committed a fence-post error there (is the invariant what is true before the loop body has executed or after?). Either way, the invariant essentially says "everything past this point is a (sorted) suffix of the final result". To prove the induction step, you just need to show that the inner loop moves a maximal element to the end of the unsorted part. – Rafe Jul 12 '18 at 06:57