Sorting Algorithms - #2 Bubble Sort
Tags: programming, beginners, tutorial, algorithms
Published: 2022-01-07 11:22:01

Hi Devs,

In the previous article we have discussed the [**Selection Sort**](https://dev.to/sachin26/sorting-algorithms-1-selection-sort-1imm) algorithm and in this one, we will learn & understand the **Bubble Sort** algorithm.

## Bubble Sort
**Bubble Sort** is the simplest sorting algorithm from the others because its working procedure is very easy to understand.
In this algorithm, we do two things.

1. compare two adjacent elements.
 `Arr[i] > Arr[i+1]` _for ascending order_
 `Arr[i] < Arr[i+1]` _for descending order_

2. and swap them if they are incorrect order (ascending or descending).

in each iteration, each element of the array moves to the end of the array.

let's visually understand this algorithm.


![Bubble Sort](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/d9q3cnw2s0nwqqyfp25t.png)
now, we are going to sort this array in ascending order using the above two steps or bubble sort algorithms.

compare two adjacent elements. and swap them if `Arr[i] > Arr[i+1]`


![Bubble Sort](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8reibpxdn4p54oltjgh1.png)
`5` is less than `9`, it does not satisfy the swapping condition just move forward.


![Bubble Sort](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tvu7br116hnfow3ee0nl.png)
`9` is greater than `2`,it satisfy the swapping condition then swap them.


![Bubble Sort](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vm5xhteooipuamxzfajg.png)
`9` is greater than `7`,it satisfy the swapping condition then swap them.


![Bubble Sort](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/t1lyqspnn9zoyw2gqtz0.png)
`9` is greater than `1`,it satisfy the swapping condition then swap them.

![Bubble Sort](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4mn7mwqoh7gsdk1t9rnv.png)
In the first iteration, we placed one element of the array in its correct position.
The same process will be follow until the last iteration.


after the last iteration,Array looks like this.

![Bubble Sort](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/arakhwoy54g4v2ln0hi6.png)

See the java Solution.

## Java

```java
import java.util.Arrays;
public class Main {
    public static void main(String[] args) {

      int[] arr = new int[]{5,9,2,7,1};
      
      System.out.println("unsorted Array : "+Arrays.toString(arr));

      bubbleSort(arr); 

      System.out.println("sorted Array in ascending order : "+Arrays.toString(arr));
    }
   
   private static void bubbleSort(int[] arr){
      
     for(int i=0; i<arr.length;i++){

       for(int j =0;j<arr.length-i-1;j++){

        if(arr[j] > arr[j+1]){
          swap(arr,j,j+1);
        }
       }

     }
   }

   private static void swap(int[] arr,int i,int j){
     int temp = arr[i];
     arr[i] = arr[j];
     arr[j] = temp;
   }
}


```

> Pros & Cons

- _good for the smallest data sets._

- _its easy to implement._

- _it will take more than n swaps._

- _not require any extra memory._

- _required more time to sort._

> Time Complexity

 _In the worst case, the Time complexity of this algorithm 
 will be **O(n^2)**._
 _bcoz it required two nested loops for comparing & swapping the two 
 adjacent elements._



> Space Complexity

 _Bubble sort does not require any extra memory for sorting._
 then, Space Complexity will be **O(1)**

---
Thank you for reading this article. share this article with your dev friends and save it for the future.