Bubble Sort is the most simplest sorting technique among all sorting algorithms. It compares the element adjacent to it and keeps on rearranging it until all the elements are sorted. Sounds tough. No worries, let us simplify with the below example :
Let us say we have 5 numbers in random order and we have to sort them in ascending order.
Now, let us apply bubble sort algorithm to sort the numbers and at the end of every pass we will see the highest number sits at the top of the list. It is like a bubble, where the largest bubble sits on top of the list.
By the end of first pass we will see that the largest element i.e. 8 will sit at the end of the list.
As said in the definition, we need to start comparing the first two elements. So , the first element is 7 and the 2nd element is 4. And 7 is greater than 4. So , we swap them as we want the list to be in ascending order and the new list becomes,
Next, we compare 2nd with 3rd element and find, 7 is less than 8. So, we make no change as they are already in ascending order.
Again , we compare 3rd element with the 4th element and find, 8 is greater than 1. So, we swap them.
Then , we compare 4th and 5th element and find 8 is greater than 6. So, we swap them.
And as said the largest numberi.e. 8 is on the top of the list i.e. In 5th position.
In the 1st Pass the list iterated - 4 times
In the second pass we will apply bubble sort algorithm only till 4th location as we already know that the element(i.e. 8) in the 5th location is already sorted.
Now , if we consider the elements only till 4th location .
The largest element is 7. And at the end of 2nd pass, 7 will sit on the 4th location.
So , in the second pass we start comparing the elements starting from the first location. And, currently the 1st element is 4 and the 2nd element is 7. And, 4 is less than 7, which means they are already in ascending order. So, we don't make any change,
Then , we try comparing 2nd and 3rd element and find that 7 is greater than 1. So , we swap them and the new list becomes.
Again , in 3rd iteration, we compare 3rd and 4th element and find 7 is greater than 6. So , we swap them and the new list becomes,
And as said the second largest elementi.e. 7 has sat just before 8.
In the 2nd Pass the list iterated - 3 times
Again , in the third pass we will compare the elements till 3rd location as the elements in 4th and 5th location are already sorted.
Now , if we consider the elements only till 3rd location.
The largest element is 6. And at the end of 3rd pass, 6 will be in 3rd position. Where it actually is.
So , currently the 1st element is 4 and the 2nd element is 1. And, 4 is greater than 1, which means we have to swap 4 and 1 to get them in ascending order.
Then , we try comparing 2nd and 3rd element and find that 4 is greater than 6. So, we don't make any change and the list remains as is.
In the 3rd Pass the list iterated - 2 times
Again , in the fourth pass we will compare the elements till 2nd location as the elements in 3rd, 4th and 5th location are already sorted.
And since its 4th pass, we start comparing the elements starting from the first location.
So , currently the 1st element is 1 and the 2nd element is 4. And, 1 is less than 1, which means we do not have to swap them as they are already in ascending order.
In the 4th Pass the list iterated - 1 time
And we have the list sorted in ascending order .