JSFiddle - React, Tailwind, and code Playground

by dru_zod

JavaScript

function bubbleSort(a) {
    if (a.length < 2)
        return a;
    
    var i, t;
    var n = a.length;
    var s = true;
    
    while (s) {
        n--;
        s = false;
        for (i=0; i<n; i++) {
            if (a[i+1] < a[i]) {
                t = a[i];
                a[i] = a[i+1];
                a[i+1] = t;
                s = true;
            }
        }
    }
    
    return a;
}


alert(bubbleSort([9, 2, 8, 0, 7, 8, 1, 2, 0, 6, 4]));