Time and Space Complexity For Bubble Sort

TIME COMPLEXITY

  • Time complexity is defined as the amount of time taken by a specific algorithm to process, as a function of the length of the input.
  • It basically measures the time taken to execute every particular statement of code in an algorithm. It's not going to examine the net run time of the algorithm.

For Best Case - It occurs when there is no sorting required, i.e. the array is already sorted. The time complexity of bubble sort for this case is O(n).

For Average Case - It occurs when the array elements are in jumbled order that is not properly ascending and not properly descending. The time complexity of bubble sort for this case is O(n^2).

For Worst Case - It occurs when the array elements are required to be sorted in reverse order. That happens when you have to sort the elements of the array in ascending order, but its elements are in descending order. The time complexity of bubble sort for this case is O(n^2).

SPACE COMPLEXITY

  • The space complexity of bubble sort is O(1). It is because, an extra variable is required for swapping, in Bubble Sort.
Discussion
Good explanation

1

Great one, thank you for sharing!

8

2