function qsort(list, left, right, desc) {
const stack = [left, right]
let cnt = performance.now()
while(stack.length) {
cnt++
let end = stack.pop()
let begin = stack.pop()
let i = begin, j = end
if (i >= j) {
continue
}
let pivot = list[i]
if (!desc) {
while(i < j) {
while(i < j && list[j] >= pivot) {
j--
}
list[i] = list[j]
while(i < j && list[i] <= pivot) {
i++
}
list[j] = list[i]
}
} else {
while(i < j) {
while(i < j && list[j] <= pivot) {
j--
}
list[i] = list[j]
while(i < j && list[i] >= pivot) {
i++
}
list[j] = list[i]
}
}
list[i] = pivot
stack.push(i+1)
stack.push(end)
stack.push(begin)
stack.push(i-1)
}
return list
}
let test = []
for (let i = 0; i < 100000; i++) {
const tmp = Math.floor(Math.random() * 10000 + 1)
test.push(tmp)
}
const asc = qsort([...test], 0, test.length - 1)
console.log(asc)
const desc = qsort([...test], 0, test.length - 1, true)
console.log(desc)
console