Proofs: Bubble sort
These are the human proofs of Bubble sort, at full length.
Bubble sort sorts the array and stops full
This proof shows: correctness, termination.
Each change of is an exchange of two items, so the multiset of the items never changes. We prove that the order is correct and that the loop stops.
Invariant of the outer loop. At each test of line L2:
(B) is sorted in non-decreasing order, and each of its items is at least each item of .
Lemma (one pass). Assume (B) at the start of a pass, with . Let be the array at the start of the pass. Then during the pass, just before the comparison of line L5 for index , is a largest item of . After the pass, with the value of line L8:
- is sorted;
- each item of is at most .
Proof. The first claim is by induction on . For no exchange has happened yet, so . Assume it for . Lines L5 and L6 put the larger of and at . The item was not touched before step , so it is . So after the step, is a largest item of , and the item at is not changed again before the comparison for .
Now the two claims. After the last step of the pass, is a largest item of by the first claim. Consider the steps . By the definition of , line L5 found no pair out of order in these steps, so at each of them, and these positions were not changed after their step. So , which is claim 1. For claim 2, there are two cases. If the pass made an exchange, the last one was at , and it put at a largest item of , which is at least each item now in , because those items are a sub-multiset of minus this one item. If the pass made no exchange, then , and the step found .
Theorem. BUBBLE-SORT stops after at most passes, and after it is sorted.
Proof. Initialization. Line L1 sets . Then is empty, so (B) holds.
Maintenance. Assume (B) and , and let be the value after the pass. The pass changes only , so is unchanged and still sorted, and its items are still at least each item of , since this part holds the same multiset as before. By the lemma, is sorted. Its last item is an item of , so it is at most . So is sorted. Each item of is at most by claim 2 of the lemma, and so at most each item of . Line L8 sets , and (B) holds.
Termination. In a pass, line L7 sets only to values , and line L3 sets it to . So each pass makes smaller by at least 1. It starts at and the loop runs while , so there are at most passes. When the loop stops, . If , then , and (B) says that is sorted and that is at most each of its items, so is sorted. If , the array is empty.
Stability. Line L5 exchanges two items only when the left one is strictly larger. So two equal items are never exchanged. An item moves only by exchanges with its neighbour, so it can pass an equal item only by an exchange with it. So equal items keep their relative order.
Exchanges. Each exchange of line L6 swaps a pair of neighbours. This pair is an inversion, and the exchange changes no other pair of items, so it lowers the number of inversions by exactly 1. The sorted array has none. So the number of exchanges equals the number of inversions of the input.