Reputation: 610
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?
Upvotes: 16
Views: 6198
Reputation: 31458
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()
which (in Kotlin/JVM) leads to:
public fun <T> Array<out T>.sort(): Unit {
if (size > 1) java.util.Arrays.sort(this)
}
This will result in java.util.ComparableTimSort
being used (see the sort(Object[] a)
)
val array = intArrayOf(3,2,1)
array.sort()
which (in Kotlin/JVM) leads to:
public actual fun IntArray.sort(): Unit {
if (size > 1) java.util.Arrays.sort(this)
}
This will result in java.util.DualPivotQuicksort
being used (see the sort(int[] a)
)
Upvotes: 12