Learnerslesson
   JAVA   
  SPRING  
  SPRINGBOOT  
 HIBERNATE 
  HADOOP  
   HIVE   
   ALGORITHMS   
   PYTHON   
   GO   
   KOTLIN   
   C#   
   RUBY   
   C++   
   HTML   
   CSS   
   JAVA SCRIPT   
   JQUERY   




BUBBLE SORT


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 :


Example :


Let us say we have 5 numbers in random order and we have to sort them in ascending order.

java_Collections

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.


1st Pass

By the end of first pass we will see that the largest element i.e. 8 will sit at the end of the list.


  1. 1st Iteration

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,

java_Collections

  1. 2nd Iteration

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.

java_Collections

  1. 3rd Iteration

Again , we compare 3rd element with the 4th element and find, 8 is greater than 1. So, we swap them.

java_Collections

  1. 4th Iteration

Then , we compare 4th and 5th element and find 8 is greater than 6. So, we swap them.

java_Collections

Note : Even though we have come to the end of the list but we can see the list is not completely sorted. That is because we have completed just one pass. In order to get the list completely sorted, we have to complete all 4 passes.

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


2nd Pass

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 .

java_Collections

The largest element is 7. And at the end of 2nd pass, 7 will sit on the 4th location.


  1. 1st Iteration

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,

java_Collections

  1. 2nd Iteration

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.

java_Collections

  1. 3rd Iteration

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,

java_Collections

Note : We did not run the 4th Iteration as we know that the largest element is 8 which is sitting in the last location in pass 1.


And as said the second largest elementi.e. 7 has sat just before 8.


In the 2nd Pass the list iterated - 3 times


3rd Pass

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.

java_Collections

The largest element is 6. And at the end of 3rd pass, 6 will be in 3rd position. Where it actually is.


  1. 1st Iteration

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.

java_Collections

  1. 2nd Iteration

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.

java_Collections

In the 3rd Pass the list iterated - 2 times


4th Pass

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.

java_Collections

And since its 4th pass, we start comparing the elements starting from the first location.


  1. 1st Iteration

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.

java_Collections

In the 4th Pass the list iterated - 1 time


And we have the list sorted in ascending order .

java_Collections