快速排序-quickSort

快速排序

const partition=(list,left,right)=>{
    let i=left,j=right,key=list[i]
    while(i<j){
        while(i<j&&list[j]>=key)j--
        if(i<j)list[i]=list[j]
        while(i<j&&list[i]<=key)i++
        if(i<j)list[j]=list[i]    
    }
    list[i]=key
    return i
}
const quickSort=(list,left,right)=>{
    if(left<right){
        const pos=partition(list,left,right)
        quickSort(list,left,pos-1)
        quickSort(list,pos+1,right)
    }
}
const list=[9,8,7,6,5,4,3,2,1]
quickSort(list,0,list.length-1)
console.log(list)

 

posted @ 2019-03-19 10:31  朱现国  阅读(152)  评论(0编辑  收藏  举报