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).
 
<br>It can handle integers, floating point numbers, exponentatedexponentiated numbers, and/or character strings.
<lang rexx>/*REXX pgmprogram sorts a random list of words (or numbers) using the strand sort algorithm.*/
parse arg size minv maxv,old /*get options from command line. */
ifparse arg size=='' minv maxv old then size=20 /*no size?obtain optional Thenarguments usefrom the default.CL*/
if minvsize=='' | then minvsize=0="," then size=20 /*noNot minVspecified? Then use " " " " the default.*/
if maxvminv=='' | minv=="," then maxvminv=size 0 /*noNot maxVspecified? Then use " " " " the default.*/
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 /*generate a list of random(0,maxv-minv)+minv numbers. */
old=old random(0,maxv-minv)+minv /*append a random number to a list. end /*i*/
old=space(old) end /*remove any extraneous blanks. i*/
exitold=space(old) /*stickelide aextraneous forkblanks infrom it,the we're done.list*/
say center('unsorted list',length(old),"─"); say old; say
new=strand_sort(old) say center('unsorted list', length(old), "─"); /*sort the list of randomsay numbers*/old
new=strand_sort(old) /*sort the list of the random numbers. */
say center('sorted list' ,length(new),"─"); say new
say; say center('unsortedsorted list' , length(oldnew), "─"); say old; say new
exit /*stick a fork in it, we're done.*/
exit do forever /*keepstick ata fork in it, while 2we're listsall existdone. */
/*──────────────────────────────────STRAND_SORT subroutine──────────────*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
strand_sort: procedure; parse arg x; y=
strand_sort: procedure; parse arg x; do while words(x)\==0; w y=words(x)
do j=1 while forwords(x)\==0; w-1 /*any number | word out of order?*/w=words(x)
if word(x,j)>word(x,j+1) then do; j=1 for w=j;-1 /*anything leave;out of endorder?*/
end /* if word(x,j*/)>word(x,j+1) then do; w=j; leave; end
y=merge(y,subword(x,1,w)); x=subword(x,w+1) end /*j*/
end /*while*/ y=merge(y,subword(x,1,w)); x=subword(x,w+1)
end do i=1 for size /*generate random # listwhile*/
return y
return y
/*──────────────────────────────────MERGE subroutine────────────────────*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
merge: procedure; procedure; parse arg a.1,a.2; p=
do forever /*keep at it while 2 lists exist.*/
do i=1 for 2 do forever; w.i w1=words(a.i1); end /*findw2=words(a.2) number of entries/*do inwhile 2 lists exist*/
if w.1*w.2w1==0 | thenif leavew2==0 then leave /*if anyAny list is empty,? then stopStop.*/
if word(a.1,w.1w1) <= word(a.2,1) then leave /*lists are now sorted? */
if word(a.2,w.2w2) <= word(a.1,1) then return space(p a.2 a.1)
#=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''' &nbsp; when using the input of: &nbsp; <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''' &nbsp; when using the input of: &nbsp; <tt> 24 -9 100 66 66 8.8 carp Carp </tt>
<pre>
──────────────────────────────────────unsorted list───────────────────────────────────────