Sorting algorithms/Sleep sort: Difference between revisions

→‎{{header|REXX}}: changed comments and whitespace, updated the output to reflect changed glyphs.
(Added Wren)
(→‎{{header|REXX}}: changed comments and whitespace, updated the output to reflect changed glyphs.)
Line 1,549:
 
=={{header|REXX}}==
This sort will accept any manner of numbers,   or for that matter,   any character string as well.
<br>REXX isn't particular what is being sorted.
<lang rexx>/*REXX program implements a sleep sort (with numbers entered from C.L.the command line (CL).*/
 
numeric digits 300 /*over the top, but what the hey! */
This REXX version '''only''' works with Regina REXX &nbsp; (as the program uses the &nbsp; '''fork''' &nbsp;
/* (above) ··· from vaudeville. */
function.
@.= /*placeholder for the array of numbers.*/
<lang rexx>/*REXX program implements a sleep sort (with numbers entered from C.L.).*/
numericstuff= digits1e9 30050 5 40 4 1 100 30 3 12 2 8 9 7 6 6 10 20 0 /*over the top, butalphabetically what··· theso hey!far.*/
parse arg numbers /*obtain optional (above)arguments from ··· fromthe vaudeville.CL*/
#.if numbers='' then numbers= stuff /*Not specified? Then /*placeholder foruse the array of #sdefault.*/
stuffN= 1e9words(numbers) 50 5 40 4 1 100 30 3 12 2 8 9 7 6 6 10 20 0 /*alphabeticallyN is the number of numbers in list. */
parsew= length(N) arg numbers /*letwidth theof user specifyN on the(used CLfor nice output). */
parse upper version !ver . /*obtain which REXX we're running under*/
if numbers='' then numbers=stuff /*Not specified? Then use default*/
N!regina=words (numbers'REXX-REGINA'==left(!ver, 11) ) /*Nindicate (or isnot) theif this number ofis numbersRegina. */
w=length(say N) 'numbers to be sorted:' numbers /*width of N (forinformative niceinformational output).information*/
/*from department of redundancy depart.*/
say N 'numbers to be sorted:' numbers /*informative informational info.*/
do j=1 for N /*let's start to boogie─woogie da sort.*/
 
do @.j=1 word(numbers, j) for N /*let'splug startin toa boogie-woogiesingle number at a time. */
#if datatype(@.j=word(numbers,j 'N') then @.j= @.j / 1 /*plugnormalize init oneif number atit's a time. numeric number*/
if datatype(#.j,'N')!regina then #.j=#.j/1call fork /*normalizeonly REGINA REXX supports it ifFORK a numberBIF.*/
call forksortItem j /*onlystart REGINAa REXXsort supportsfor FORKan array number. */
call sortItem j /*start a sort for array number. */
end /*j*/
 
do forever while \inOrder(N) /*wait for the sorts to complete. */
call sleepdelay 1 /*1one secsecond is minimum effective time.*/
end /*forever while*/ /*well heck, other than zero seconds. */
 
m= max(length(#@.1), length(#@.N) ) /*width of smallest |or largest numnumber. */
say; say 'after sort:' say 'after sort:' /*display a blank line and a title. */
 
do k=1 for N /*list the (sorted) array's elements.*/
say left('', 20) 'array element' right(k, w) '───►' right(#@.k, m)
end /*k*/
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
/*───────────────────────────────────SortItem subroutine────────────────*/
sortItem: procedure expose #@.; parse arg ? /*sorts a single (numeric) item. */
do Asort=1 until \switched /*cooksort unsorted array until cooked.it's sorted*/
switched= 0 /*it's all hunky─dorey, happy─dappy /*hunky-dorey so far···*/
do i=1 while #@.i\=='' & \switched
if #@.? >= #@.i then iterate /*thisitem oneis in order. ok*/
parse value #@.? #@.i with #@.i #@.?
switched= 1 /* [↑] swapped one.*/
end /*i*/
if Asort//?==0 then call sleepdelay switched /*sleep if last item*/
end /*Asort*/
return /*Sleeping Beauty awakes. Not to worry: (c) = circa 1697.*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
/*───────────────────────────────────InOrder subroutine─────────────────*/
inOrder: procedure expose #@.; parse arg howMany /*is the array in numerical order? */
do m=1 for howMany-1; next= m+1; if #@.m>#@.next then return 0 /*¬ in order*/
end /*m*/ /*keep looking for fountain of yutyouth. */
return 1 /*yes, indicate with an indicator. */</lang>
'''{{out|output'''|text=&nbsp; when using the internal default input:}}
</lang>
'''output''' when using the default input
<pre>
19 numbers to be sorted: 1e91E9 50 5 40 4 1 100 30 3 12 2 8 9 7 6 6 10 20 0
 
after sort:
array element 1 ───>───► 0
array element 2 ───>───► 1
array element 3 ───>───► 2
array element 4 ───>───► 3
array element 5 ───>───► 4
array element 6 ───>───► 5
array element 7 ───>───► 6
array element 8 ───>───► 6
array element 9 ───>───► 7
array element 10 ───>───► 8
array element 11 ───>───► 9
array element 12 ───>───► 10
array element 13 ───>───► 12
array element 14 ───>───► 20
array element 15 ───>───► 30
array element 16 ───>───► 40
array element 17 ───>───► 50
array element 18 ───>───► 100
array element 19 ───>───► 1000000000
</pre>