Sorting algorithms/Sleep sort: Difference between revisions

Content added Content deleted
m (→‎{{header|REXX}}: removed STYLE from the PRE html tag.)
m (→‎{{header|REXX}}: added a section header comment (only works with ...), changed/add comments and indentations, added whitespace.)
Line 904: Line 904:
This sort will accept any manner of numbers, or for that matter, any character string as well.
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.
<br>REXX isn't particular what is being sorted.

<lang rexx>/*REXX program implements a sleep sort (with numbers entered from C.L.)*/
This REXX version '''only''' works with Regina REXX &nbsp; (as the program uses the &nbsp; '''fork''' &nbsp; func tion.
<br>REXX isn't particular what is being sorted.
<lang rexx>/*REXX program implements a sleep sort (with numbers entered from C.L.).*/
numeric digits 300 /*over the top, but what the hey!*/
numeric digits 300 /*over the top, but what the hey!*/
/* (above) ... from vaudeville.*/
/* (above) ··· from vaudeville.*/
#.= /*placeholder for the array of #s*/
#.= /*placeholder for the array of #s*/
stuff='1e9 50 5 40 4 1 100 30 3 12 2 8 9 7 6 6 10 20 0' /*alphabetically*/
stuff= 1e9 50 5 40 4 1 100 30 3 12 2 8 9 7 6 6 10 20 0 /*alphabetically*/
parse arg numbers /*let the user specify on the CL.*/
parse arg numbers /*let the user specify on the CL.*/
if numbers='' then numbers=stuff /*Not specified? Then use default*/
if numbers='' then numbers=stuff /*Not specified? Then use default*/
N=words(numbers) /*N is the number of numbers. */
N=words(numbers) /*N is the number of numbers. */
w=length(N) /*width of N (for nice output).*/
w=length(N) /*width of N (for nice output).*/
say N 'numbers to be sorted:' numbers /*informative informational info.*/
say N 'numbers to be sorted:' numbers /*informative informational info.*/


do j=1 for N /*let's start to boogie-woogie. */
do j=1 for N /*let's start to boogie-woogie. */
#.j=word(numbers,j) /*plug in one number at a time. */
#.j=word(numbers,j) /*plug in one number at a time. */
if datatype(#.j,'Numeric') then #.j = #.j/1 /*normalize it if num.*/
if datatype(#.j,'N') then #.j=#.j/1 /*normalize it if a number.*/
call fork /*REGINA REXX supports FORK func.*/
call fork /*only REGINA REXX supports FORK.*/
call sortItem j /*start a sort for array number. */
call sortItem j /*start a sort for array number. */
end /*j*/
end /*j*/


do forever while \inOrder(N) /*wait for the sorts to complete.*/
do forever while \inOrder(N) /*wait for the sorts to complete.*/
call sleep 1 /*1 sec is the min effective time*/
call sleep 1 /*1 sec is minimum effective time*/
end /*forever while*/ /*well, other than zero seconds. */
end /*forever while*/ /*well, other than zero seconds. */


m=max(length(#.1),length(#.N)) /*width of smallest | largest num*/
m=max(length(#.1),length(#.N)) /*width of smallest | largest num*/
say; say 'after sort:'; indent=left('',20) /*same as: COPIES(' ',20) */
say; say 'after sort:' /*display blank line and a title.*/

/*∙∙∙but LEFT pads [much faster]*/
do k=1 for N /*list (sorted) array's elements.*/
do k=1 for N /*list (sorted) array's elements.*/
say indent 'array element' right(k,w) '───>' right(#.k,m)
say left('',20) 'array element' right(k,w) '───►' right(#.k,m)
end /*k*/
end /*k*/
exit /*stick a fork in it, we're done.*/
exit /*stick a fork in it, we're done.*/
/*───────────────────────────────────SortItem subroutine────────────────*/
/*───────────────────────────────────SortItem subroutine────────────────*/
sortItem: procedure expose #.; parse arg ? /*sorts single item.*/
sortItem: procedure expose #.; parse arg ? /*sorts single item.*/
do Asort=1 until \switched /*cook until cooked.*/
do Asort=1 until \switched /*cook until cooked.*/
switched=0 /*honky-dorey so far*/
switched=0 /*hunky-dorey so far*/
do i=1 while #.i\=='' & \switched
do i=1 while #.i\=='' & \switched
if #.? >= #.i then iterate /*this one ok*/
if #.? >= #.i then iterate /*this one ok*/
parse value #.? #.i with #.i #.?
parse value #.? #.i with #.i #.?
switched=1 /*yup, we done switched one.*/
switched=1 /* [↑] swapped one.*/
end /*i*/
end /*i*/
if Asort//?==0 then call sleep switched /*sleep if last*/
if Asort//?==0 then call sleep switched /*sleep if last*/
end /*Asort*/
end /*Asort*/
return /*Sleeping Beauty awakes. Not to worry: (c) = circa 1697.*/
return /*Sleeping Beauty awakes. Not to worry: (c) = circa 1697.*/
/*───────────────────────────────────InOrder subroutine─────────────────*/
/*───────────────────────────────────InOrder subroutine─────────────────*/
inOrder: procedure expose #.; parse arg howMany /*is array in order? */
inOrder: procedure expose #.; parse arg howMany /*is array in order? */
do m=1 for howMany-1; next=m+1; if #.m>#.next then return 0
do m=1 for howMany-1; next=m+1; if #.m>#.next then return 0
end /*m*/ /*keep looking for fountain of yut*/
end /*m*/ /*keep looking for fountain of yut*/
return 1 /*yes, indicate with an indicator.*/</lang>
return 1 /*yes, indicate with an indicator.*/
</lang>
'''output''' when using the default input
'''output''' when using the default input
<pre>
<pre>