时间复杂度:O(nlog2^n)
Array.prototype.quick = function quick() {
let _this = this;
// 如果处理的数组只有一项或者空的,则无需处理了
if (_this.length <= 1) {
return _this;
}
// 获取中间项,并且把中间项在数组中删除
let middleIndex = Math.floor(_this.length / 2),
middleValue = _this.splice(middleIndex, 1)[0];
let arrLeft = [],
arrRight = [];
for (let i = 0; i < _this.length; i++) {
let item = _this[i];
item < middleValue ? arrLeft.push(item) : arrRight.push(item);
}
return quick.call(arrLeft).concat(middleValue, quick.call(arrRight));
};