Bubble Sort


public class BubbleSort {

    public static void main(String[] args) {
        int[] myArray=new int[]{10,47,3,89,37}//myArray of 5 elements is to be sorted with bubble sort
        int j;
        boolean flag = true;                    // set flag to true to begin first pass
        int temp;                               //holding variable

        while (flag) {
            flag = false;                       //set flag to false awaiting a possible swap
            for (j = 0; j < myArray.length - 1; j++) {
                if (myArray[j< myArray[j + 1]) // change to > for ascending sort
                {
                    temp = myArray[j];                //swap elements
                    myArray[j= myArray[j + 1];
                    myArray[j + 1= temp;
                    flag = true;                //shows a swap occurred
                }
            }
        }               //After the while loop sorting is completed.Now we can display the sorted array
        System.out.println("Sorted array :");
        for(int i=0;i<myArray.length;i++){
            System.out.println(myArray[i]);
        }
    }
}

Output:
Sorted array :
89
47
37
10
3