var quickSort = function (arr) {
if (arr.length <= 1) {
return arr;
}
var centerIndex = Math.floor(arr.length / 2); // 取中间基准
var center = arr.splice(centerIndex, 1)[0]; // 拿到基准数
var left = [];
var right = [];
for (var i = 0; i < arr.length; i++) {
if (arr[i] < center) {
left.push(arr[i]);
} else {
right.push(arr[i]);
}
}
return quickSort(left).concat([center], quickSort(right)); // 合并
};
// 测试
// var arr = [5, 7, 3, 16, 9, 4];
// console.log(quickSort(arr)); // [3, 4, 5, 7, 9, 16]
原文链接:https://blog.csdn.net/a5252145/java/article/details/105071655