❓ Question
Correctness of bubble sort
Bubble sort is a popular but inefficient sorting algorithm. It works by repeatedly swapping adjacent elements that are out of order. The procedure BUBBLESORT sorts array A[1:n]
- Let $A'$ denotes the array A after BUBBLESORT(A, n) is executed. To prove that BUBBLESORT is correct, you need to prove that it terminates and that $A’[1] ≤ A’[2] ≤ …. ≤ A’[n]$……………… (2.5) In order to show that BUBBLESORT actually sorts, what else do you need to prove?
- 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.
- Using the termination condition of the loop invariant proved in part (b), state a loop invariant for the for loop in lines 1-4 that allows you to prove inequality (2.5). Your proof should use the structure of the loop-invariant proof presented in this chapter.
- What is the worst-case running time of BUBBLESORT? How does it compare with the running time of INSERTION-SORT?