js之快速排序算法

function quickSort(arr = [3, 7, 20, 1, 10, 6, 15, 5, 12]) {
if (arr.length <= 1) return arr
 
const leftArr,rightArr  = [],
let current = null
 
current = arr.splice(0, 1)
 
for (let i = 0; i < arr.length; i++) {
  arr[i] < current ? leftArr.push(arr[i]) : rightArr.push(arr[i])
}
 
return quickSort(leftArr).concat(current, quickSort(rightArr))
}

猜你喜欢

转载自www.cnblogs.com/rrrjc/p/11258632.html