Sorting algorithms/Sleep sort: Difference between revisions

Include a PHP implementation
(Include a PHP implementation)
Line 1,675:
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<!--</syntaxhighlight>-->
 
=={{header|PHP}}==
A PHP implementation of sleep sort using process forking.
<syntaxhighlight lang="php">
<?php
 
$buffer = 1;
$pids = [];
 
for ($i = 1; $i < $argc; $i++) {
$pid = pcntl_fork();
if ($pid < 0) {
die("failed to start child process");
}
 
if ($pid === 0) {
sleep($argv[$i] + $buffer);
echo $argv[$i] . "\n";
exit();
}
$pids[] = $pid;
}
 
foreach ($pids as $pid) {
pcntl_waitpid($pid, $status);
}
</syntaxhighlight>
<pre>
php ./sleepsort.php 35 21 11 1 2 27 32 7 42 20 50 42 25 41 43 14 46 20 30 8
1
2
7
8
11
14
20
20
21
25
27
30
32
35
41
42
42
43
46
50
</pre>
 
 
=={{header|PicoLisp}}==
1

edit