Sorting algorithms/Sleep sort: Difference between revisions

m
syntax highlighting fixup automation
(Implementation for Visual Basic .NET added)
m (syntax highlighting fixup automation)
Line 10:
 
=={{header|Ada}}==
<langsyntaxhighlight Adalang="ada">with Ada.Text_IO;
with Ada.Command_Line; use Ada.Command_Line;
procedure SleepSort is
Line 24:
TaskList(i) := new PrintTask(Integer'Value(Argument(i)));
end loop;
end SleepSort;</langsyntaxhighlight>
{{out}}
<pre>./sleepsort 35 21 11 1 2 27 32 7 42 20 50 42 25 41 43 14 46 20 30 8
Line 30:
 
=={{header|APL}}==
<syntaxhighlight lang="apl">
<lang APL>
sleepsort←{{r}⎕TSYNC{r,←⊃⍵,⎕DL ⍵}&¨⍵,r←⍬}
</syntaxhighlight>
</lang>
 
=={{header|AutoHotkey}}==
<langsyntaxhighlight AutoHotkeylang="autohotkey">items := [1,5,4,9,3,4]
for i, v in SleepSort(items)
result .= v ", "
Line 55:
global Sorted
Sorted.Push(v)
}</langsyntaxhighlight>
{{out}}
<pre>[1, 3, 4, 4, 5, 9]</pre>
 
=={{header|Bash}}==
<langsyntaxhighlight lang="bash">
function sleep_and_echo {
sleep "$1"
Line 71:
 
wait
</syntaxhighlight>
</lang>
 
{{out}}
Line 102:
{{works with|BBC BASIC for Windows}}
This does not explicitly 'sleep', but uses timers to implement the different delays.
<langsyntaxhighlight lang="bbcbasic"> INSTALL @lib$+"TIMERLIB"
DIM test%(9)
Line 125:
DEF PROCtask7 : PRINT test%(7) : ENDPROC
DEF PROCtask8 : PRINT test%(8) : ENDPROC
DEF PROCtask9 : PRINT test%(9) : ENDPROC</langsyntaxhighlight>
'''Output:'''
<pre>
Line 141:
 
=={{header|Brainf***}}==
<syntaxhighlight lang="c">
<lang C>
>>>>>,----------[++++++++
++[->+>+<<]>+>[-<<+>>]+++
Line 150:
->>>>>[>>>>>]<-<<<<[<<<<<
]+<]<<<<]>>>>>[>>>>>]<]
</syntaxhighlight>
</lang>
Not exactly 'sleep' sort but it is similar: it inputs an array of digits and in each iteration reduces elements by 1. When an element becomes 0 – it prints the original digit.
 
Line 158:
 
=={{header|C}}==
<langsyntaxhighlight Clang="c">#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
Line 170:
wait(0);
return 0;
}</langsyntaxhighlight>
Running it:<syntaxhighlight lang="text">% ./a.out 5 1 3 2 11 6 4
1
2
Line 178:
5
6
11</langsyntaxhighlight>
If you worry about time efficiency of this sorting algorithm (ha!), you can make it a 100 times faster by replacing the <code>sleep(...</code> with <code>usleep(10000 * (c = atoi(v[c])))</code>. The smaller the coefficient, the faster it is, but make sure it's not comparable to your kernel clock ticks or the wake up sequence will be wrong.
 
=={{header|C sharp|C#}}==
<langsyntaxhighlight lang="csharp">using System;
using System.Collections.Generic;
using System.Linq;
Line 207:
SleepSort(arguments.Select(int.Parse));
}
}</langsyntaxhighlight>
 
===Using Tasks===
 
<langsyntaxhighlight lang="csharp">var input = new[] { 1, 9, 2, 1, 3 };
 
foreach (var n in input)
Line 219:
Console.WriteLine(n);
});
</syntaxhighlight>
</lang>
 
Output, i.e. in LINQPad:
Line 230:
 
=={{header|C++}}==
<langsyntaxhighlight lang="cpp">
#include <chrono>
#include <iostream>
Line 251:
}
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 274:
=={{header|Clojure}}==
Using core.async
<langsyntaxhighlight lang="clojure">(ns sleepsort.core
(require [clojure.core.async :as async :refer [chan go <! <!! >! timeout]]))
 
