2.快速排序

Array.prototype.quick_sort = function() {

var len = this.length;

if (len <= 1) {

    return this.slice(0);

 }

var left = [];

var right = [];

var mid = [this[0]];

for (var i = 1; i < len; i++) {

    if (this[i] < mid[0]) { left.push(this[i]) }

    else {right.push(this[i])}

}

return left.quick_sort().concat(mid.concat(right.quick_sort()))

}

var num = [22, 34, 3, 32, 82,434,2,4];

var numsort = num.quick_sort();

console.log(numsort)

你可能感兴趣的:(2.快速排序)