Open In App

Time and Space Complexity Analysis of Bubble Sort

Last Updated : 18 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

The time complexity of Bubble Sort is O(n^2) in the worst-case scenario and the space complexity of Bubble sort is O(1). Bubble Sort only needs a constant amount of additional space during the sorting process.

Complexity Type Complexity
Time Complexity Best: O(n)
Average: O(n^2)
Worst: O(n^2)
Space Complexity Worst: O(1)

Time Complexity Analysis of Bubble Sort:

Best Case Time Complexity Analysis of Bubble Sort: O(N)

The best case occurs when the array is already sorted. So the number of comparisons required is N-1 and the number of swaps required = 0. Hence the best case complexity is O(N).

Worst Case Time Complexity Analysis of Bubble Sort: O(N2)

The worst-case condition for bubble sort occurs when elements of the array are arranged in decreasing order.
In the worst case, the total number of iterations or passes required to sort a given array is (N-1). where ‘N’ is the number of elements present in the array.

At pass 1:
Number of comparisons = (N-1)
Number of swaps = (N-1)

At pass 2:
Number of comparisons = (N-2)
Number of swaps = (N-2)

At pass 3:
Number of comparisons = (N-3)
Number of swaps = (N-3)
.
.
.

At pass N-1:
Number of comparisons = 1
Number of swaps = 1

Now, calculating total number of comparison required to sort the array
= (N-1) + (N-2) +  (N-3) + . . . 2 + 1
= (N-1)*(N-1+1)/2  { by using sum of N natural Number formula }
= (N * (N-1)) / 2

In worst case, Total number of swaps = Total number of comparison
Total number of comparison (Worst case) = N(N-1)/2
Total number of swaps (Worst case) = N(N-1)/2

So worst case time complexity is O(N2) as N2 is the highest order term.

Average Case Time Complexity Analysis of Bubble Sort: O(N2)

The number of comparisons is constant in Bubble Sort. So in average case, there are O(N2) comparisons. This is because irrespective of the arrangement of elements, the number of comparisons C(N) is same.

For the number of swaps, consider the following points:

  1. If an element is in index I1 but it should be in index I2, then it will take a minimum of (I2-I1) swaps to bring the element to the correct position.
  2. Consider an element E is at a distance of I3 from its position in sorted array. Then the maximum value of I3 will be (N-1) for the edge elements and N/2 for the elements at the middle.

The sum of maximum difference in position across all elements will be:

(N – 1) + (N – 3) + (N – 5) . . . + 0 + . . . + (N-3) + (N-1)
= N x (N – 2) x (1 + 3 + 5 + … + N/2)
= N2 – (2 x N2 / 4)
= N2 – N2 / 2
= N2 / 2

Therefore, in average case the number of comparisons is O(N2)

Space Complexity Analysis of Bubble Sort:

The space complexity of Bubble Sort is O(1). This means that the amount of extra space (memory) required by the algorithm remains constant regardless of the size of the input array being sorted. Bubble Sort only needs a constant amount of additional space to store temporary variables or indices during the sorting process. Therefore, the space complexity of Bubble Sort is considered to be very efficient as it does not depend on the input size and does not require additional space proportional to the input size.


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads