直接上代码
var a= [51,23,45,78,21,79,65,62,7,48]
function quickSort(a) {
if(a.length <= 1) {
return a;
}
let left = [],right = [],temp = [];
key = Math.floor(a.length / 2);
// temp.push(a[key]);
temp = a.splice(key, 1);
for(var i = 0; i < a.length; i++) {
temp>a[i]?left.push(a[i]):right.push(a[i]);
}
return quickSort(left).concat(temp, quickSort(right));
}
console.log(quickSort(a));
当使用temp = a.splice(key, 1);排序算法没问题可得到预期结果。
当使用temp.push(a[key]);,控制台提示的报错信息是堆栈溢出(Maximum call stack size exceeded)
将这两个值打出来你会发现,这两个值一模一样...
路过的大神们,求解...