Bubble Sort In JAVA (Algorithm Program Steps With Examples)

0
162

The Bubble sort algorithm is one of the most simple sorting algorithms and it is a great learning tool because it is easy to understand and fast to implement. Here we have shared how to implement Bubble Sort Algorithm in Java with example.

What is Bubble Sort?

Bubble sort is a simple and effective sorting algorithm. This sorting algorithm is a comparison-based sorting algorithm in which each pair of adjacent elements is compared and elements are swapped if they are not in order.

It moves on down the list and continues doing so. And at the end of the data, it starts over until all the data is in the right order.

But Bubble sort algorithm is not suitable for large data sets.

Bubble Sort In Java: 

We can use bubble sort to sort array elements in a java program. The Java bubble sort method is the most basic sorting method. 

The array is traversed from the start to the final element in the bubble sort method. The current element is compared to the following element in this case. It is exchanged if the current element is greater than the next element. 

Each element of the array is compared to its neighboring element in bubble sort.

The list is processed in passes by the algorithm. Sorting a list with n elements involves n-1 passes. Consider an array A with n elements that must be sorted using the Bubble sort algorithm. The algorithm works in the same way as a follower. 

  • A[0] is compared to A[1], 
  • A[1] is compared to A[2,] 
  • A[2] is compared to A[3,] and so on in Pass 1.

The largest element of the list is placed at the highest index of the list at the end of pass 1. 

  • A[0] is compared to A[1],
  • A[1] is compared to A[2],
  • and so on in Pass 2.

The second largest element of the list is inserted at the second-highest index of the list at the end of Pass 2. 

  • A[0] is compared to A[1], 
  • A[1] is compared to A[2],
  • and so on in pass n-1.

At the conclusion of this passage. The first index of the list is given to the list’s smallest element. 

Must Read ➜ Types of Memory in Computer

How to write an algorithm for bubble sort in JAVA?

An algorithm is a set of instructions that is used to solve a problem. Here is a step-by-step guide to implementing the Bubble sort algorithm.

  • Step 1: For I = 0 to N-1, repeat Step 2. 
  • Step 2: Repeat Steps 1 through 2 for J = I + 1 to N – I. 
  • Step 3: IF A[J] > A[i], go to the next step. 
  • Step 4: A[J] and A[i] are swapped. 
  • Step 5:  [FINAL INNER LOOP] 
  • Step 6: [THE END OF THE OUTER LOOP] 
  • Step 7:  EXIT is the fourth step. 

Example of Java Bubble Sort Program  

 public class BubbleSortExample {   

    staticvoid 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;   

                         }   

                       }   

         }   

    }   

    publicstaticvoid main(String[] args) {   

                int arr[] ={1,6,8,9,4,5,2};   

             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
1689452                 
Array After Bubble Sort 
1245689                 

Must Read ➜ Recursion function in Python

Example 2 

publicclass BubbleSort {   

    publicstaticvoid main(String[] args) {   

    int[] a = {10, 9, 7, 101, 23, 44, 12, 78, 34, 23};   

    for(int i=0;i<10;i++)   

    {   

        for (int j=0;j<10;j++)   

        {   

            if(a[i]<a[j])   

            {   

                int temp = a[i];   

                a[i]=a[j];   

                a[j] = temp;    

            }   

        }   

    }   

    System.out.println("Sorted List ...");   

    for(int i=0;i<10;i++)   

    {   

        System.out.println(a[i]);   

    }   

}   

}   

Output: 

Sorted List . . .
7                
9                
10               
12               
23               
34               
44               
78               
101               

Advantages of Bubble sort

  • Bubble sort is one of the easiest sort algorithms.
  • It is easy to implement.
  • Elements are swapped in place, not use extra array.
  • It is stable and fast.

Disadvantages of Bubble sort

  • It does not deal well with a list containing a huge number of items.
  • More than the number of comparisons.
  • The code becomes complex for a large amount of data.