Correctness of Bubblesort

Bubblesort is a popular sorting algorithm. It works by repeatedly swapping adjacent elements that are out of order.

  BUBBLESORT(A)
  1 for i = 1 to A.length
  2   for j = A.length downto i + 1
  3       if A[j] < A[j − 1]
  4           swap A[j] and A[j − 1]
  1. Let denote the output of BUBBLESORT(A). To prove that BUBBLESORT is correct, we need to prove that it terminates and that where . What else must be proved to show that BUBBLESORT actually sorts?
  2. State precisely a loop invariant for the for loop in lines 2–4, and prove that this loop invariant holds. Your proof should use the structure of the loop invariant proof presented in this chapter.
  3. Using the termination condition of the loop invariant proved in part (2), state a loop invariant for the for loop in lines 1–4 that will allow you to prove inequality (2.3). Your proof should use the structure of the loop invariant proof presented in this chapter.
  4. What is the worst-case running time of bubblesort? How does it compare to the running time of insertion sort?

1. Required Proof of Correctnesses

We also need to prove that consists of the elements originally in but in sorted order.


2. Loop Invariant for Inner Loop

The loop invariant for the for loop in lines 2–4 can be stated as follows:

At the start of each iteration of the for loop, the subarray consists of the elements originally in before entering the loop but possibly in a different order and the first element is the smallest among them.

And here is how the three necessary properties hold for the loop invariant:

Initialization: Initially the subarray contains only the last element and this is the smallest element of the subarray.

Maintenance: In every step we compare with and make the smallest among them. So, after the iteration, the length of the subarray increases by one and the first element is the smallest of the subarray.

Termination: The loop terminates when . At that point also the length of the subarray increases by one and the first element is the smallest of the subarray as we swap with .


3. Loop Invariant for the Outer Loop

The loop invariant for the for loop in lines 1–4 can be stated as follows:

At the start of each iteration of the for loop, the subarray consists of the elements that are smaller than the elements in the subarray in sorted order.

And here is how the three necessary properties hold for the loop invariant:

Initialization: Initially the subarray is empty and trivially this is the smallest element of the subarray.

Maintenance: From part (2), after the execution of the inner loop, will be the smallest element of the subarray . And in the beginning of the outer loop, consists of elements that are smaller than the elements of , in sorted order. So, after the execution of the outer loop, subarray will consists of elements that are smaller than the elements of , in sorted order.

Termination: The loop terminates when . At that point the array will consists of all elements in sorted order.


4. Running Time of Bubblesort

  • At worst-case, bubblesort will iterate over the whole array for each element, i.e. for each element bubble sort will perform comparisons and swaps. Therefore, worst-case running time of bubblesort is .
  • Although insertion also runs at worst-case time, the number of assignments (swaps) performed in bubblesort is way more than that of insertion sort. So, the constant factors in the running time will be much larger for bubblesort compared to that of insertion sort. This means, for the same input size, insertion sort will run faster than bubblesort.
If you have any question or suggestion or you have found any error in this solution, please leave a comment below.