Java program to implement bubble sort algorithm

Bubble Sort is a simple sorting algorithm that works by repeatedly swapping adjacent elements if they are in the wrong order. Here's a Java program that implements the Bubble Sort algorithm:

public class BubbleSort {
    public static void main(String[] args) {
        int[] arr = { 64, 34, 25, 12, 22, 11, 90 };
        int n = arr.length;

        // iterate over the array n-1 times
        for (int i = 0; i < n - 1; i++) {
            // iterate over the unsorted part of the array
            for (int j = 0; j < n - i - 1; j++) {
                // swap adjacent elements if they are in the wrong order
                if (arr[j] > arr[j + 1]) {
                    int temp = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = temp;
                }
            }
        }

        // print the sorted array
        System.out.println("Sorted array:");
        for (int i = 0; i < n; i++) {
            System.out.print(arr[i] + " ");
        }
    }
}
Sou‮.www:ecr‬theitroad.com

In this program, we first define an array of integers arr. We then iterate over the array n-1 times, where n is the length of the array. In each iteration, we iterate over the unsorted part of the array and swap adjacent elements if they are in the wrong order. This way, the largest element in the unsorted part of the array "bubbles up" to the end of the array in each iteration. Finally, we print the sorted array.

The output of the program should be:

Sorted array:
11 12 22 25 34 64 90