- #1
evinda
Gold Member
MHB
- 3,836
- 0
Hello! (Wave)
Could you help me to prove the correctness of the following algorithm?
We could do this using induction, right? (Thinking)
So, for [m] i=1 [/m] we compare [m] A[n-1] [/m] with [m] A[n] [/m] and if it holds that [m] A[n-1]>A[n] [/m] then we swap these two values.
So at the base case do we just say that the last two elements of the array are sorted?
What do we suppose at the induction hypothesis? (Thinking)
Could you help me to prove the correctness of the following algorithm?
Code:
Bubblesort(A){
int i, j;
for i from 1 to n {
for j from n-1 downto i {
if (A[j] > A[j+1])
swap(A[j], A[j+1])
}
}
}
So, for [m] i=1 [/m] we compare [m] A[n-1] [/m] with [m] A[n] [/m] and if it holds that [m] A[n-1]>A[n] [/m] then we swap these two values.
So at the base case do we just say that the last two elements of the array are sorted?
What do we suppose at the induction hypothesis? (Thinking)