So now we are going to analyze what is the time complexity of bubble sort. So here as you saw that this was my worst case scenario when I required the maximum number of comparisons since all my elements were out of order. Now if you look carefully, in my first iteration I did four number of comparisons. So since had five elements here, in the first step I did four comparisons. So this implies that in the first step I did a total of n minus one comparisons. Now similarly in the second iteration I performed three comparisons. So that means I did five minus two number of comparisons, that would be n minus two. Similarly for the third iteration I did n minus three comparisons here, n minus four comparisons and so on till my last step when I did exactly one comparison to see whether all my elements are in order. So now what is the total time required for this the total number of comparisons? My total number of comparisons can be written by this expression t n is equal to one plus two plus three,
so on plus n minus two till n minus one. So t n can be written as one plus two till n minus one. Now, if you look at this expression carefully you would see that this is nothing but the sum of all the natural numbers starting from one till n minus one. You know that for natural numbers the sum of the first natural numbers can be written as n into n plus one by two. Here my capital n will be replaced by n minus one. Since I need to find the sum of first n minus one natural numbers, this can be then written as n minus one into n minus one plus one which gives me n divided by two. On solving this expression I would get half of n square minus n. Now if you look at this expression carefully you would see that here in comparison to n square n will always be a smaller number. So I can conveniently ignore this. Moreover, this half outside of this bracket is a constant which can also be ignored. So this gives me an expression that t n is of the order n square. So basically the time complexity of bubble sort will then become theta of n square. So finally we conclude that it requires order n square number of comparisons for bubble sort to perform.
Bubble sort's time complexity is being analyzed
Worst case scenario is when all elements are out of order
The total number of comparisons can be written as
This expression is the sum of all natural numbers from to
The sum of the first n natural numbers can be written as
Therefore, tn can be written as
This simplifies to , which is of the order
Hence, the time complexity of bubble sort is theta of
Bubble sort requires order number of comparisons to perform
Great! So, as discussed, bubble sort has a time complexity of (), where N is the number of elements in the array. In first iteration, it does comparisons ; in the second iteration it does comparisons; in third, it does comparisons. This continues for a total of iterations. So, the total number of comparisons effectively become the sum of first natural numbers.
In the next video, you will see sorting of a deck of six cards using the algorithm you just learnt about. Let’s see how it’s done.
The video reinforces your takeaways from this lesson on bubble sort. The time complexity calculated from the actual number of steps required comes out to be the same as that discussed by Aishwarya. In the next segment, you will move to the next sorting algorithm — Selection sort.