Programming

Program for Bubble Sort in Java

Gautam Gautam Donate Nov 27, 2019 · 5 mins read
Program for Bubble Sort in Java

Sorting is any process of arranging items systematically. Bubble sort algorithm is known as the simplest sorting algorithm.

In bubble sort algorithm, the array is traversed from the first element to last element. Here, the current element is compared with the next element. If the current element is greater than the next element, it is swapped, this process continues till the whole array gets sorted.

Example - Swap 6, 2, 5, 3, 9 using Bubble Sort - 

Pass 1 -

6 2 5 3 9 ) -> ( 2 6 5 3 9 ), Here the first 2 elements are compared and is swapped since 6 > 2, again,

( 2 6 5 3 9 ) -> ( 2 5 6 3 9 ) Swap Since, 6 > 5

( 2 5 6 3 9 ) -> ( 2 5 3 6 9 ) Swap since, 6 > 3

( 2 5 3 6 9 ) -> ( 2 5 3 6 9 ) since 6 and 9 are already sorted, no need to swap

Pass 2 - 

2 5 3 6 9 ) -> ( 2 5 3 6 9 ) already in order, no need to swap

( 2 5 3 6 9 ) -> ( 2 3 6 9 ) Swap since 5 > 3

( 2 3 5 6 9 ) -> ( 2 3 5 6 9 ) already in order, no need to swap

( 2 3 5 6 9 ) -> ( 2 3 5 6 9 ) already in order, no need to swap

Now, the array is sorted, but the algorithm does not know if it is completed or not, so again one whole pass is done without any swap operation -

Pass 3 -

2 3 5 6 9 ) -> ( 2 3 5 6 9 )

( 2 3 5 6 9 ) -> ( 2 3 5 6 9 )

( 2 3 5 6 9 ) -> ( 2 3 5 6 9 )

( 2 3 5 6 9 ) -> ( 2 3 5 6 9 )

Now, let's implement this algorithm in Java -

Program for Bubble Sort in Java - 

/**
 * Program for Bubble Sort in Java...
 */
import java.io.*;

public class BubbleSortExample {  
    static void bubbleSort(int[] arr) {  
        int n = arr.length;  
        int temp = 0;  
         for(int i=0; i < n; i++){  
                 for(int j=1; j < (n-i); j++){  
                          if(arr[j-1] > arr[j]){  
                                 //swap elements  
                                 temp = arr[j-1];  
                                 arr[j-1] = arr[j];  
                                 arr[j] = temp;  
                         }  
                          
                 }  
         }  
  
    }  
    public static void main(String[] args) {  
                int arr[] ={6,2,5,3,9};  
                 
                System.out.println("Array Before Bubble Sort");  
                for(int i=0; i < arr.length; i++){  
                        System.out.print(arr[i] + " ");  
                }  
                System.out.println();  
                  
                bubbleSort(arr);//sorting array elements using bubble sort  
                 
                System.out.println("Array After Bubble Sort");  
                for(int i=0; i < arr.length; i++){  
                        System.out.print(arr[i] + " ");  
                }  
   
        }  
}

Output - 

Array Before Bubble Sort
6 2 5 3 9 
Array After Bubble Sort
2 3 5 6 9

Tags

Join Newsletter
Get the latest news right in your inbox. We never spam!
Gautam
Written by Gautam Sharma Profile
Developer, Writer, Emotional, loves to help, blogger

Connect with Gautam