Hi,
I have the following pseudo-code for Bubble Sort, the correctness of which I'd like to prove via induction.
Code:
BubbleSort(input: Array A[1…n], integer n)
{
for i = n-1 to 1
{
for j = 1 to i
{
if A[j] > A[j+1]
swap (A,j,j+1)
}
}
}
I am not really sure how to go about it as the fact that the outer loop runs from n-1 to 1 somewhat perplexes me. Should the induction's basis still be i=1, where i denotes the number of iterations the outer loop has run?
Furthermore, would it be correct to say that at the end of the ith iteration A[i+1..n] is sorted, or would it be, rather, A[i..n]?
I'd appreciate some guidance with this.