You are on page 1of 5

BUBBLE SORT

Round 1:
32, 51, 27, 85, 66, 23, 13, 57
32, 27, 51, 85, 66, 23, 13, 57
32, 27, 51, 85, 66, 23, 13, 57
32, 27, 51, 66, 85, 23, 13, 57
32, 27, 51, 66, 23, 85, 13, 57
32, 27, 51, 66, 23, 13, 85, 57
32, 27, 51, 66, 23, 13, 57, 85 # of comparisons = 7
Round 2:
27, 32, 51, 66, 23, 13, 57, 85
27, 32, 51, 66, 23, 13, 57, 85
27, 32, 51, 66, 23, 13, 57, 85
27, 32, 51, 23, 66, 13, 57, 85
27, 32, 51, 23, 13, 66, 57, 85
27, 32, 51, 23, 13, 57, 66, 85 # of comparisons = 6
Round 3:
27, 32, 51, 23, 13, 57, 66, 85
27, 32, 51, 23, 13, 57, 66, 85
27, 32, 23, 51, 13, 57, 66, 85
27, 32, 23, 13, 51, 57, 66, 85
27, 32, 23, 13, 51, 57, 66, 85 # of comparisons = 5

Round 4:
27, 32, 23, 13, 51, 57, 66, 85
27, 23, 32, 13, 51, 57, 66, 85
27, 23, 13, 32, 51, 57, 66, 85
27, 23, 13, 32, 51, 57, 66, 85 # of comparisons = 4
Round 5:
23, 27, 13, 32, 51, 57, 66, 85
23, 13, 27, 32, 51, 57, 66, 85
23, 13, 27, 32, 51, 57, 66, 85 # of comparisons = 3

Round 6:
13, 23, 27, 32, 51, 57, 66, 85
13, 23, 27, 32, 51, 57, 66, 85 # of comparisons = 2

Round 7:
13, 23, 27, 32, 51, 57, 66, 85 # of comparisons = 1
Proving that the complexity of bubble sort algorithm (i.e.
the sum of the sequence of comparisons for the rounds
given by n-1, n-2, n-3, n-4, n-5, …, 2 is O(n2).
Since 1 + 2 + 3 + … + n = n(n+1)/2
Then, 1, 2 + 3 + 4 + … + n – 5 + n – 4 + n-3 + n-2 + n-1
=[(n-1)(n)]/2 =[(n-1)n]/2
= [n2-n]/2
= n2/2 –n/2
= O(n2) - O(n)
~ O(n2)
Thus, the complexity of a bubble sort (ie the sum of the
sequence of comparisons for the rounds given by n-1, n-
2, n-3, n-4, n-5, …, 4, 3, 2) is O(n2).

You might also like