Sorting algorithms/Sleep sort: Difference between revisions

Content added Content deleted
m (Improved the formatting of the jq code (x2))
Line 1,015: Line 1,015:
20 milliseconds is used to (try to) handle scheduler tick on Windows systems (around 15 ms). On Linux systems (after kernel 2.6.8), this value can be smaller.
20 milliseconds is used to (try to) handle scheduler tick on Windows systems (around 15 ms). On Linux systems (after kernel 2.6.8), this value can be smaller.


<lang Oforth>func: sleepSort(l)
<lang Oforth>import: parallel

{
: sleepSort(l)
| ch n |
| ch n |
Channel new ->ch
Channel new ->ch
l forEach: n [ #[ System sleep(n 20 *) ch send(n) ] & ]
l forEach: n [ #[ n dup 20 * sleep ch send drop ] & ]
ListBuffer newSize(l size) #[ ch receive over add ] times(l size)
ListBuffer newSize(l size) #[ ch receive over add ] times(l size) ;</lang>
}</lang>


{{out}}
{{out}}
<pre>
<pre>
sleepSort(100 seq 100 seq +) println
100 seq 100 seq + sleepSort println
[1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10, 11, 11, 12, 12, 13, 13, 14,
[1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10, 11, 11, 12, 12, 13, 13, 14,
14, 15, 15, 16, 16, 17, 17, 18, 18, 19, 19, 20, 20, 21, 21, 22, 22, 23, 23, 24, 24, 25, 2
14, 15, 15, 16, 16, 17, 17, 18, 18, 19, 19, 20, 20, 21, 21, 22, 22, 23, 23, 24, 24, 25, 2