Professional Documents
Culture Documents
Analysis of Bubble Sort
Analysis of Bubble Sort
Loop Invariant
N-1
N-1 Iteration
1
42
2
35
3
12
4
77
5
5
6
101
1
35
2
12
3
42
4
5
5
77
6
101
1
12
3
5
3
35
4
42
5
77
6
101
1
12
2
35
2
5
4
42
5
77
6
101
1
5
2
12
3
35
4
42
5
77
6
101
Outer loop
loop
exitif(to_do = 0)
To bubble a value
index <- 1
loop
exitif(index > to_do)
if(A[index] > A[index + 1]) then
Swap(A[index], A[index + 1])
endif
index <- index + 1
endloop
to_do <- to_do - 1
endloop
endprocedure // Bubblesort
Inner loop
To do N-1 iterations
Loop Invariant
It is a condition or property that is guaranteed to be correct with
each iteration in the loop
Usually used to prove the correctness of the algorithm
N-1
N-1 Iterations
1
42
2
35
3
12
4
77
5
5
6
101
1st
1
35
2
12
3
42
4
5
5
77
6
101
2nd
1
12
3
5
3
35
4
42
5
77
6
101
3rd
1
12
2
35
2
5
4
42
5
77
6
101
4th
1
5
2
12
3
35
4
42
5
77
6
101
5th