//

Bubble Sort

Bubble sort

import java.util.Arrays;

public class BubbleSort {
    public static void main(String[] args) {
        int[] arr = new int[]{5,4,3,2,1};
        bubbleSort(arr);
        System.out.println(Arrays.toString(arr));
    }

    static void bubbleSort(int[] arr){

        for(int i = 0; i < arr.length - 1;i++){
            for(int j = 1; j < arr.length - i; j++){

                if(arr[j] < arr[j - 1]){
                    int temp = arr[j];
                    arr[j] = arr[j-1];
                    arr[j-1] = temp;
                }
            }
        }
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *