Bubble sort an array - Node.js Algorithm

Node.js examples for Algorithm:Sort

Description

Bubble sort an array

Demo Code


Array.prototype.bubblesort = function() {
    var done = false;
    while (!done) {
        done = true;/*  ww w.  ja v a  2  s. c om*/
        for (var i = 1; i<this.length; i++) {
            if (this[i-1] > this[i]) {
                done = false;
                [this[i-1], this[i]] = [this[i], this[i-1]]
            }
        }
    }
    return this;
}

Related Tutorials