I was trying to understand the Data Structure and different algorithm, then i got confused to measure the Bubble sort time complexity.
for (c = 0; c < ( n
Best case: This time complexity can occur if the array is already sorted. That means no swapping occurs and only 1 iteration of n elements will be there.
So time complexity is O(n).
Worst case: This time complexity can occur if the array is already sorted but is descending order.
In 1st iteration, number of comparison = n-1
In 2nd iteration, number of comparison = n-2
.......................................................................
.......................................................................
.......................................................................
In (n-2)th iteration, number of comparison = 2
In (n-1)th iteration, number of comparison = 1
for n elements total number of iteration= n-1
Total number of comparison S = (n-1)+ (n-2) +........ + 2 + 1
We can write this also S = 1 + 2 + ........+(n-2) + (n-1)
................................................................................................................................
2S = n + n + ......... + n + n .... [Adding both line]
2S = n(n-1) ..... [as total no of iteration = n-1]
S = n(n-1)/2
In polynomial function, highest order of n is considered as time complexity.
So, Time Complexity is O(n^2)