1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
|
function quickSort(arr){
function partition(arr,left,right){
let pivot = arr[right]
let storeIndex = left
for(let i = left;i<right;i++){
if(arr[i]<pivot){
[arr[left],arr[storeIndex]] = [arr[storeIndex],arr[left]]
storeIndex++
}
}
[arr[storeIndex],arr[right]] = [arr[right],arr[storeIndex]]
return storeIndex
}
function sort(arr,left,right){
if(left>=right) return
let storeIndex = partition(arr,left,right)
console.log(storeIndex)
sort(arr,left,storeIndex-1)
sort(arr,storeIndex+1,right)
}
sort(arr,0,arr.length-1)
return arr
}
console.log(quickSort([8, 4, 90,1000,29]));
|