Java Tutorial

Operators

Flow Control

String

Number and Date

Built-in Classes

Array

Class and Object

Inheritance and Polymorphism

Exception Handling

Collections, Generics and Enumerations

Reflection

Input/Output Stream

Annotation

Java Array Bubble Sort

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. The pass through the list is repeated until the list is sorted.

In this tutorial, we will demonstrate how to implement the Bubble Sort algorithm in Java using arrays.

public class BubbleSort {
    public static void main(String[] args) {
        int[] arr = {5, 3, 1, 4, 2};

        bubbleSort(arr);

        // Print the sorted array
        for (int num : arr) {
            System.out.print(num + " ");
        }
    }

    public static void bubbleSort(int[] array) {
        int n = array.length;
        boolean swapped;

        for (int i = 0; i < n - 1; i++) {
            swapped = false;
            for (int j = 0; j < n - i - 1; j++) {
                if (array[j] > array[j + 1]) {
                    // Swap the elements
                    int temp = array[j];
                    array[j] = array[j + 1];
                    array[j + 1] = temp;
                    swapped = true;
                }
            }

            // If no two elements were swapped in the inner loop, the array is already sorted
            if (!swapped) {
                break;
            }
        }
    }
}

When you run this code, it will print the sorted array:

1 2 3 4 5

In this example, we defined a bubbleSort method that takes an array as input and sorts it using the Bubble Sort algorithm. The outer loop iterates through the array, while the inner loop compares adjacent elements and swaps them if they are in the wrong order.

The swapped variable is used to optimize the algorithm. If no elements were swapped during an entire pass through the inner loop, it means the array is already sorted, and we can break out of the outer loop early.

Keep in mind that Bubble Sort has a worst-case and average-case time complexity of O(n^2), where n is the number of items being sorted. It is not suitable for large datasets, and more efficient sorting algorithms like QuickSort or MergeSort should be used in those cases.

  1. Java Bubble Sort Code: Here's a basic implementation of the bubble sort algorithm in Java:

    public class BubbleSort {
        public static void main(String[] args) {
            int[] array = {5, 2, 9, 1, 5, 6};
            bubbleSort(array);
            for (int num : array) {
                System.out.print(num + " ");
            }
        }
    
        public static void bubbleSort(int[] arr) {
            int n = arr.length;
            for (int i = 0; i < n - 1; i++) {
                for (int j = 0; j < n - i - 1; j++) {
                    if (arr[j] > arr[j + 1]) {
                        // Swap arr[j] and arr[j + 1]
                        int temp = arr[j];
                        arr[j] = arr[j + 1];
                        arr[j + 1] = temp;
                    }
                }
            }
        }
    }
    
  2. Optimized Bubble Sort in Java: Bubble sort can be optimized by adding a flag to check if any swaps were made during a pass. If no swaps are made, the array is already sorted.

    public static void optimizedBubbleSort(int[] arr) {
        int n = arr.length;
        boolean swapped;
        for (int i = 0; i < n - 1; i++) {
            swapped = false;
            for (int j = 0; j < n - i - 1; j++) {
                if (arr[j] > arr[j + 1]) {
                    // Swap arr[j] and arr[j + 1]
                    int temp = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = temp;
                    swapped = true;
                }
            }
            // If no two elements were swapped, the array is already sorted
            if (!swapped) {
                break;
            }
        }
    }
    
  3. Java Bubble Sort for Strings: Bubble sort can be applied to arrays of strings by comparing string elements.

    public static void bubbleSortStrings(String[] arr) {
        int n = arr.length;
        for (int i = 0; i < n - 1; i++) {
            for (int j = 0; j < n - i - 1; j++) {
                if (arr[j].compareTo(arr[j + 1]) > 0) {
                    // Swap arr[j] and arr[j + 1]
                    String temp = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = temp;
                }
            }
        }
    }
    
  4. Bubble Sort with Java ArrayList: Bubble sort can be applied to ArrayList using the get and set methods.

    public static void bubbleSortArrayList(ArrayList<Integer> list) {
        int n = list.size();
        for (int i = 0; i < n - 1; i++) {
            for (int j = 0; j < n - i - 1; j++) {
                if (list.get(j) > list.get(j + 1)) {
                    // Swap list[j] and list[j + 1]
                    int temp = list.get(j);
                    list.set(j, list.get(j + 1));
                    list.set(j + 1, temp);
                }
            }
        }
    }
    
  5. Java Bubble Sort Descending Order: To sort in descending order, adjust the comparison condition.

    if (arr[j] < arr[j + 1]) {
        // Swap for descending order
        int temp = arr[j];
        arr[j] = arr[j + 1];
        arr[j + 1] = temp;
    }
    
  6. Bubble Sort Visualization in Java: Visualization can be done by printing the array at each step.

    public static void visualizeBubbleSort(int[] arr) {
        int n = arr.length;
        for (int i = 0; i < n - 1; i++) {
            for (int j = 0; j < n - i - 1; j++) {
                if (arr[j] > arr[j + 1]) {
                    // Swap arr[j] and arr[j + 1]
                    int temp = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = temp;
    
                    // Print the array for visualization
                    printArray(arr);
                }
            }
        }
    }
    
    public static void printArray(int[] arr) {
        for (int num : arr) {
            System.out.print(num + " ");
        }
        System.out.println();
    }
    
  7. Java Bubble Sort Recursive Implementation: Bubble sort can be implemented recursively, though it's less common due to its inefficiency.

    public static void recursiveBubbleSort(int[] arr, int n) {
        if (n == 1) {
            return;
        }
        for (int i = 0; i < n - 1; i++) {
            if (arr[i] > arr[i + 1]) {
                // Swap arr[i] and arr[i + 1]
                int temp = arr[i];
                arr[i] = arr[i + 1];
                arr[i + 1] = temp;
            }
        }
        recursiveBubbleSort(arr, n - 1);
    }