fun <T : Comparable<T>> List<T>.quickSort(): List<T> = when { size < 2 -> this else -> { val pivot = first() val (smaller, greater) = drop(1).partition { it <= pivot } smaller.quickSort() + pivot + greater.quickSort() } } fun main() { print(listOf(5, 0, 1, 5, 3, 7, 4, 2).quickSort()) }
![JetBrains Mono: A free and open source typeface for developers](https://cdn-ak-scissors.b.st-hatena.com/image/square/58440675baa3b1619571217f9fe70469cd39a1bc/height=288;version=1;width=512/https%3A%2F%2Fwww.jetbrains.com%2Flp%2Fmono%2Fstatic%2Fpreview-38b2d6922a253515b0fddf9bc56b00ff.png)