function quick(array, n){
let max=0
let index;
for(let i=0;i<n;i++){
if(max<array[i]){
max = array[i]
index = i
}
}
[array[index],array[n-1]]=[array[n-1],array[index]]
quickSort(array, 0, n - 2)
}
function quickSort(array,leftEnd, rightEnd){
//输入的array是数组
if(leftEnd >= rightEnd) return
let leftCursor = leftEnd
let rightCursor = rightEnd + 1
let pivot = array[leftEnd]
//取首位为pivot
while(true){
do{
leftCursor++
}while(array[leftCursor]<pivot)
do{
rightCursor--
}while(array[rightCursor]>pivot)
if(leftCursor>=rightCursor)break
[array[leftCursor],array[rightCursor]] = [array[rightCursor],array[leftCursor]]
}
array[leftEnd] = array[rightCursor]
array[rightCursor] = pivot
quickSort(array, leftEnd, rightCursor - 1)
quickSort(array, rightCursor + 1, rightEnd)
}
var A = [20,68,13,77,50,16,2,92,38,9,80]
quick(A,A.length)
console.log(A)
快速排序(JavaScript实现)
最新推荐文章于 2024-04-04 01:37:56 发布