Problem

Correctness of bubblesortBubblesort is a popular sorting algorithm. It works by repeatedly...

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 length[A]

2                     do for j ←length[A] downto i + 1

3                              do if A[j] < A[j - 1]

4                                    then exchange A[j]↔A[j - 1]

a. Let A' denote the output of BUBBLESORT(A). To prove that BUBBLESORT is correct, we need to prove that it terminates and that

where n = length[A]. What else must be proved to show that BUBBLESORT actually sorts?

The next two parts will prove inequality (2.3).


b. 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.


c. 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 will allow you to prove inequality (2.3). Your proof should.use the structure of the loop invariant proof presented in this chapter.


d. What is the worst-case running time of bubblesort? How does it compare to the running time of insertion sort?

Step-by-Step Solution

Request Professional Solution

Request Solution!

We need at least 10 more requests to produce the solution.

0 / 10 have requested this problem solution

The more requests, the faster the answer.

Request! (Login Required)


All students who have requested the solution will be notified once they are available.
Add your Solution
Textbook Solutions and Answers Search
Solutions For Problems in Chapter 2.P