Algorithms/JAVA/Sorting_Algorithms/BubbleSort.java
import java.util.Arrays;

public class BubbleSort {
    public static void main(String[] args) {
        int[] arr = {3,1,5,4,2};
        bubble(arr);
        System.out.println(Arrays.toString(arr));
    }
    static void bubble(int[] arr){
        boolean swapped;
        for (int i = 0; i < arr.length; i++) {
            swapped = false;
            //for each step max item will come at last respective index
            for (int j = 1; j < arr.length-i; j++) {
                //swap if item is smaller than previous one
                if(arr[j]<arr[j-1]){
                    //swap
                    int temp = arr[j];
                    arr[j]=arr[j-1];
                    arr[j-1]=temp;
                    swapped = true;
                }
            }
            //if any items were not swapped then array is sorted, stop the loop
            if(!swapped)
                break;
        }
    }
}
