3.45 We prove the correctness of BubbleSort (see the solution to Exercise 3.15) by using
induction to establish the following loop invariant of its while loop.
Loop Invariant: After k passes of the while loop, 1 k n, the 1st largest, 2nd largest, … ,
kth largest elements have been properly placed in the last k index positions n – k + 1, n – k +
2, … , n. Moreover, if SwapFlag is .false. after the kth pass, then the entire list has been
3.49 We show that when GeneratePermutations is called with parameter i, it generates all
permutations of the elements T[i], …, T[n], and returns T[i:n] to its state before the
recursive call. We prove this by induction on k = n – i, i = n, …, 1.
Basis Step: k = 0. Clearly true, since GeneratePermutations simply returns, and there is
only one permutation of T[n].
Section 3.5 Establishing Lower Bounds for Problems
3.50 Suppose (π(i), π(i+1)) is an inversion, so that π(i) > π(i+1). Let π* denote the
permutation resulting from π by interchanging π(i) and π(i+1). Suppose first that j > i +1. If