Industrial manufacturing
Industrial Internet of Things | Industrial materials | Equipment Maintenance and Repair | Industrial programming |
home  MfgRobots >> Industrial manufacturing >  >> Industrial programming >> Java

Master Bubble Sort in Java: Clear Example & Step‑by‑Step Walkthrough

What Is Bubble Sort?

Bubble sort is a classic comparison‑based algorithm that repeatedly steps through a list, compares adjacent items, and swaps them if they are in the wrong order. Each pass moves the next largest value to its final position—hence the name “bubble.” While simple, it provides a solid foundation for understanding sorting mechanics.

Java Implementation

The following program demonstrates bubble sort on an integer array. It prints detailed debugging information so you can see the algorithm in action.

package com.guru99;

public class BubbleSort {

    public static void main(String[] args) {
        int[] arr = {860, 8, 200, 9};

        System.out.println("---Array BEFORE Bubble Sort---");
        printArray(arr);

        bubbleSort(arr); // Sort using bubble sort

        System.out.println("---Array AFTER Bubble Sort---");
        printArray(arr);
    }

    static void bubbleSort(int[] array) {
        int n = array.length;
        for (int i = 0; i < n; i++) {
            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
                    int 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();
    }
}

Program Output

860 8 200 9 
Sort Pass Number 1
Comparing 860 and 8
860 is greater than 8
Swapping Elements: New Array After Swap
8 860 200 9 
Comparing 860 and 200
860 is greater than 200
Swapping Elements: New Array After Swap
8 200 860 9 
Comparing 860 and 9
860 is greater than 9
Swapping Elements: New Array After Swap
8 200 9 860 
Sort Pass Number 2
Comparing 8 and 200
Comparing 200 and 9
200 is greater than 9
Swapping Elements: New Array After Swap
8 9 200 860 
Sort Pass Number 3
Comparing 8 and 9
Sort Pass Number 4
---Array AFTER Bubble Sort---
8 9 200 860

Java

  1. Java Hello World: Your First Program
  2. Mastering Java Arrays: Declaration, Initialization, and Manipulation
  3. Mastering Java Multidimensional Arrays: 2D & 3D Examples
  4. Java Array Copying Techniques: A Comprehensive Guide
  5. Java Hello World: Step‑by‑Step Guide to Writing Your First Java Program
  6. Java For‑Each Loop: Simplifying Array Iteration Without Counters
  7. Master Bubble Sort in Java: Clear Example & Step‑by‑Step Walkthrough
  8. Insertion Sort Algorithm in Java: A Clear Example & Step‑by‑Step Guide
  9. Selection Sort in Java: Step‑by‑Step Example & Full Code
  10. Master Java Arrays: A Comprehensive Guide to Efficient Data Handling