Bubble Sort

average O(n^2) - best O(n) - worst O(n^2)

space O(1)

public static void bubbleSort(int[] a) {
    if (a == null || a.length <= 1) {
        return;
    }
    boolean swap= true;
    while (swap) {
        swap = false;
        for (int i = 0; i < a.length - 1; i++) {
            if (a[i] > a[i + 1]) {
                int temp = a[i];
                a[i] = a[i + 1];
                a[i + 1] = temp;

                swap = true;
            }
        }       
    }
}

results matching ""

    No results matching ""