Google News
logo
Java Programs for Bubble Sort in Ascending & Descending Order
In the following examples of Java programs for bubble sort in ascending and descending order :

Bubble Sort in Ascending Order :

Program :
public class BubbleSortAscending {

    public static void main(String[] args) {
        int[] arr = {5, 2, 8, 1, 3};
        System.out.println("Before sorting:");
        printArray(arr);
        bubbleSortAscending(arr);
        System.out.println("After sorting in ascending order:");
        printArray(arr);
    }

    public static void bubbleSortAscending(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]) {
                    int temp = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = temp;
                }
            }
        }
    }

    public static void printArray(int[] arr) {
        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i] + " ");
        }
        System.out.println();
    }
}
Output :
Before sorting:5 2 8 1 3 
After sorting in ascending order:
1 2 3 5 8 
In this program, we first declare an integer array arr and initialize it with the values 5, 2, 8, 1, and 3. We then print the array before sorting using the printArray method.

We then call the bubbleSortAscending method and pass the array as an argument. This method performs a bubble sort on the array in ascending order.

The bubble sort algorithm works by repeatedly swapping adjacent elements if they are in the wrong order, until no more swaps are needed. We start by comparing the first two elements, and if the first is larger than the second, we swap them. We then move on to compare the second and third elements, and so on, until we reach the end of the array.

At this point, we have the largest element in the array at the last position. We then repeat the process for the remaining elements, up to the second-to-last element. We continue this process until no more swaps are needed, which means the array is sorted.

Finally, we print the array after sorting using the printArray method.


Bubble Sort in Descending Order :

Program :
public class BubbleSortDescending {

    public static void main(String[] args) {
        int[] arr = {5, 2, 8, 1, 3};
        System.out.println("Before sorting:");
        printArray(arr);
        bubbleSortDescending(arr);
        System.out.println("After sorting in descending order:");
        printArray(arr);
    }

    public static void bubbleSortDescending(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]) {
                    int temp = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = temp;
                }
            }
        }
    }

    public static void printArray(int[] arr) {
        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i] + " ");
        }
        System.out.println();
    }
}
Output :
Before sorting:5 2 8 1 3 
After sorting in descending order:8 5 3 2 1