Javascript Array quick_sort()

Description

Javascript Array quick_sort()



Array.prototype.quick_sort = function () {
    if (this.length < 2) { return this; }

    var pivot = this[Math.round(this.length / 2)];

    return this.filter(x => x < pivot)
               .quick_sort()/*from w ww. j av  a2s. c om*/
               .concat(this.filter(x => x === pivot))
               .concat(this.filter(x => x > pivot).quick_sort());
};

Javascript Array quick_sort()

Array.prototype.quick_sort = function ()
{
    if (this.length <= 1)
        return this;

    var pivot = this[Math.round(this.length / 2)];

    return this.filter(function (x) { return x <  pivot }).quick_sort().concat(
           this.filter(function (x) { return x == pivot })).concat(
           this.filter(function (x) { return x >  pivot }).quick_sort());
}



PreviousNext

Related