Java bubble sort algorithm code

  • 2020-04-01 02:19:11
  • OfStack



public class  Bubble sort  {
    public int[] bubbleSort(int[] a, int n) {
        for (int i = 0; i < n; i++) {
            int flag = 0;
            for (int j = n - 1; j > i; j--) {// i or i-1 ?
                if (a[j] < a[j - 1]) {
                    int x = a[j];
                    a[j] = a[j - 1];
                    a[j - 1] = x;
                    flag = 1;
                }
            }
            if (flag == 0)
                break;
        }
        return a;
    }
    
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int[] a = new int[] { 25, 56, 32, 20, 1, 5, 89, 3, 8, 41 };
         Bubble sort  sort = new  Bubble sort ();
        sort.bubbleSort(a, a.length);
        for (int i = 0; i < a.length; i++) {
            System.out.print(a[i] + " ");
        }
    }
}


Related articles: