what are the number of comparisons and swapping done for bubble sort algorithm for worst case?
Time complexity: On each outer loop, we are comparing n-1 times
for the first time, and n-2 times for second loop.
In the last loop it makes 1 comparison.
So total no of comparisons are :
(n-1)+(n-2)+....+3+2+1 = (n-1)(n/2) = n^2 (roughly)
& total no of element moves in swap is:
in the best case 0 and in the worst case
for first loop we swap n-1 times, second loop n-2 times etc..
so in worst case no of swaps are n^2.
so total time complexity is = (n-1)(n/2) + (n-1)(n/2)
= n^2 + n^2 = 2(n^2) = worst case
Back To Top