Bubble Sort in Ascending Order in Java

In this blog post, we will learn how to write a Java Program to Implement Bubble Sort in Ascending Order.

Bubble sort is a sorting algorithm that compares two adjacent elements and swaps them until they are in the intended order. Just like the movement of air bubbles in the water that rises up to the surface, each element of the array moves to the end in each iteration. Therefore, it is called a bubble sort.

Java Program to Implement Bubble Sort in Ascending Order

Below is the complete Java program that demonstrates the Bubble Sort algorithm for sorting an array in ascending order, followed by its output.

public class BubbleSortAscending {

    public static void main(String[] args) {
        // Define a sample array of numbers
        int[] numbers = {34, 12, 56, 78, 33};

        // Sort the array using bubble sort
        bubbleSortAscending(numbers);

        // Display the sorted array
        System.out.println("Sorted array in ascending order:");
        for (int num : numbers) {
            System.out.print(num + " ");
        }
    }

    /**
     * Sorts an array of integers in ascending order using Bubble Sort.
     *
     * @param arr The array to be sorted.
     */
    public static void bubbleSortAscending(int[] arr) {
        int n = arr.length;  // Get the length of the array
        boolean swapped;    // Flag to track whether elements were swapped in a pass

        // Outer loop for the number of passes
        for (int i = 0; i < n - 1; i++) {
            swapped = false;  // Reset the swapped flag for each pass

            // Inner loop to compare and potentially swap adjacent elements
            for (int j = 0; j < n - i - 1; j++) {
                // If the current element is greater than the next element
                if (arr[j] > arr[j + 1]) {
                    // Swap the two elements
                    int temp = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = temp;

                    // Set the swapped flag to true as a swap has been performed
                    swapped = true;
                }
            }

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

Output:

Sorted array in ascending order:
12 33 34 56 78 

Understanding the Code 

  1. The outer loop represents the pass through the array. 
  2. The inner loop performs the actual comparisons and potential swaps. 
  3. If the current element (arr[j]) is greater than the next element (arr[j+1]), they are swapped to ensure that numbers are sorted in ascending order. 
  4. The swapped flag acts as an optimization. If during a full pass, no swaps are made, it indicates the array is sorted, and we break out of the loop.

Key Takeaways 

Performance: Bubble Sort has an average and worst-case time complexity of O(n^2). Therefore, while it’s great for learning and can be efficient for small datasets, there are more efficient sorting algorithms for larger datasets. 

Learning Value: Grasping the logic behind Bubble Sort can make understanding more complex algorithms easier. 

Conclusion 

Bubble Sort in ascending order in Java offers beginners a concise and intuitive introduction to the world of sorting algorithms. Although there are numerous other, more efficient algorithms out there, starting with Bubble Sort allows for a robust foundation in algorithmic thought. Dive in, experiment, and then explore further into the fascinating realm of algorithms!

Comments