默认情况下,Kotlin使用哪种排序方式? [英] What kind of sorting does Kotlin use by default?
本文介绍了默认情况下,Kotlin使用哪种排序方式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
.sort()、. sortWith()等方法使用哪种排序方式?
Which sorting is using for .sort(), .sortWith() etc methods?
val array = arrayOf(3,2,1)
用于不同类型和大小的数组的算法是否有所不同?
Are there some differences in algorithms for arrays of different types and sizes?
推荐答案
Just to extend on what Marko Toplnik said in the comment: Be careful how you create your arrays, because based on that, different sort
functions will be used.
val array = arrayOf(3,2,1)
array.sort()
(在Kotlin/JVM中)导致的结果:
which (in Kotlin/JVM) leads to:
public fun <T> Array<out T>.sort(): Unit {
if (size > 1) java.util.Arrays.sort(this)
}
这将导致 sort(Object[] a)
)
This will result in java.util.ComparableTimSort
being used (see the sort(Object[] a)
)
val array = intArrayOf(3,2,1)
array.sort()
(在Kotlin/JVM中)导致的结果:
which (in Kotlin/JVM) leads to:
public actual fun IntArray.sort(): Unit {
if (size > 1) java.util.Arrays.sort(this)
}
这将导致 sort(int[] a)
)
This will result in java.util.DualPivotQuicksort
being used (see the sort(int[] a)
)
这篇关于默认情况下,Kotlin使用哪种排序方式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文