Anonymous user
Sorting algorithms/Strand sort: Difference between revisions
m
→{{header|REXX}}: changed indentations and whitespace, used faster construnct in a DO loop.
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:
well as allowing a pre-pended list of numbers).
<br>It can handle integers, floating point numbers, exponentated numbers, and character strings.
<lang rexx>/*REXX
parse arg size minv maxv,old /*get options from command line. */
if size=='' then size=20
if minv=='' then minv=0
if maxv=='' then maxv=size
do i=1 for size
old=old random(0,maxv-minv)+minv
end /*i*/
old=space(old) /*remove any extraneous blanks. */
say center('unsorted list',length(old),"
new=strand_sort(old) /*sort the list of random numbers*/
say center('sorted list' ,length(new),"
exit /*stick a fork in it, we're done.*/
/*──────────────────────────────────STRAND_SORT subroutine──────────────*/
strand_sort: procedure; parse arg x; y=
do while words(x)\==0; w=words(x)
y=merge(y,subword(x,1,w)); x=subword(x,w+1)
end
return y
/*──────────────────────────────────MERGE subroutine────────────────────*/
merge: procedure; parse arg a.1,a.2; p=
do forever /*keep at it while 2 lists exist.*/
do i=1
if w.1*w.2==0 then leave
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)
Line 1,401:
end /*forever*/
return space(p a.1 a.2)</lang>
'''output''' when using the input of: <tt> 25 -9 30
<pre>
────────────────────────────────unsorted list────────────────────────────────
─────────────────────────────────sorted list─────────────────────────────────
-8 -
</pre>
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>
<pre>
──────────────────────────────────────unsorted list───────────────────────────────────────
66 66 8.8 carp Carp
───────────────────────────────────────sorted list────────────────────────────────────────
</pre>
Note that an ASCII computer will sort words differently than an EBCDIC machine. <br>
|