The bubble sort in java is an algorithm that sorts a set of data in either in ascending order or in descending order. There are various types of sorting algorithms like merge sort, heap sort, insertion sort, quick sort, etc, but, bubble sort is the most straightforward algorithm designed ever. In bubble sort compare and swap technique is used. That means bubble sort compares adjacent data with each other and swaps it if the data is in the wrong order.
Bubble sort is used very rarely while sorting because it takes comparatively more time than other algorithms. Bubble sort has excellent stability and provides correct accuracy than others. In some cases, the data is more shuffled for that case the complexity of the data set is O(n²). The bubble sort algorithm is just to learn how sorting algorithms work. It is not for practical use.
Table of Contents
The logic of bubble sort
As mentioned above, the bubble sort is a simple sorting. It uses the compare and swap technique. Following are the steps to sort a set of data using bubble sort.
- Select the first element from unsorted data
- Compare the current data with the next element present in that set.
- If the current data is greater than the next data, then swap their positions.
- Follow this n-1 time where n is the number of elements of the data set.
Program for bubble sort in java
There are various ways to implement the bubble sort in java like using for loop, while loop, and using a function to use bubble sort multiple times in the program. The example of Bubble sort code in java is given below.
1. Bubble sort using for loop
A simple code of sorting an array using bubble sort in java. We have declared an unsorted array in the starting. To sort that array we are using a bubble sort algorithm using for loop. Now let’s have a look at the example.
Example
public static void main(String[] args) {
int arr[] = {80,90,50,60,30,20,10,40}; //unsorted array
int tmp = 0; // temporary variable
System.out.print("Unsorted array is ");
for (int i = 0; i <arr.length; i++)
{
System.out.print(arr[i]+" "); // printing unsorted array
}
for(int i=0; i < arr.length; i++)
{
for(int j=1; j < (arr.length-i); j++)
{
if(arr[j-1] > arr[j]) // comparing adjacent data
{
tmp = arr[j-1];
arr[j-1] = arr[j]; // Swapping code
arr[j] = tmp;
}
}
}
System.out.print("\nSorted array is ");
for (int i = 0; i <arr.length; i++)
{
System.out.print(arr[i]+" "); // printing sorted array
}
}
}
Output
Unsorted array is 80 90 50 60 30 20 10 40
Sorted array is 10 20 30 40 50 60 80 90
2. Bubble sort using while loop
Same as the above example, We have declared an unsorted array and sorting it with a while loop using bubble sort. An example is given below.
Example
public class bubble_sort {
public static void main(String[] args) {
int a[] = {80,90,50,60,30,20,10,40}; //declaring unsorted array
int tmp = 0; // temporary variable
System.out.print("Unsorted array is ");
for (int i = 0; i <a.length; i++)
{
System.out.print(a[i]+" "); //printing unsorted array
}
for(int j = 0; j<a.length; j++)
{
boolean swapped = false; // flag variable
int i = 0;
while(i<a.length-1)
{
if (a[i] > a[i+1]) // comparing adjacent data
{
tmp = a[i];
a[i] = a[i+1]; // swapping
a[i+1] = tmp;
swapped = true;
}
i++;
}
if (!swapped) //break condition
break;
}
System.out.print("\nSorted array is ");
for (int i = 0; i <a.length; i++)
{
System.out.print(a[i]+" "); // printing sorted data
}
}
}
Output
Unsorted array is 80 90 50 60 30 20 10 40
Sorted array is 10 20 30 40 50 60 80 90
3. Bubble sort in java using a function
To use the sort algorithm multiple times in the program. We have created the function of bubble sort. Let’s have a look at the example given below.
Example
public class bubble_sort
{
static void sort (int arr[]) // Static function
{
int tmp = 0; // temporary variable
for(int i=0; i < arr.length; i++)
{
for(int j=1; j < (arr.length-i); j++)
{
if(arr[j-1] > arr[j]) //comparing adjacent data
{
tmp = arr[j-1];
arr[j-1] = arr[j]; //swapping
arr[j] = tmp;
}
}
}
}
public static void main(String[] args)
{
int arr[] = {80,90,50,60,30,20,10,40};
System.out.print("Unsorted array is ");
for (int i = 0; i <arr.length; i++)
{
System.out.print(arr[i]+" "); // prints unsorted data
}
sort(arr); // call to the function
System.out.print("\nSorted array is ");
for (int i = 0; i <arr.length; i++) {
System.out.print(arr[i]+" "); //prints sorted data
}
}
}
Output
Unsorted array is 80 90 50 60 30 20 10 40
Sorted array is 10 20 30 40 50 60 80 90