Sorting algorithms/Selection sort: Difference between revisions

Content deleted Content added
Kotlin entry
Line 1,240: Line 1,240:
[-57,-52,-39,-35,-28,-20,-16,-15,-12,-11,12,21,21,37,44,51,55,70,83,85]
[-57,-52,-39,-35,-28,-20,-16,-15,-12,-11,12,21,21,37,44,51,55,70,83,85]
</pre>
</pre>

=={{header|Kotlin}}==
{{trans|C#}}
<lang scala>fun <T : Comparable<T>> Array<T>.selection_sort() {
for (i in indices) {
var k = i
for (j in i..size - 1)
if (j != i && this[j] < this[k])
k = j

if (k != i) {
val tmp = this[i]
this[i] = this[k]
this[k] = tmp
}
}
}

fun main(args: Array<String>) {
val i = arrayOf(4, 9, 3, -2, 0, 7, -5, 1, 6, 8)
i.selection_sort()
println(i.joinToString())

val s = Array(i.size, { -i[it].toShort() })
s.selection_sort()
println(s.joinToString())

val c = arrayOf('z', 'h', 'd', 'c', 'a')
c.selection_sort()
println(c.joinToString())
}</lang>
{{out}}
<pre>-5, -2, 0, 1, 3, 4, 6, 7, 8, 9
-9, -8, -7, -6, -4, -3, -1, 0, 2, 5
a, c, d, h, z</pre>


=={{header|Liberty BASIC}}==
=={{header|Liberty BASIC}}==