/**
* 1.选择一个元素作为基准:效益基准的移动到左边,大于基准的移动到右边分区操作结束后基准元素位置就是最终排序后的位置
* 2.对基准元素的左边和右边继续第一步的操作,直到排序完成
* 2.
*/
const quickSort = function(arr){
if(arr.length<=1)return arr;
const midIndex = Math.floor(arr.length/2);
const mid = arr.splice(midIndex,1)[0];
const left = [];
const right = [];
arr.map(item=>{
if(item<mid){
left.push(item)
}else{
right.push(item)
}
});
return quickSort(left).concat(mid,quickSort(right))
}
const arr = [9,2,8,11,7,99,33,0,77,22,1,77]
console.log(quickSort(arr))
快速排序算法
于 2023-08-03 14:54:50 首次发布