JS排序算法之快速排序

const Arr = [85, 24, 63, 45, 17, 31, 96, 50];
function quickSort(arr) {
80
if (arr.length <= 1) {
return arr;
}
let pivotIndex = Math.floor(arr.length / 2);
let pivot = arr.splice(pivotIndex, 1)[0];
let left = [];
let right = [];
for (let i = 0; i < arr.length; i++) {
if (arr[i] < pivot) {
left.push(arr[i]);
} else {
right.push(arr[i]);
}
}
// 递归
return quickSort(left).concat([pivot], quickSort(right));
}
console.log(quickSort(Arr));
上一篇:Phonegap 开发环境搭建


下一篇:phoneGap开发环境搭建(android)