Bubble Sort in Descending Order in Java

In this blog post, we will discuss how to implement the Bubble Sort algorithm to sort elements in descending order using Java. We will also walk through the program step by step to make sure you understand every bit of it.

Bubble Sort is a simple and intuitive sorting algorithm that repeatedly steps through the list, compares adjacent elements, and swaps them if they are in the wrong order. The procedure is repeated until no more swaps are required, which means the list is sorted. Although it's not the most efficient sorting algorithm in the world, its simplicity and ease of understanding make it a popular choice among beginners.

Java Program for Bubble Sort in Descending Order

public class BubbleSortDescending {

    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 in descending order
        bubbleSortDescending(numbers);

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

    /**
     * Sorts an array of integers in descending order using Bubble Sort.
     *
     * @param arr The array to be sorted.
     */
    public static void bubbleSortDescending(int[] arr) {
        int n = arr.length;  // Determine the length of the array

        // Outer loop for the number of passes
        for (int i = 0; i < n - 1; i++) {
            
            // Flag to track if any swapping occurred during this pass
            boolean swapped = false;  

            // Inner loop to compare and potentially swap adjacent elements
            for (int j = 0; j < n - i - 1; j++) {
                
                // If the current element is less than the next element (for descending order)
                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 since a swap has occurred
                    swapped = true;
                }
            }

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

Output:

Sorted array in descending order:
78 56 34 33 12

Step by Step Explanation

Initialization: A sample array of numbers numbers is defined. 

Sorting Mechanism: The bubbleSortDescending function is invoked with numbers as its argument. This function sorts the array in descending order using the Bubble Sort algorithm. 

Comparison and Swapping: Inside the bubbleSortDescending function, nested loops are utilized. The outer loop represents the number of passes required, while the inner loop compares adjacent array elements. If the elements are in ascending order (which is incorrect for our requirement), they are swapped. 

Optimization: A swapped flag is introduced to track if any swapping occurred during a single pass through the array. If no swapping happens during a complete pass, it means the array is already sorted, and we can break out of the loop early. This optimization helps in improving the performance for nearly sorted or already sorted lists. 

Displaying the Result: After sorting, the sorted array is printed to the console in descending order. 

We hope this blog post has provided you with a clear understanding of the Bubble Sort algorithm in Java, especially for descending-order sorting. Happy coding!

Comments