Sorting algorithms/Cycle sort: Difference between revisions

→‎{{header|Kotlin}}: Updated example see https://github.com/dkandalov/rosettacode-kotlin for details
(Added Kotlin)
(→‎{{header|Kotlin}}: Updated example see https://github.com/dkandalov/rosettacode-kotlin for details)
Line 754:
 
/** Sort an array in place and return the number of writes */
fun <T : Comparable<T>> cycleSort(array: Array<T>): Int {
var writes = 0
 
Line 770:
// Otherwise, put the item there or right after any duplicates.
while (item == array[pos]) pos++
varval temp = array[pos]
array[pos] = item
item = temp
Line 779:
// Find where to put the item.
pos = cycleStart
for (i in cycleStart + 1 until array.size) if (array[i] < item) pos++
 
// Otherwise, put the item there or right after any duplicates.
while (item == array[pos]) pos++
varval temp2 = array[pos]
array[pos] = item
item = temp2
Line 792:
}
 
fun <T : Comparable<T>> printResults(array: Array<T>) {
println(array.asList())
val writes = cycleSort(array)
Line 804:
printResults(array)
val array2 = arrayOf(5, 0, 1, 2, 2, 3, 5, 1, 1, 0, 5, 6, 9, 8, 0, 1)
printResults(array2)
val array3 = "the quick brown fox jumps over the lazy dog".split(' ').toTypedArray()
printResults(array3)
val array4 = "sphinx of black quartz judge my vow".replace(" ", "").toCharArray().distinct().toTypedArray()
printResults(array4)
}</lang>