Jump to content

Sorting algorithms/Strand sort: Difference between revisions

m
→‎{{header|REXX}}: added DO-END labels, remove superflous blanks. -- ~~~~
m (→‎{{header|Perl 6}}: minor simplfications)
m (→‎{{header|REXX}}: added DO-END labels, remove superflous blanks. -- ~~~~)
Line 978:
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, and exponentated numbers, and character strings.
<lang rexx>/*REXX program uses a strand sort to sort a random list of words | nums.*/
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 ji=1 for size /*generate random # list*/
old=old random(0,maxv-minv)+minv
end /*j*/
old=space(old) /*remove any extraneous blanks. */
say center('unsorted list',length(old),"="); say old; say
new=strand_sort(old) /*sort it.*/
say center('sorted list' ,length(new),"="); say new
exit
/*─────────────────────────────────────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 to 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)
#=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>
</lang>
'''output''' when using the input of <tt> 25 -9 30 20.5117 1e7 </tt>:
<pre style="height:20ex;overflow:scroll">
================================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
Line 1,021 ⟶ 1,022:
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 style="height:20ex;overflow:scroll">
====================================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
Cookies help us deliver our services. By using our services, you agree to our use of cookies.