You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
In Bubble Sort, sorting takes place by stepping through all the elements one-by-one and comparing it with the adjacent element and swapping them if required. With each iteration the largest element in the given array, is shifted towards the last place or the highest index in the array.
Steps in Bubble Sort
Starting with the first element(index = 0), compare the current element with the next element of the array.
If the current element is greater than the next element of the array, swap them.
If the current element is less than the next element, move to the next element. Repeat Step 1.
It includes two loops :
Outer loop decides the number of traversals the algorithm makes over the array i.e. $n-1$.
Inner loop decides upto where the algorithm parses the array in each traversal i.e. $n-i-1$, where $i$ is the iterator for the outer loop.
In nth traversal, the nth largest element is pushed to the right side of the array while comparing two elements at a time.
Time Complexity
Bubble Sort makes $n-1$ comparisons will be done in the first traversal, $n-2$ in second traversal, $n-3$ in third traversal and so on. So the total number of comparisons will be,
$$
(n-1) + (n-2) + (n-3) + ..... + 1 = (n(n+1))/2 = O(n^2)
$$
Hence the time complexity of Bubble Sort is $O(n^2)$.
The main advantage of Bubble Sort is the simplicity of the algorithm. The space complexity for Bubble Sort is $O(1)$, because only a single additional memory space is required i.e. for temp variable.
Also, the best case time complexity will be $O(n)$, it is when the
list is already sorted.Following are the Time and Space complexity for the Bubble Sort
algorithm.
For $i = 5$,
Inner loop doesn't execute even once i.e.
$j\ in\ range (0,-1) => False$
Thus the sorted array is returned.
Optimised Bubble Sort
In Optimized Bubble Sort, sorting takes place in the same way as in Bubble Sort except the fact that if no swaps take place in any iteration the outer loop breaks and the sorted array is returned.
In the above example itself, if we add the flag variable swapped it will be false after the 4th iteration and sorted array will be returned after four interations only.