Line 282:
(go (<! (timeout (* 1000 i)))
(>! c i)))
(<!! (async/into [] (async/take (count l) c)))))</langsyntaxhighlight>
<langsyntaxhighlight lang="clojure">(sleep-sort [4 5 3 1 2 7 6])
;=> [1 2 3 4 5 6 7]</langsyntaxhighlight>
 
=={{header|CoffeeScript}}==
{{works_with|node.js}}
<langsyntaxhighlight lang="coffeescript">
after = (s, f) -> setTimeout f, s*1000
 
Line 300:
input = (parseInt(arg) for arg in process.argv[2...])
sleep_sort input
</syntaxhighlight>
</lang>
output
<syntaxhighlight lang="text">
> time coffee sleep_sort.coffee 5, 1, 3, 4, 2
1
Line 313:
user 0m0.147s
sys 0m0.024s
</syntaxhighlight>
</lang>
 
=={{header|Common Lisp}}==
{{works_with|SBCL}}
<langsyntaxhighlight lang="lisp">(defun sleeprint(n)
(sleep (/ n 10))
(format t "~a~%" n))
Line 324:
(sb-thread:make-thread (lambda() (sleeprint (parse-integer arg)))))
 
(loop while (not (null (cdr (sb-thread:list-all-threads)))))</langsyntaxhighlight>
{{Out}}
<pre>$ sbcl --script ss.cl 3 1 4 1 5
Line 335:
 
=={{header|D}}==
<langsyntaxhighlight lang="d">void main(string[] args)
{
import core.thread, std;
Line 343:
write(a, " ");
});
}</langsyntaxhighlight>
{{out}}
<pre>$ ./sorting_algorithms_sleep_sort 200 20 50 10 80
Line 349:
 
