Optimized Bubble Sort
For Complete YouTube Video: Click Here
In this class, we will try to understand Optimized Bubble Sort.
We discussed the algorithm of bubble sort in our previous class.
Optimized Bubble Sort
The image below is the algorithm.
The lines marked are the changes to the original bubble sort algorithm.
In the above algorithm, we have added a new variable called swapped.
The logic behind the use of a swapped variable is to identify whether any swaps are made or not.
After completing all the iterations of the inner “for” loop, all the elements are sorted if the swaps are not made.
The if condition after the inner for loop is true means the elements are in the sorted order.
The break will get executed, and the algorithm’s execution will be stopped.