fun quickSort(array: MutableList<Int>, left: Int, right: Int) {
if (right <= left) {
return
}
val pivot = array[(left + right) / 2]
var i = left
var j = right
while (i <= j) {
while (array[i] < pivot) {
i += 1
}
while (array[j] > pivot) {
j -= 1
}
if (i > j) {
break
}
val tmp = array[i]
array[i] = array[j]
array[j] = tmp
i++
j--
}
quickSort(array, left, j)
quickSort(array, i, right)
}
fun main() {
val array = mutableListOf(7, 3, 0, 1, 5, 2, 5, 19, 10, 5)
quickSort(array, 0, array.count() - 1)
println(array)
}
ZnVuIHF1aWNrU29ydChhcnJheTogTXV0YWJsZUxpc3Q8SW50PiwgbGVmdDogSW50LCByaWdodDogSW50KSB7CiAgICBpZiAocmlnaHQgPD0gbGVmdCkgewogICAgICAgIHJldHVybgogICAgfQoKICAgIHZhbCBwaXZvdCA9IGFycmF5WyhsZWZ0ICsgcmlnaHQpIC8gMl0KICAgIHZhciBpID0gbGVmdAogICAgdmFyIGogPSByaWdodAoKICAgIHdoaWxlIChpIDw9IGopIHsKICAgICAgICB3aGlsZSAoYXJyYXlbaV0gPCBwaXZvdCkgewogICAgICAgICAgICBpICs9IDEKICAgICAgICB9CgogICAgICAgIHdoaWxlIChhcnJheVtqXSA+IHBpdm90KSB7CiAgICAgICAgICAgIGogLT0gMQogICAgICAgIH0KCiAgICAgICAgaWYgKGkgPiBqKSB7CiAgICAgICAgICAgIGJyZWFrCiAgICAgICAgfSAKCiAgICAgICAgdmFsIHRtcCA9IGFycmF5W2ldCiAgICAgICAgYXJyYXlbaV0gPSBhcnJheVtqXQogICAgICAgIGFycmF5W2pdID0gdG1wCgogICAgICAgIGkrKwogICAgICAgIGotLQogICAgfQogICAgCiAgICBxdWlja1NvcnQoYXJyYXksIGxlZnQsIGopCiAgICBxdWlja1NvcnQoYXJyYXksIGksIHJpZ2h0KQp9CgpmdW4gbWFpbigpIHsKICAgIHZhbCBhcnJheSA9IG11dGFibGVMaXN0T2YoNywgMywgMCwgMSwgNSwgMiwgNSwgMTksIDEwLCA1KQoKICAgIHF1aWNrU29ydChhcnJheSwgMCwgYXJyYXkuY291bnQoKSAtIDEpCgogICAgcHJpbnRsbihhcnJheSkKfQ==