=={{header|Dart}}==
<langsyntaxhighlight lang="dart">
void main() async {
Future<void> sleepsort(Iterable<int> input) => Future.wait(input
Line 356:
await sleepsort([3, 10, 2, 120, 122, 121, 54]);
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 369:
 
=={{header|Delphi}}==
<langsyntaxhighlight Delphilang="delphi">program SleepSortDemo;
 
{$APPTYPE CONSOLE}
Line 427:
Writeln;
ReadLn;
end.</langsyntaxhighlight>
Output:
<pre>
Line 436:
=={{header|Elena}}==
ELENA 5.0 :
<langsyntaxhighlight lang="elena">import extensions;
import system'routines;
import extensions'threading;
Line 467:
console.readChar()
}</langsyntaxhighlight>
 
=={{header|Elixir}}==
{{trans|Erlang}}
<langsyntaxhighlight lang="elixir">defmodule Sort do
def sleep_sort(args) do
Enum.each(args, fn(arg) -> Process.send_after(self, arg, 5 * arg) end)
Line 486:
end
 
Sort.sleep_sort [2, 4, 8, 12, 35, 2, 12, 1]</langsyntaxhighlight>
 
{{out}}
Line 503:
GNU Emacs supports threads, but it's more straightforward to do this by just using timers.
Evaluate in the *scratch* buffer by typing <code>C-M-x</code> on the expression:
<langsyntaxhighlight Lisplang="lisp">(dolist (i '(3 1 4 1 5 92 65 3 5 89 79 3))
(run-with-timer (* i 0.001) nil 'message "%d" i))</langsyntaxhighlight>
 
The output printed in the *Messages* buffer is:
Line 519:
 
=={{header|Erlang}}==
<langsyntaxhighlight lang="erlang">#!/usr/bin/env escript
%% -*- erlang -*-
%%! -smp enable -sname sleepsort
Line 536:
io:format("~s~n", [Num]),
loop(N - 1)
end.</langsyntaxhighlight>
{{out}}
<pre>./sleepsort 2 4 8 12 35 2 12 1
Line 549:
 
=={{header|Euphoria}}==
<langsyntaxhighlight lang="euphoria">include get.e
 
integer count
Line 579:
task_yield()
end while
end if</langsyntaxhighlight>
 
=={{header|F_Sharp|F#}}==
<langsyntaxhighlight lang="fsharp">
let sleepSort (values: seq<int>) =
values
Line 592:
|> Async.Ignore
|> Async.RunSynchronously
</syntaxhighlight>
</lang>
 
Usage:
<langsyntaxhighlight lang="fsharp">
sleepSort [10; 33; 80; 32]
10
Line 601:
33
80
</syntaxhighlight>
</lang>
 
=={{header|Factor}}==
 
<syntaxhighlight lang="factor">
<lang Factor>
USING: threads calendar concurrency.combinators ;
 
: sleep-sort ( seq -- ) [ dup seconds sleep . ] parallel-each ;
</syntaxhighlight>
</lang>
 
Usage:
 
<syntaxhighlight lang="factor">
<lang Factor>
{ 1 9 2 6 3 4 5 8 7 0 } sleep-sort
</syntaxhighlight>
</lang>
 
=={{header|Fortran}}==
<syntaxhighlight lang="fortran">
<lang Fortran>
program sleepSort
use omp_lib
Line 652:
end subroutine sleepNprint
end program sleepSort
</syntaxhighlight>
</lang>
Compile and Output:
<pre>
Line 669:
Can't use FreeBASIC '''sleep''' since it halts the program.
Instead it uses a second array filled with times based on the value of number, this array is check against the timer. If the timer is past the stored time the value is printed.
<langsyntaxhighlight lang="freebasic">' version 21-10-2016
' compile with: fbc -s console
' compile with: fbc -s console -exx (for bondry check on the array's)
Line 726:
Print : Print "hit any key to end program"
Sleep
End</langsyntaxhighlight>
{{out}}
<pre>unsorted 5 2 5 6 4 6 9 5 1 2 0
Line 733:
 
=={{header|Go}}==
<langsyntaxhighlight lang="go">package main
 
import (
Line 758:
fmt.Println(<-out)
}
}</langsyntaxhighlight>
Usage and output:
<pre>./sleepsort 3 1 4 1 5 9
Line 770:
 
=== Using sync.WaitGroup ===
<langsyntaxhighlight lang="go">package main
 
import (
Line 797:
}
wg.Wait()
}</langsyntaxhighlight>
 
Usage and output are the same as the version using channels. Note that the original version would sleep for increments of 1 full second, so I made my code do the same.
 
=={{header|Groovy}}==
<langsyntaxhighlight lang="groovy">
@Grab(group = 'org.codehaus.gpars', module = 'gpars', version = '1.2.1')
import groovyx.gpars.GParsPool
Line 812:
}
}
</syntaxhighlight>
</lang>
 
Sample Run:
Line 825:
=={{header|Haskell}}==
 
<langsyntaxhighlight lang="haskell">import System.Environment
import Control.Concurrent
import Control.Monad
Line 836:
 
main :: IO ()
main = getArgs >>= sleepSort . map read</langsyntaxhighlight>
 
===Using mapConcurrently_===
<langsyntaxhighlight lang="haskell">import System.Environment
import Control.Concurrent
import Control.Concurrent.Async
Line 847:
 
main :: IO ()
main = getArgs >>= sleepSort . map read</langsyntaxhighlight>
 
This is problematic for inputs with multiple duplicates like <code>[1,2,3,1,4,1,5,1]</code> because simultaneous <code>print</code>s are done concurrently and the 1s and newlines get output in jumbled up order. The channels-based version above doesn't have this problem.
Line 855:
The following solution only works in Unicon.
 
<langsyntaxhighlight lang="unicon">procedure main(A)
every insert(t:=set(),mkThread(t,!A))
every spawn(!t) # start threads as closely grouped as possible
Line 863:
procedure mkThread(t,n) # 10ms delay scale factor
return create (delay(n*10),delete(t,&current),n@>&main)
end</langsyntaxhighlight>
 
Sample run:
Line 883:
 
{{works with|J|903+}}
<langsyntaxhighlight Jlang="j">scheduledumb=: {{
id=:'dumb',":x:6!:9''
wd 'pc ',id
Line 897:
{{echo R}} scheduledumb poly"0 >:>./ y
EMPTY
}}</langsyntaxhighlight>
 
Task example:
 
<langsyntaxhighlight Jlang="j"> t=: ?~30
t
11 7 22 16 17 2 1 19 23 29 9 21 15 10 12 27 3 4 24 20 14 5 26 18 8 6 0 13 25 28
ssort t
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29</langsyntaxhighlight>
 
Note that since t is the result of an RNG, the order of values in t would be different in subsequent attempts. For example:
 
<langsyntaxhighlight Jlang="j"> t=: ?~30
t
23 26 24 25 10 12 4 5 7 27 16 17 14 8 3 15 18 13 19 21 2 28 22 9 6 20 11 1 29 0
ssort t
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29</langsyntaxhighlight>
 
=={{header|Java}}==
{{works with|Java|1.5+}}
<langsyntaxhighlight lang="java5">import java.util.concurrent.CountDownLatch;
 
public class SleepSort {
Line 947:
sleepSortAndPrint(nums);
}
}</langsyntaxhighlight>
Output (using "3 1 4 5 2 3 1 6 1 3 2 5 4 6" as arguments):
<pre>1
Line 965:
 
=={{header|JavaScript}}==
<langsyntaxhighlight lang="javascript">Array.prototype.timeoutSort = function (f) {
this.forEach(function (n) {
setTimeout(function () { f(n) }, 5 * n)
});
}
</syntaxhighlight>
</lang>
Usage and output:
<langsyntaxhighlight lang="javascript">[1, 9, 8, 7, 6, 5, 3, 4, 5, 2, 0].timeoutSort(function(n) { document.write(n + '<br>'); })</langsyntaxhighlight>
<pre>
0
Line 986:
</pre>
 
<langsyntaxhighlight lang="javascript">Array.prototype.sleepSort = function(callback) {
const res = [];
for (let n of this)
Line 999:
[1, 9, 8, 7, 6, 5, 3, 4, 5, 2, 0].sleepSort(console.log);
// [ 1, 0, 2, 3, 4, 5, 5, 6, 7, 8, 9 ]
</syntaxhighlight>
</lang>
 
=={{header|jq}}==
Line 1,006:
Doesn't actually sleep. Instead, iterates reducing the values by one until each is zero.
 
<langsyntaxhighlight lang="jq">echo '[5, 1, 3, 2, 11, 6, 4]' | jq '
def f:
if .unsorted == [] then
Line 1,016:
end;
{unsorted: [.[] | {v: ., t: .}], sorted: []} | f | .[]
'</langsyntaxhighlight>
{{out}}
<pre>1
Line 1,029:
{{works with|Julia|1.6}}
 
<langsyntaxhighlight lang="julia">function sleepsort(V::Vector{T}) where {T <: Real}
U = Vector{T}()
sizehint!(U, length(V))
Line 1,044:
 
v = rand(-10:10, 10)
println("# unordered: $v\n -> ordered: ", sleepsort(v))</langsyntaxhighlight>
 
{{out}}
Line 1,051:
 
=={{header|Kotlin}}==
<langsyntaxhighlight lang="scala">// version 1.1.51
 
import kotlin.concurrent.thread
Line 1,073:
println("Unsorted: ${list.joinToString(" ")}")
sleepSort(list, 50)
}</langsyntaxhighlight>
 
Sample output:
Line 1,085:
Here's a slow implementation using only stock C Lua:
 
<langsyntaxhighlight lang="lua">function sleeprint(n)
local t0 = os.time()
while os.time() - t0 <= n do
Line 1,111:
end
end
end</langsyntaxhighlight>
 
By installing LuaSocket, you can get better than 1-second precision on the clock, and therefore faster output:
 
<langsyntaxhighlight lang="lua">socket = require 'socket'
 
function sleeprint(n)
Line 1,143:
end
end
end</langsyntaxhighlight>
 
Either way, the output is the same:
Line 1,164:
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<langsyntaxhighlight lang="mathematica">SleepSort = RunScheduledTask[Print@#, {#, 1}] & /@ # &;
SleepSort@{1, 9, 8, 7, 6, 5, 3, 4, 5, 2, 0};</langsyntaxhighlight>
{{Out}}
<pre>
Line 1,183:
As implemented this sample goes beyond the scope of the task as defined; it will handle negative numbers.
 
<langsyntaxhighlight NetRexxlang="netrexx">/* NetRexx */
options replace format comments java crossref symbols nobinary
import java.util.concurrent.CountDownLatch
Line 1,242:
parent.getDoneLatch().countDown() -- this one's done; decrement the latch
return
</syntaxhighlight>
</lang>
'''Output:'''
<pre>
Line 1,251:
=={{header|Nim}}==
Compile with <code>nim --threads:on c sleepsort</code>:
<langsyntaxhighlight lang="nim">import os, strutils
 
proc single(n: int) =
Line 1,263:
thr.joinThreads
 
main()</langsyntaxhighlight>
Usage:
<pre>$ ./sleepsort 5 1 3 2 11 6 4
Line 1,275:
 
=={{header|Objeck}}==
<langsyntaxhighlight lang="objeck">
use System.Concurrency;
use Collection;
Line 1,307:
}
}
</syntaxhighlight>
</lang>
 
=={{header|Objective-C}}==
<langsyntaxhighlight lang="objc">#import <Foundation/Foundation.h>
 
int main(int argc, char **argv)
Line 1,323:
}
[queue waitUntilAllOperationsAreFinished];
}</langsyntaxhighlight>
Rather than having multiple operations that sleep, we could also dispatch the tasks after a delay:
<langsyntaxhighlight lang="objc">#import <Foundation/Foundation.h>
 
int main(int argc, char **argv)
Line 1,335:
^{ NSLog(@"%d\n", i); });
}
}</langsyntaxhighlight>
 
=={{header|Oforth}}==
Line 1,343:
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.
 
<langsyntaxhighlight Oforthlang="oforth">import: parallel
 
: sleepSort(l)
Line 1,349:
Channel new ->ch
l forEach: n [ #[ n dup 20 * sleep ch send drop ] & ]
ListBuffer newSize(l size) #[ ch receive over add ] times(l size) ;</langsyntaxhighlight>
 
{{out}}
Line 1,366:
 
=={{header|Ol}}==
<langsyntaxhighlight lang="scheme">
(define (sleep-sort lst)
(for-each (lambda (timeout)
Line 1,375:
 
(sleep-sort '(5 8 2 7 9 10 5))
</syntaxhighlight>
</lang>
{{Out}}
<pre>
Line 1,390:
{{works with|Free Pascal}}
my limit under linux was 4000 threads nearly 2 GB.
<langsyntaxhighlight lang="pascal">
program sleepsort;
{$IFDEF FPC}
Line 1,490:
readln;
{$ENDIF}
end.</langsyntaxhighlight>
{{out}}
<pre>time ./sleepsort
Line 1,501:
=={{header|Perl}}==
Basically the C code.
<langsyntaxhighlight Perllang="perl">1 while ($_ = shift and @ARGV and !fork);
sleep $_;
print "$_\n";
wait;</langsyntaxhighlight>
 
 
A more optimal solution makes use of Coro, a cooperative threading library. It has the added effect of being much faster, fully deterministic (sleep is not exact), and it allows you to easily collect the return value:
<langsyntaxhighlight Perllang="perl">use Coro;
$ret = Coro::Channel->new;
@nums = qw(1 32 2 59 2 39 43 15 8 9 12 9 11);
Line 1,519:
}
 
print $ret->get,"\n" for 1..@nums;</langsyntaxhighlight>
 
=={{header|Phix}}==
Based on [[Sorting_algorithms/Sleep_sort#Euphoria|Euphoria]]
<!--<langsyntaxhighlight Phixlang="phix">(notonline)-->
<span style="color: #008080;">without</span> <span style="color: #008080;">js</span> <span style="color: #000080;font-style:italic;">-- (multitasking)</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">count</span>
Line 1,551:
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<!--</langsyntaxhighlight>-->
 
=={{header|PicoLisp}}==
===Sleeping in main process===
<langsyntaxhighlight PicoLisplang="picolisp">(de sleepSort (Lst)
(make
(for (I . N) Lst
Line 1,562:
(pop 'Lst)
(task (- I)) ) )
(wait NIL (not Lst)) ) )</langsyntaxhighlight>
===Sleeping in child processes===
<langsyntaxhighlight PicoLisplang="picolisp">(de sleepSort (Lst)
(make
(for N Lst
Line 1,571:
(pop 'Lst)
(task (close @)) ) )
(wait NIL (not Lst)) ) )</langsyntaxhighlight>
Output in both cases:
<pre>: (sleepSort (3 1 4 1 5 9 2 6 5))
Line 1,577:
===Just printing (no sorted result list)===
Basically the C code.
<langsyntaxhighlight PicoLisplang="picolisp">(for N (3 1 4 1 5 9 2 6 5)
(unless (fork)
(call 'sleep N)
(msg N)
(bye) ) )</langsyntaxhighlight>
Output:
<pre>1
Line 1,595:
=={{header|Pike}}==
 
<syntaxhighlight lang="pike">
<lang Pike>
#!/usr/bin/env pike
Line 1,617:
return;
}
</syntaxhighlight>
</lang>
Output :
<pre>
Line 1,631:
=={{header|Prolog}}==
Works with SWI-Prolog.
<langsyntaxhighlight Prologlang="prolog">sleep_sort(L) :-
thread_pool_create(rosetta, 1024, []) ,
maplist(initsort, L, LID),
Line 1,640:
thread_create_in_pool(rosetta, (sleep(V), writeln(V)), Id, []).
 
</syntaxhighlight>
</lang>
Output :
<pre> sleep_sort([5, 1, 3, 2, 11, 6, 3, 4]).
Line 1,656:
 
=={{header|PureBasic}}==
<langsyntaxhighlight PureBasiclang="purebasic">NewMap threads()
 
Procedure Foo(n)
Line 1,672:
Next
Print("Press ENTER to exit"): Input()
EndIf</langsyntaxhighlight>
<pre>Sleep_sort.exe 3 1 4 1 5 9
1
Line 1,685:
===Python: Using threading.Timer===
 
<langsyntaxhighlight lang="python">from time import sleep
from threading import Timer
 
Line 1,704:
print('sleep sort worked for:',x)
else:
print('sleep sort FAILED for:',x)</langsyntaxhighlight>
 
;Sample output:
Line 1,714:
could be a sole translation from the original version in Bash:
{{Works with|Python 3.5+}}
<langsyntaxhighlight lang="python">#!/usr/bin/env python3
from asyncio import run, sleep, wait
from sys import argv
Line 1,723:
 
if __name__ == '__main__':
run(wait(map(f, map(int, argv[1:]))))</langsyntaxhighlight>
Example usage:
<pre>
Line 1,740:
=={{header|Racket}}==
 
<langsyntaxhighlight lang="racket">
#lang racket
 
Line 1,756:
;; outputs '(2 5 5 7 8 9 10)
(sleep-sort '(5 8 2 7 9 10 5))
</syntaxhighlight>
</lang>
 
=={{header|Raku}}==
(formerly Perl 6)
 
<syntaxhighlight lang="raku" perl6line>await map -> $delay { start { sleep $delay ; say $delay } },
<6 8 1 12 2 14 5 2 1 0>;</langsyntaxhighlight>
 
{{out}}
Line 1,778:
This can also be written using reactive programming:
 
<syntaxhighlight lang="raku" perl6line>#!/usr/bin/env raku
use v6;
react whenever Supply.from-list(@*ARGS).start({ .&sleep // +$_ }).flat { .say }</langsyntaxhighlight>
 
{{out}}
Line 1,794:
This sort will accept any manner of numbers, &nbsp; or for that matter, &nbsp; any character string as well.
<br>REXX isn't particular what is being sorted.
<langsyntaxhighlight lang="rexx">/*REXX program implements a sleep sort (with numbers entered from the command line (CL).*/
numeric digits 300 /*over the top, but what the hey! */
/* (above) ··· from vaudeville. */
Line 1,841:
do m=1 for howMany-1; next= m+1; if @.m>@.next then return 0 /*¬ in order*/
end /*m*/ /*keep looking for fountain of youth. */
return 1 /*yes, indicate with an indicator. */</langsyntaxhighlight>
Programming note: &nbsp; this REXX program makes use of &nbsp; '''DELAY''' &nbsp; BIF which delays (sleeps) for a specified amount of seconds.
<br>Some REXXes don't have a &nbsp; '''DELAY''' &nbsp; BIF, &nbsp; so one is included here &nbsp; ──► &nbsp; [[DELAY.REX]].
Line 1,873:
 
=={{header|Ruby}}==
<langsyntaxhighlight lang="ruby">require 'thread'
 
nums = ARGV.collect(&:to_i)
Line 1,887:
threads.each {|t| t.join}
 
p sorted</langsyntaxhighlight>
 
Example
Line 1,894:
 
=={{header|Rust}}==
<langsyntaxhighlight lang="rust">use std::thread;
 
fn sleepsort<I: Iterator<Item=u32>>(nums: I) {
Line 1,906:
fn main() {
sleepsort(std::env::args().skip(1).map(|s| s.parse().unwrap()));
}</langsyntaxhighlight>
Output:
<pre>$ ./sleepsort 50 34 43 3 2
Line 1,917:
 
=={{header|Scala}}==
<langsyntaxhighlight lang="scala">object SleepSort {
 
def main(args: Array[String]): Unit = {
Line 1,933:
}.start())
 
}</langsyntaxhighlight>
{{out}}
<langsyntaxhighlight lang="bash">$ scala SleepSort 1 3 6 0 9 7 4 2 5 8
0 1 2 3 4 5 5 6 7 8 9 </langsyntaxhighlight>
 
=={{header|Sidef}}==
<langsyntaxhighlight lang="ruby">ARGV.map{.to_i}.map{ |i|
{Sys.sleep(i); say i}.fork;
}.each{.wait};</langsyntaxhighlight>
{{out}}
<pre>% sidef test.sf 5 1 3 2 11 6 4
Line 1,953:
 
=={{header|Simula}}==
<langsyntaxhighlight lang="simula">SIMULATION
BEGIN
 
Line 1,972:
OUTIMAGE;
 
END;</langsyntaxhighlight>
{{out}}
<pre> 1 2 3 3 4 6 7 9
Line 1,979:
=={{header|SNUSP}}==
Bloated SNUSP is ideally suited to this task, since this the variant adds multithreading and an additional dimension of data space. Sleep time is simulated by the loop delay required to copy each cell value, thereby ensuring that smaller values are printed earlier than larger values. This program requires a Bloated SNUSP interpreter which returns zero on input end-of-file.
<syntaxhighlight lang="snusp">
<lang SNUSP>
/$>\ input until eof
#/?<\?,/ foreach: fork
Line 1,985:
/:\?-; delay /
\.# print and exit thread
</syntaxhighlight>
</lang>
 
Legend:
Line 1,993:
 
=={{header|Swift}}==
<langsyntaxhighlight Swiftlang="swift">import Foundation
 
for i in [5, 2, 4, 6, 1, 7, 20, 14] {
Line 2,004:
}
 
CFRunLoopRun()</langsyntaxhighlight>
{{out}}
<pre>
Line 2,019:
=={{header|Tcl}}==
===Tcl 8.5===
<langsyntaxhighlight lang="tcl">#!/bin/env tclsh
set count 0
proc process val {
Line 2,032:
while {$count < $argc} {
vwait count
}</langsyntaxhighlight>
'''Demo:'''
<pre>
Line 2,052:
</pre>
===Tcl 8.6===
<langsyntaxhighlight lang="tcl">set sorted {}
lmap x $argv {after $x [list lappend sorted $x]}
while {[llength $sorted] != $argc} {
vwait sorted
}
puts $sorted</langsyntaxhighlight>
{{out}}
<pre>$ echo 'puts [lmap _ [lrepeat 30 {}] {expr {int(rand() * 100)}}]' | tclsh | tee /dev/tty | xargs tclsh sleepsort.tcl
Line 2,064:
 
===Tcl 8.6: coroutine===
<langsyntaxhighlight lang="tcl">#! /usr/bin/env tclsh
package require Tcl 8.6
Line 2,100:
coroutine c sleep-sort [validate $argv] ::sorted
vwait sorted</langsyntaxhighlight>
'''Demo:'''
<pre>
Line 2,118:
=={{header|UNIX Shell}}==
{{works with|Bourne Shell}}
<langsyntaxhighlight lang="bash">f() {
sleep "$1"
echo "$1"
Line 2,127:
shift
done
wait</langsyntaxhighlight>
Usage and output:
<pre>
Line 2,140:
 
=={{header|Visual Basic .NET}}==
<langsyntaxhighlight lang="vbnet">Imports System.Threading
 
Module Module1
Line 2,158:
End Sub
 
End Module</langsyntaxhighlight>
 
{{out}}
Line 2,173:
=={{header|Wren}}==
More of a simulation than a 'true' sleep sort.
<langsyntaxhighlight lang="ecmascript">import "timer" for Timer
import "io" for Stdout
import "os" for Process
Line 2,202:
}
}
System.print()</langsyntaxhighlight>
 
{{out}}
Line 2,213:
 
=={{header|zkl}}==
<langsyntaxhighlight lang="zkl">vm.arglist.apply(fcn(n){ Atomic.sleep(n); print(n) }.launch);
Atomic.waitFor(fcn{ vm.numThreads == 1 }); Atomic.sleep(2); println();</langsyntaxhighlight>
{{out}}
<pre>
10,327

edits