$$
\Omicron\lparen n \log n \rparen
$$
Kotlin代码实现:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
fun quickSort(list: List<Int>): List<Int> {
if (list.size < 2) return list
val pivot = list[list.size / 2]
val less = list.filter { it < pivot }
val equal = list.filter { it == pivot }
val greater = list.filter { it > pivot }
return quickSort(less) + equal + quickSort(greater)
}
@Test
fun testQuickSort() {
val srcList = mutableListOf(5, 1, 10, 2, 30, -1, 6)
val dstList = quickSort(srcList)
println(dstList)
}
|
katex