Sorting algorithms/Sleep sort: Difference between revisions

Line 1,201:
<pre>
$ java -jar sleepsort.jar 5 7 -1 2 4 1 8 0 3 9 6
Unsorted: 5 7 2 4 1 8 0 3 9 6
Sorted : 0 1 2 3 4 5 6 7 8 9
</pre>
 
=== Using coroutines ===
<syntaxhighlight lang="kotlin">
import kotlinx.coroutines.*
 
fun sleepSort(list: List<Int>, delta: Long) {
runBlocking {
list.onEach {
launch {
delay(it * delta)
print("$it ")
}
}
}
}
 
fun main() {
val list = listOf(5, 7, 2, 4, 1, 8, 0, 3, 9, 6)
println("Unsorted: ${list.joinToString(" ")}")
print("Sorted : ")
sleepSort(list, 10)
}
 
</syntaxhighlight>
 
Output:
<pre>
Unsorted: 5 7 2 4 1 8 0 3 9 6
Sorted : 0 1 2 3 4 5 6 7 8 9
19

edits