Sorting algorithms/Strand sort: Difference between revisions
Content added Content deleted
m (→{{header|REXX}}: removed STYLE from the PRE html tag.) |
m (→{{header|REXX}}: changed indentations and whitespace, used faster construnct in a DO loop.) |
||
Line 1,369: | Line 1,369: | ||
well as allowing a pre-pended list of numbers). |
well as allowing a pre-pended list of numbers). |
||
<br>It can handle integers, floating point numbers, exponentated numbers, and character strings. |
<br>It can handle integers, floating point numbers, exponentated numbers, and character strings. |
||
<lang rexx>/*REXX |
<lang rexx>/*REXX pgm sorts a random list of words using the strand sort algorithm.*/ |
||
parse arg size minv maxv,old /*get options from command line. */ |
parse arg size minv maxv,old /*get options from command line. */ |
||
if size=='' then size=20 |
if size=='' then size=20 /*no size? Then use the default.*/ |
||
if minv=='' then minv=0 |
if minv=='' then minv=0 /*no minV? " " " " */ |
||
if maxv=='' then maxv=size |
if maxv=='' then maxv=size /*no maxV? " " " " */ |
||
do i=1 for size |
do i=1 for size /*generate random # list*/ |
||
old=old random(0,maxv-minv)+minv |
old=old random(0,maxv-minv)+minv |
||
end /*i*/ |
end /*i*/ |
||
old=space(old) /*remove any extraneous blanks. */ |
old=space(old) /*remove any extraneous blanks. */ |
||
say center('unsorted list',length(old)," |
say center('unsorted list',length(old),"─"); say old; say |
||
new=strand_sort(old) /*sort the list of random numbers*/ |
new=strand_sort(old) /*sort the list of random numbers*/ |
||
say center('sorted list' ,length(new)," |
say center('sorted list' ,length(new),"─"); say new |
||
exit /*stick a fork in it, we're done.*/ |
exit /*stick a fork in it, we're done.*/ |
||
/*──────────────────────────────────STRAND_SORT subroutine──────────────*/ |
/*──────────────────────────────────STRAND_SORT subroutine──────────────*/ |
||
strand_sort: procedure; parse arg x; y= |
strand_sort: procedure; parse arg x; y= |
||
do while words(x)\==0; w=words(x) |
do while words(x)\==0; w=words(x) |
||
do j=1 for w-1 /*any number | word out of order?*/ |
|||
if word(x,j)>word(x,j+1) then do; w=j; leave; end |
|||
end /*j*/ |
|||
y=merge(y,subword(x,1,w)); x=subword(x,w+1) |
y=merge(y,subword(x,1,w)); x=subword(x,w+1) |
||
end |
end /*while*/ |
||
return y |
return y |
||
/*──────────────────────────────────MERGE subroutine────────────────────*/ |
/*──────────────────────────────────MERGE subroutine────────────────────*/ |
||
merge: procedure; parse arg a.1,a.2; p= |
merge: procedure; parse arg a.1,a.2; p= |
||
do forever /*keep at it while 2 lists exist.*/ |
do forever /*keep at it while 2 lists exist.*/ |
||
do i=1 |
do i=1 for 2; w.i=words(a.i); end /*find number of entries in lists*/ |
||
if w.1*w.2==0 then leave |
if w.1*w.2==0 then leave /*if any list is empty, then stop*/ |
||
if word(a.1,w.1) <= word(a.2,1) then leave /*lists are now sorted?*/ |
if word(a.1,w.1) <= word(a.2,1) then leave /*lists are now sorted?*/ |
||
if word(a.2,w.2) <= word(a.1,1) then return space(p a.2 a.1) |
if word(a.2,w.2) <= word(a.1,1) then return space(p a.2 a.1) |
||
Line 1,401: | Line 1,401: | ||
end /*forever*/ |
end /*forever*/ |
||
return space(p a.1 a.2)</lang> |
return space(p a.1 a.2)</lang> |
||
'''output''' when using the input of: <tt> 25 -9 30 |
'''output''' when using the input of: <tt> 25 -9 30 1000 2000 3000 </tt> |
||
<pre> |
<pre> |
||
────────────────────────────────unsorted list──────────────────────────────── |
|||
================================unsorted list================================== |
|||
1000 2000 3000 9 0 3 -8 17 8 -2 4 0 -3 19 -1 3 1 8 27 14 20 2 -6 23 1 -8 -4 4 |
|||
─────────────────────────────────sorted list───────────────────────────────── |
|||
=================================sorted list=================================== |
|||
-8 - |
-8 -8 -6 -4 -3 -2 -1 0 0 1 1 2 3 3 4 4 8 8 9 14 17 19 20 23 27 1000 2000 3000 |
||
</pre> |
</pre> |
||
The REXX program can also sort words as well as numbers. <br> |
The REXX program can also sort words as well as numbers. <br> |
||
<br>'''output''' when using the input of: <tt> 24 -9 100 66 66 8.8 carp Carp </tt> |
<br>'''output''' when using the input of: <tt> 24 -9 100 66 66 8.8 carp Carp </tt> |
||
<pre> |
<pre> |
||
──────────────────────────────────────unsorted list─────────────────────────────────────── |
|||
====================================unsorted array==================================== |
|||
66 66 8.8 carp Carp |
66 66 8.8 carp Carp 20 77 88 9 39 -5 10 12 80 87 26 61 87 94 73 27 49 35 95 81 76 40 13 72 |
||
───────────────────────────────────────sorted list──────────────────────────────────────── |
|||
=====================================sorted array===================================== |
|||
-5 8.8 9 10 12 13 20 26 27 35 39 40 49 61 66 66 72 73 76 77 80 81 87 87 88 94 95 Carp carp |
|||
</pre> |
</pre> |
||
Note that an ASCII computer will sort words differently than an EBCDIC machine. <br> |
Note that an ASCII computer will sort words differently than an EBCDIC machine. <br> |