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));

 

posted @ 2018-12-25 16:27  zt123123  阅读(193)  评论(0编辑  收藏  举报