Java
Java

 

 

Overview

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.

Its average and worst-case time complexity is O(n²), where n is the number of items being sorted. Despite its inefficiency on large lists, it is a popular introductory algorithm.

Java Implementation


public class BubbleSort {
    
    /**
     * This method sorts an array using the Bubble Sort algorithm.
     *
     * @param arr The array of integers to be sorted.
     */
    public static void bubbleSort(int[] arr) {
        int n = arr.length;
        boolean swapped;

        // Traverse through all array elements
        for (int i = 0; i < n - 1; i++) {
            swapped = false;

            // Last i elements are already in place
            for (int j = 0; j < n - i - 1; j++) { // Compare adjacent elements if (arr[j] > arr[j + 1]) {
                    // Swap if they are in the wrong order
                    int temp = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = temp;
                    swapped = true;
                }
            }

            // If no elements were swapped, the array is sorted
            if (!swapped) break;
        }
    }

    /**
     * Main method to execute the bubble sort.
     *
     * @param args Command line arguments.
     */
    public static void main(String[] args) {
        int[] array = {64, 34, 25, 12, 22, 11, 90};
        
        System.out.println("Original array:");
        printArray(array);
        
        bubbleSort(array);
        
        System.out.println("Sorted array:");
        printArray(array);
    }

    /**
     * This method prints the elements of the array.
     *
     * @param arr The array to be printed.
     */
    public static void printArray(int[] arr) {
        for (int value : arr) {
            System.out.print(value + " ");
        }
        System.out.println();
    }
}
        

Program Structure

The program consists of the following main components:

  • bubbleSort Method: This method takes an array as input and sorts it using the Bubble Sort algorithm.
  • main Method: This is the entry point of the program, where an array is defined, and the sorting function is called.
  • printArray Method: A utility method to display the elements of the array before and after sorting.

Usage

To run this program, copy the code into a file named BubbleSort.java and compile it using a Java compiler. After compiling, execute the program, and it will display the original and sorted arrays.

 

By Aditya Bhuyan

I work as a cloud specialist. In addition to being an architect and SRE specialist, I work as a cloud engineer and developer. I have assisted my clients in converting their antiquated programmes into contemporary microservices that operate on various cloud computing platforms such as AWS, GCP, Azure, or VMware Tanzu, as well as orchestration systems such as Docker Swarm or Kubernetes. For over twenty years, I have been employed in the IT sector as a Java developer, J2EE architect, scrum master, and instructor. I write about Cloud Native and Cloud often. Bangalore, India is where my family and I call home. I maintain my physical and mental fitness by doing a lot of yoga and meditation.

Leave a Reply

Your email address will not be published. Required fields are marked *

error

Enjoy this blog? Please spread the word :)