JS 手写系列 (一)快速排序

310 阅读1分钟

注意点:

分治的思想主要集中在最后return

千万别忘了pivot加入到最后的队列里

fucntion quickSort (arr) {
    if(arr.length===0) return arr;
    const index = Math.floor(arr.length/2);
    const pivot = arr.splice(index, 1);
    const left = [], right = [];
    for(let i = 0; i< ar.length; i++) {
        if(arr[i]<pivot){
            left.push(arr[i]);
        } 
        right.push(arr[i]);
    }
    
    return quickSort(left).concat([pivot], quickSort(right));
}