Bubble Sort is a simple sorting algorithm.

In this Sorting, each pair of adjacent elements are compared with each other and are swapped with each other if they are not in order. (Order is decided based on whether the elements are to be sorted in ascending or descending order).

e.g. int []x = {12,11,23,45,21,24,53,28};

Suppose array x has to be sorted in ascending order.Consider a pair of adjacent elements (12,11). 12 > 11, hence they are not in order as in ascending order lower index element has to be less than or equal to higher index element. Hence, 12 and 11 are swapped with each other to bring them in order.

Suppose array x has to be sorted in descending order.Consider a pair of adjacent elements (12,11).In descending order lower index element has to be greater than or equal to higher index element. 12 > 11, hence they are in order and do not require swapping of each other.

let’s take an example with smaller length array.

Logic to Sort in Ascending Order:Bubble Sort

int X[] = {12,11,23,6};

Iteration1:
———–
{12,11,23,6} //12 and 11 are swapped
{11,12,23,6} //12 and 23 are not swapped as 12 < 23
{11,12,23,6} //23 and 6 are swapped

result : {11,12,6,23}

In the first Iteration, the largest element will be decided.

Iteration 2:
———–
{11,12,6,23} //11 and 12 are not swapped
{11,12,6,23} //12 and 6 are swapped

result : {11,6,12,23}

In the Second Iteration, the second largest element will be decided.

Iteration 3:
————
{11,6,12,23} //11 and 6 are swapped

result : {6,11,12,23}

In the third Iteration, the third largest element will be decided.

Fourth Number(Last Number here) will automatically fall in place.

Program to Sort in Ascending Order: Bubble Sort

Points to Notice in the below Program:
I. Each time outer loop runs , the number of times inner loop runs reduces.
II. Make Sure inner loop runs at max (length-1) times, otherwise it can throw ArrayIndexOutOfBoundsException.
III. Please not that inner loop does not run n times for iteration.

package com.masterjavatutorial;
public class Test {

	public static void main(String args[]) {

		int []x = {12,11,23,45,21,24,53,28};

		System.out.println("#####Printing Elements Before Sorting######");
		for(int x1 : x){
		   System.out.print(x1+" ");
		}
                //Sorting in the Ascending Order begins
		for(int i=0; i < x.length;i++){
			for(int j=0; j < (x.length-1-i);j++){
				//Swapping logic 
				if(x[j] > x[j+1]){
				    int temp = x[j+1];
				    x[j+1] = x[j];
				    x[j] = temp;
				}
				
			}
		}
		//Sorting in the Ascending Order finishes
		System.out.println();
		System.out.println("#####Printing Elements After Sorting######");
		for(int x1 : x){
		   System.out.print(x1+" ");
		}
		

	}
	
}

Output of the above Program is as below:

#####Printing Elements Before Sorting######
12 11 23 45 21 24 53 28 
#####Printing Elements After Sorting######
11 12 21 23 24 28 45 53 

What if the Above array has to be sorted in Descending Order

As we order is decided based on whether it is to be in ascending order or descending order. Accordingly two adjacent elements are compared and swapped.

So, only part which changes is comparision and swapping part.

For Ascending order :

if(x[j] > x[j+1]){
  int temp = x[j+1];
  x[j+1] = x[j];
  x[j] = temp;
}

For Descending Order :

if(x[j] < x[j+1]){
  int temp = x[j+1];
  x[j+1] = x[j];
  x[j] = temp;
}

Time Complexity of Bubble Sort

Bubble Sort takes O(n^2) time as there are two loop running.