What is Bubble Sort?
Bubble sort is a simple algorithm which compares the first element of the array to the next one. If the current element of the array is numerically greater than the next one, the elements are swapped. Likewise, the algorithm will traverse the entire element of the array.
In this tutorial, we will create a JAVA program to implement Bubble Sort. Check the output of the code that will help you understand the program logic.
Here is how the process works graphically
import java.util.*;
public class BubbleSort
{
public static void main(String[] args)
{
int arr[]=new int[10],n,i;
Scanner sc=new Scanner(System.in);
System.out.println("Enter the number to store and do bubble sort:");
n=sc.nextInt();
System.out.println("Enter "+n+ " elements do bubble sort:");
for(i=0;i<n;i++)
arr[i]=sc.nextInt();
System.out.println("---Array BEFORE Bubble Sort---");
printArray(arr);
bubbleSort(arr);//sorting array elements using bubble sort
System.out.println("---Array AFTER Bubble Sort---");
printArray(arr);
}
static void bubbleSort(int[] array)
{
int n = array.length;
int temp = 0;
for(int i=0; i < n; i++) // Looping through the array length
{ System.out.println("Sort Pass Number "+(i+1));
for(int j=1; j < (n-i); j++)
{
System.out.println("Comparing "+ array[j-1]+ " and " + array[j]);
if(array[j-1] > array[j])
{
//swap elements
temp = array[j-1];
array[j-1] = array[j];
array[j] = temp;
System.out.println(array[j] + " is greater than " + array[j-1]);
System.out.println("Swapping Elements: New Array After Swap");
printArray(array);
}
}
}
}
static void printArray(int[] array)
{
for(int i=0; i < array.length; i++)
{
System.out.print(array[i] + " ");
}
System.out.println();
}
}
public class BubbleSort
{
public static void main(String[] args)
{
int arr[]=new int[10],n,i;
Scanner sc=new Scanner(System.in);
System.out.println("Enter the number to store and do bubble sort:");
n=sc.nextInt();
System.out.println("Enter "+n+ " elements do bubble sort:");
for(i=0;i<n;i++)
arr[i]=sc.nextInt();
System.out.println("---Array BEFORE Bubble Sort---");
printArray(arr);
bubbleSort(arr);//sorting array elements using bubble sort
System.out.println("---Array AFTER Bubble Sort---");
printArray(arr);
}
static void bubbleSort(int[] array)
{
int n = array.length;
int temp = 0;
for(int i=0; i < n; i++) // Looping through the array length
{ System.out.println("Sort Pass Number "+(i+1));
for(int j=1; j < (n-i); j++)
{
System.out.println("Comparing "+ array[j-1]+ " and " + array[j]);
if(array[j-1] > array[j])
{
//swap elements
temp = array[j-1];
array[j-1] = array[j];
array[j] = temp;
System.out.println(array[j] + " is greater than " + array[j-1]);
System.out.println("Swapping Elements: New Array After Swap");
printArray(array);
}
}
}
}
static void printArray(int[] array)
{
for(int i=0; i < array.length; i++)
{
System.out.print(array[i] + " ");
}
System.out.println();
}
}
Output:
Enter the number to store and do bubble sort:
Enter 5 elements do bubble sort:
12
Enter 5 elements do bubble sort:
12
33
45
12
1
---Array BEFORE Bubble Sort---
12 33 45 12 1 0 0 0 0 0
Sort Pass Number 1
Comparing 12 and 33
Comparing 33 and 45
Comparing 45 and 12
45 is greater than 12
Swapping Elements: New Array After Swap
12 33 12 45 1 0 0 0 0 0
Comparing 45 and 1
45 is greater than 1
Swapping Elements: New Array After Swap
12 33 12 1 45 0 0 0 0 0
Comparing 45 and 0
45 is greater than 0
Swapping Elements: New Array After Swap
12 33 12 1 0 45 0 0 0 0
Comparing 45 and 0
45 is greater than 0
Swapping Elements: New Array After Swap
12 33 12 1 0 0 45 0 0 0
Comparing 45 and 0
45 is greater than 0
Swapping Elements: New Array After Swap
12 33 12 1 0 0 0 45 0 0
Comparing 45 and 0
45 is greater than 0
Swapping Elements: New Array After Swap
12 33 12 1 0 0 0 0 45 0
Comparing 45 and 0
45 is greater than 0
Swapping Elements: New Array After Swap
12 33 12 1 0 0 0 0 0 45
Sort Pass Number 2
Comparing 12 and 33
Comparing 33 and 12
33 is greater than 12
Swapping Elements: New Array After Swap
12 12 33 1 0 0 0 0 0 45
Comparing 33 and 1
33 is greater than 1
Swapping Elements: New Array After Swap
12 12 1 33 0 0 0 0 0 45
Comparing 33 and 0
33 is greater than 0
Swapping Elements: New Array After Swap
12 12 1 0 33 0 0 0 0 45
Comparing 33 and 0
33 is greater than 0
Swapping Elements: New Array After Swap
12 12 1 0 0 33 0 0 0 45
Comparing 33 and 0
33 is greater than 0
Swapping Elements: New Array After Swap
12 12 1 0 0 0 33 0 0 45
Comparing 33 and 0
33 is greater than 0
Swapping Elements: New Array After Swap
12 12 1 0 0 0 0 33 0 45
Comparing 33 and 0
33 is greater than 0
Swapping Elements: New Array After Swap
12 12 1 0 0 0 0 0 33 45
Sort Pass Number 3
Comparing 12 and 12
Comparing 12 and 1
12 is greater than 1
Swapping Elements: New Array After Swap
12 1 12 0 0 0 0 0 33 45
Comparing 12 and 0
12 is greater than 0
Swapping Elements: New Array After Swap
12 1 0 12 0 0 0 0 33 45
Comparing 12 and 0
12 is greater than 0
Swapping Elements: New Array After Swap
12 1 0 0 12 0 0 0 33 45
Comparing 12 and 0
12 is greater than 0
Swapping Elements: New Array After Swap
12 1 0 0 0 12 0 0 33 45
Comparing 12 and 0
12 is greater than 0
Swapping Elements: New Array After Swap
12 1 0 0 0 0 12 0 33 45
Comparing 12 and 0
12 is greater than 0
Swapping Elements: New Array After Swap
12 1 0 0 0 0 0 12 33 45
Sort Pass Number 4
Comparing 12 and 1
12 is greater than 1
Swapping Elements: New Array After Swap
1 12 0 0 0 0 0 12 33 45
Comparing 12 and 0
12 is greater than 0
Swapping Elements: New Array After Swap
1 0 12 0 0 0 0 12 33 45
Comparing 12 and 0
12 is greater than 0
Swapping Elements: New Array After Swap
1 0 0 12 0 0 0 12 33 45
Comparing 12 and 0
12 is greater than 0
Swapping Elements: New Array After Swap
1 0 0 0 12 0 0 12 33 45
Comparing 12 and 0
12 is greater than 0
Swapping Elements: New Array After Swap
1 0 0 0 0 12 0 12 33 45
Comparing 12 and 0
12 is greater than 0
Swapping Elements: New Array After Swap
1 0 0 0 0 0 12 12 33 45
Sort Pass Number 5
Comparing 1 and 0
1 is greater than 0
Swapping Elements: New Array After Swap
0 1 0 0 0 0 12 12 33 45
Comparing 1 and 0
1 is greater than 0
Swapping Elements: New Array After Swap
0 0 1 0 0 0 12 12 33 45
Comparing 1 and 0
1 is greater than 0
Swapping Elements: New Array After Swap
0 0 0 1 0 0 12 12 33 45
Comparing 1 and 0
1 is greater than 0
Swapping Elements: New Array After Swap
0 0 0 0 1 0 12 12 33 45
Comparing 1 and 0
1 is greater than 0
Swapping Elements: New Array After Swap
0 0 0 0 0 1 12 12 33 45
Sort Pass Number 6
Comparing 0 and 0
Comparing 0 and 0
Comparing 0 and 0
Comparing 0 and 0
Sort Pass Number 7
Comparing 0 and 0
Comparing 0 and 0
Comparing 0 and 0
Sort Pass Number 8
Comparing 0 and 0
Comparing 0 and 0
Sort Pass Number 9
Comparing 0 and 0
Sort Pass Number 10
---Array AFTER Bubble Sort---
0 0 0 0 0 1 12 12 33 45
12 33 45 12 1 0 0 0 0 0
Sort Pass Number 1
Comparing 12 and 33
Comparing 33 and 45
Comparing 45 and 12
45 is greater than 12
Swapping Elements: New Array After Swap
12 33 12 45 1 0 0 0 0 0
Comparing 45 and 1
45 is greater than 1
Swapping Elements: New Array After Swap
12 33 12 1 45 0 0 0 0 0
Comparing 45 and 0
45 is greater than 0
Swapping Elements: New Array After Swap
12 33 12 1 0 45 0 0 0 0
Comparing 45 and 0
45 is greater than 0
Swapping Elements: New Array After Swap
12 33 12 1 0 0 45 0 0 0
Comparing 45 and 0
45 is greater than 0
Swapping Elements: New Array After Swap
12 33 12 1 0 0 0 45 0 0
Comparing 45 and 0
45 is greater than 0
Swapping Elements: New Array After Swap
12 33 12 1 0 0 0 0 45 0
Comparing 45 and 0
45 is greater than 0
Swapping Elements: New Array After Swap
12 33 12 1 0 0 0 0 0 45
Sort Pass Number 2
Comparing 12 and 33
Comparing 33 and 12
33 is greater than 12
Swapping Elements: New Array After Swap
12 12 33 1 0 0 0 0 0 45
Comparing 33 and 1
33 is greater than 1
Swapping Elements: New Array After Swap
12 12 1 33 0 0 0 0 0 45
Comparing 33 and 0
33 is greater than 0
Swapping Elements: New Array After Swap
12 12 1 0 33 0 0 0 0 45
Comparing 33 and 0
33 is greater than 0
Swapping Elements: New Array After Swap
12 12 1 0 0 33 0 0 0 45
Comparing 33 and 0
33 is greater than 0
Swapping Elements: New Array After Swap
12 12 1 0 0 0 33 0 0 45
Comparing 33 and 0
33 is greater than 0
Swapping Elements: New Array After Swap
12 12 1 0 0 0 0 33 0 45
Comparing 33 and 0
33 is greater than 0
Swapping Elements: New Array After Swap
12 12 1 0 0 0 0 0 33 45
Sort Pass Number 3
Comparing 12 and 12
Comparing 12 and 1
12 is greater than 1
Swapping Elements: New Array After Swap
12 1 12 0 0 0 0 0 33 45
Comparing 12 and 0
12 is greater than 0
Swapping Elements: New Array After Swap
12 1 0 12 0 0 0 0 33 45
Comparing 12 and 0
12 is greater than 0
Swapping Elements: New Array After Swap
12 1 0 0 12 0 0 0 33 45
Comparing 12 and 0
12 is greater than 0
Swapping Elements: New Array After Swap
12 1 0 0 0 12 0 0 33 45
Comparing 12 and 0
12 is greater than 0
Swapping Elements: New Array After Swap
12 1 0 0 0 0 12 0 33 45
Comparing 12 and 0
12 is greater than 0
Swapping Elements: New Array After Swap
12 1 0 0 0 0 0 12 33 45
Sort Pass Number 4
Comparing 12 and 1
12 is greater than 1
Swapping Elements: New Array After Swap
1 12 0 0 0 0 0 12 33 45
Comparing 12 and 0
12 is greater than 0
Swapping Elements: New Array After Swap
1 0 12 0 0 0 0 12 33 45
Comparing 12 and 0
12 is greater than 0
Swapping Elements: New Array After Swap
1 0 0 12 0 0 0 12 33 45
Comparing 12 and 0
12 is greater than 0
Swapping Elements: New Array After Swap
1 0 0 0 12 0 0 12 33 45
Comparing 12 and 0
12 is greater than 0
Swapping Elements: New Array After Swap
1 0 0 0 0 12 0 12 33 45
Comparing 12 and 0
12 is greater than 0
Swapping Elements: New Array After Swap
1 0 0 0 0 0 12 12 33 45
Sort Pass Number 5
Comparing 1 and 0
1 is greater than 0
Swapping Elements: New Array After Swap
0 1 0 0 0 0 12 12 33 45
Comparing 1 and 0
1 is greater than 0
Swapping Elements: New Array After Swap
0 0 1 0 0 0 12 12 33 45
Comparing 1 and 0
1 is greater than 0
Swapping Elements: New Array After Swap
0 0 0 1 0 0 12 12 33 45
Comparing 1 and 0
1 is greater than 0
Swapping Elements: New Array After Swap
0 0 0 0 1 0 12 12 33 45
Comparing 1 and 0
1 is greater than 0
Swapping Elements: New Array After Swap
0 0 0 0 0 1 12 12 33 45
Sort Pass Number 6
Comparing 0 and 0
Comparing 0 and 0
Comparing 0 and 0
Comparing 0 and 0
Sort Pass Number 7
Comparing 0 and 0
Comparing 0 and 0
Comparing 0 and 0
Sort Pass Number 8
Comparing 0 and 0
Comparing 0 and 0
Sort Pass Number 9
Comparing 0 and 0
Sort Pass Number 10
---Array AFTER Bubble Sort---
0 0 0 0 0 1 12 12 33 45