Anonymous user
Sorting algorithms/Strand sort: Difference between revisions
m
→{{header|REXX}}: added/changed comments and whitespace, changed indentations, simplified a subroutine.
m (added whitespace before the TOC, added other whitespace in the task's preamble.) |
m (→{{header|REXX}}: added/changed comments and whitespace, changed indentations, simplified a subroutine.) |
||
Line 1,434:
This REXX program was written to generate a specified amount of random numbers as
well as allowing a pre-pended list of numbers).
<lang rexx>/*REXX
if
if
if maxv=='' | maxv=="," then maxv=size /*Not specified? Then use the default.*/
do i=1 for size /*generate random # list*/▼
do i=1 for size
old=old random(0,maxv-minv)+minv /*append a random number to a list.
say center('unsorted list',length(old),"─"); say old; say▼
new=strand_sort(old) /*sort the list of the random numbers. */
▲exit /*stick a fork in it, we're done.*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
strand_sort: procedure; parse arg x;
do
return y
/*──────────────────────────────────────────────────────────────────────────────────────*/
merge:
▲ do forever /*keep at it while 2 lists exist.*/
if
if word(a.1,
if word(a.2,
#=1+(word(a.1,1) >= word(a.2,1)); p=p word(a.#,1); a.#=subword(a.#,2)
end /*forever*/
return space(p a.1 a.2)</lang>
'''output''' when using the input of: <tt> 25 -9 30 1000 2000 3000 </tt>
<pre>
────────────────────────────────unsorted list────────────────────────────────
Line 1,476:
</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───────────────────────────────────────
|