Bubble Sort Implementation in Java

Java Programming Language (Article) (Program)

25

Here’s the Bubble Sort implementation in Java along with a table showing each iteration when the input is [5, 4, 3, 2, 1].

Bubble Sort Implementation:

Bubble Sort is a simple sorting algorithm that repeatedly steps through the list, compares adjacent elements, and swaps them if they are in the wrong order. This process is repeated until the list is sorted.

Steps:

  1. Compare adjacent elements: If the current element is larger than the next element, they are swapped.
  2. Largest element moves to the end: After each full pass, the largest unsorted element moves to its correct position.
  3. Repeat: The process continues for all elements until no more swaps are required.

Key Points:

  • It makes several passes through the array.
  • After each pass, the largest unsorted element is placed in its correct position.
  • The algorithm is named "Bubble Sort" because smaller elements bubble up to the top of the list.

In the example [5, 4, 3, 2, 1], after each iteration, the largest unsorted element moves to the right, and the array becomes more sorted with each pass.

Program:

import java.util.Arrays;

public class BubbleSort {

    public static void main(String[] args) {
        int[] arr = {5, 4, 3, 2, 1};  // Input array
        
        System.out.println("Original Array: " + Arrays.toString(arr));
        bubbleSort(arr);
    }

    // Bubble Sort Method
    public static void bubbleSort(int[] arr) {
        int n = arr.length;
        for (int i = 0; i < n - 1; i++) {
            // Print each iteration
            System.out.println("Iteration " + (i+1) + ": ");
            boolean swapped = false;
            for (int j = 0; j < n - i - 1; j++) {
                // Swap if the element is greater than the next element
                if (arr[j] > arr[j + 1]) {
                    int temp = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = temp;
                    swapped = true;
                }
            }
            // Print array after each pass
            System.out.println(Arrays.toString(arr));

            // If no two elements were swapped in inner loop, then break
            if (!swapped) {
                break;
            }
        }
    }
}

Output:

[1, 2, 3, 4, 5]

Explanation:

Let's break down the sorting process with the array [5, 4, 3, 2, 1]:

Iteration Step Array State Swap?
Initial   [5, 4, 3, 2, 1]  
Iteration 1 Step 1 [4, 5, 3, 2, 1] 5 and 4 swapped
  Step 2 [4, 3, 5, 2, 1] 5 and 3 swapped
  Step 3 [4, 3, 2, 5, 1] 5 and 2 swapped
  Step 4 [4, 3, 2, 1, 5] 5 and 1 swapped
Iteration 2 Step 1 [3, 4, 2, 1, 5] 4 and 3 swapped
  Step 2 [3, 2, 4, 1, 5] 4 and 2 swapped
  Step 3 [3, 2, 1, 4, 5] 4 and 1 swapped
Iteration 3 Step 1 [2, 3, 1, 4, 5] 3 and 2 swapped
  Step 2 [2, 1, 3, 4, 5] 3 and 1 swapped
Iteration 4 Step 1 [1, 2, 3, 4, 5] 2 and 1 swapped

Final Sorted Array: [1, 2, 3, 4, 5]

In Bubble Sort, the largest element "bubbles" to its correct position after each iteration, and the process continues until no swaps are needed in an iteration.


This Particular section is dedicated to Programs only. If you want learn more about Java Programming Language. Then you can visit below links to get more depth on this subject.