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 program uses a strand sort to sort a random list of words | nums.*/
<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 /*no size? Then use the default.*/
if size=='' then size=20 /*no size? Then use the default.*/
if minv=='' then minv=0 /*no minV? " " " " */
if minv=='' then minv=0 /*no minV? " " " " */
if maxv=='' then maxv=size /*no maxV? " " " " */
if maxv=='' then maxv=size /*no maxV? " " " " */
do i=1 for size /*generate random # list*/
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 old; say
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 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?*/
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
if word(x,j)>word(x,j+1) then do; w=j; leave; end
end /*j*/
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 /*while words(x)\==0*/
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 to 2; w.i=words(a.i); end /*find number of entries in lists*/
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 any list is empty, then stop*/
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: &nbsp; <tt> 25 -9 30 20.5117 1e7 </tt>:
'''output''' when using the input of: &nbsp; <tt> 25 -9 30 1000 2000 3000 </tt>
<pre>
<pre>
────────────────────────────────unsorted list────────────────────────────────
================================unsorted list==================================
20.5117 1e7 12 -6 13 26 30 27 -5 9 -8 14 9 21 3 13 17 6 23 22 14 3 9 1 30 -4 28
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 -6 -5 -4 1 3 3 6 9 9 9 12 13 13 14 14 17 20.5117 21 22 23 26 27 28 30 30 1e7
-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: &nbsp; <tt> 24 -9 100 66 66 8.8 carp Carp </tt>
<br>'''output''' when using the input of: &nbsp; <tt> 24 -9 100 66 66 8.8 carp Carp </tt>
<pre>
<pre>
──────────────────────────────────────unsorted list───────────────────────────────────────
====================================unsorted array====================================
66 66 8.8 carp Carp 49 50 8 82 59 65 -7 30 18 25 79 6 18 35 58 51 90 79 2 3 -5 72 29 5
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=====================================
-7 -5 2 3 5 6 8 8.8 18 18 25 29 30 35 49 50 51 58 59 65 66 66 72 79 79 82 90 Carp carp
-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 &nbsp; ASCII &nbsp; computer will sort words differently than an &nbsp; EBCDIC &nbsp; machine. <br>
Note that an &nbsp; ASCII &nbsp; computer will sort words differently than an &nbsp; EBCDIC &nbsp; machine. <br>