Bubble Sort Algorithm | Java Programs

Output:

*** Bubble Sort Algorithm ***

The Original Array is: [10, 2, 7, 11, 9]

After applying BUBBLE SORT algorithm.

The Array becomes: [2, 7, 9, 10, 11]

Click Here For Java Online Compiler

Solution:

import java.util.Arrays;

public class BubbleSortAlgorithm {

    public static void main(String[] args) {
        int[] myArray = {10, 2, 7, 11, 9};
        System.out.println("*** Bubble Sort Algorithm ***" + "\n");
        System.out.println("The Original Array is: " + Arrays.toString(myArray));
        for (int i = 0; i < myArray.length; i++) {
            for (int j = 0; j < myArray.length - 1; j++) {
                if (myArray[j] > myArray[j + 1]) {
                    int a = myArray[j + 1];
                    myArray[j + 1] = myArray[j];
                    myArray[j] = a;
                }
            }
        }
        System.out.println("\nAfter applying BUBBLE SORT algorithm.");
        System.out.println("\nThe Array becomes: " + Arrays.toString(myArray));

    }
}
Share This :

Related Post



sentiment_satisfied Emoticon