Sorting algorithms/Strand sort: Difference between revisions

m (syntax highlighting fixup automation)
(→‎{{header|REXX}}: Refurbished)
Line 1,558:
=={{header|REXX}}==
This REXX program was written to generate a specified amount of random numbers as
well as allowing a pre-pended list of numbersitems).
 
It can handle integers, floating point numbers, exponentiated numbers, and/or character strings.
<syntaxhighlight lang="rexx">/*REXX program sorts a random list of words (or numbers) using the strand sort algorithm */
parse/* using the strand sort algorithm arg size minv maxv old /*obtain optional arguments from the CL*/
Parse Arg size minv maxv old /* obtain optional arguments from CL*/
if size=='' | size=="," then size=20 /*Not specified? Then use the default.*/
if minvsize=='' | minvsize=="," then minvsize= 0 20 /*Not specified? Then use the default.*/
if maxvminv=='' | maxvminv=="," then maxvminv=size 0 /*Not specified? Then use the default.*/
if sizemaxv=='' | sizemaxv=="," then maxv=size=20 /*Not specified? Then use the default.*/
do i=1 for size /*generate a list of random numbers. */
Do i=1 To size
old=old random(0,maxv-minv)+minv /* append a random numbernumbers to athe list. */
end /*i*/
End
old=space(old) /*elide extraneous blanks from the list*/
old=space(old)
say center('unsorted list', length(old), "─"); say old
Say 'Unsorted list:'
new=strand_sort(old) /*sort the list of the random numbers. */
Say old
say; say center('sorted list' , length(new), "─"); say new
new=strand_sort(old) /* sort thegiven list of(extended theby random numbers.) */
exit /*stick a fork in it, we're all done. */
Say
/*──────────────────────────────────────────────────────────────────────────────────────*/
Say 'Sorted list:'
strand_sort: procedure; parse arg x; y=
Say new
do while words(x)\==0; w=words(x)
Exit /* stick a fork in it, do j=1 for w-1 /*anythingwe're outall ofdone order?*/
/*--------------------------------------------------------------------*/
if word(x,j)>word(x,j+1) then do; w=j; leave; end
strand_sort: Procedure
end /*j*/
Parse Arg source
y=merge(y,subword(x,1,w)); x=subword(x,w+1)
sorted=''
end /*while*/
Do While words(source)\==0
return y
w=words(source)
/*──────────────────────────────────────────────────────────────────────────────────────*/
/* Find first word in source that is smaller Than its predecessor */
merge: procedure; parse arg a.1,a.2; p=
Do j=1 To w-1
do forever; w1=words(a.1); w2=words(a.2) /*do while 2 lists exist*/
If word(source,j)>word(source,j+1) Then
if w1==0 | if w2==0 then leave /*Any list empty? Stop.*/
Leave
if word(a.1,w1) <= word(a.2,1) then leave /*lists are now sorted? */
End
if word(a.2,w2) <= word(a.1,1) then return space(p a.2 a.1)
/* Elements source.1 trough source.j are in ascending order */
#=1+(word(a.1,1) >= word(a.2,1)); p=p word(a.#,1); a.#=subword(a.#,2)
head=subword(source,1,j)
end /*forever*/
source=subword(source,j+1) /* the rest starts with a smaller */
return space(p a.1 a.2)</syntaxhighlight>
do i=1 for size /*generate avalue or is empty (j=w!) list of random numbers. */
sorted=merge(sorted,head)
End
Return sorted
/*--------------------------------------------------------------------*/
merge: Procedure
Parse Arg a.1,a.2
p=''
Do Forever
w1=words(a.1)
w2=words(a.2)
Select
When w1==0 | w2==0 Then
returnReturn space(p a.1 a.2)</syntaxhighlight>
When word(a.1,w1)<=word(a.2,1) Then
Return space(p a.1 a.2)
ifWhen word(a.2,w2) <= word(a.1,1) then return space(p a.2 a.1)Then
Return space(p a.2 a.1)
Otherwise Do
#nn=1+(word(a.1,1) >= word(a.2,1)); p=p word(a.#,1); a.#=subword(a.#,2)
/* move the smaller first word of a.1 or a.2 to p */
p=p word(a.nn,1)
a.nn=subword(a.nn,2)
End
End
End</syntaxhighlight>
'''output''' &nbsp; when using the input of: &nbsp; <tt> 25 -9 30 1000 2000 3000 </tt>
<pre>
2,294

edits