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 programpgm uses a strand sort to sortsorts a random list of words |using the strand sort numsalgorithm.*/
parse arg size minv maxv,old /*get options from command line. */
if size=='' then size=20 /*no size? Then use the default.*/
if minv=='' then minv=0 /*no minV? " " " " */
if maxv=='' then maxv=size /*no maxV? " " " " */
do i=1 for size /*generate random # list*/
old=old random(0,maxv-minv)+minv
end /*i*/
old=space(old) /*remove any extraneous blanks. */
say center('unsorted list',length(old),"="); say old; say
new=strand_sort(old) /*sort the list of random numbers*/
say center('sorted list' ,length(new),"="); say 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)
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)
end /*while words(x)\==0*/
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 tofor 2; w.i=words(a.i); end /*find number of entries in lists*/
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.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: &nbsp; <tt> 25 -9 30 20.51171000 1e72000 3000 </tt>:
<pre>
────────────────────────────────unsorted list────────────────────────────────
================================unsorted list==================================
20.51171000 1e72000 123000 -69 130 26 30 273 -58 917 -8 14-2 94 210 -3 1319 17-1 63 231 228 27 14 320 92 -6 23 1 30-8 -4 284
 
─────────────────────────────────sorted list─────────────────────────────────
=================================sorted list===================================
-8 -68 -56 -4 -3 -2 -1 30 30 61 91 92 93 123 134 134 148 8 9 14 17 19 20.5117 21 22 23 26 27 28 301000 302000 1e73000
</pre>
The REXX program can also sort words as well as numbers. <br>
<br>'''output''' when using the input of: &nbsp; <tt> 24 -9 100 66 66 8.8 carp Carp </tt>
<pre>
──────────────────────────────────────unsorted list───────────────────────────────────────
====================================unsorted array====================================
66 66 8.8 carp Carp 4920 5077 888 829 59 6539 -75 3010 1812 2580 7987 626 1861 3587 5894 5173 9027 7949 235 395 -581 7276 2940 513 72
 
───────────────────────────────────────sorted list────────────────────────────────────────
=====================================sorted array=====================================
-7 -5 2 3 5 6 8 8.8 189 1810 2512 2913 3020 3526 4927 5035 5139 5840 5949 6561 66 66 72 7973 76 77 80 81 87 87 7988 8294 9095 Carp carp
</pre>
Note that an &nbsp; ASCII &nbsp; computer will sort words differently than an &nbsp; EBCDIC &nbsp; machine. <br>