Sorting algorithms/Strand sort: Difference between revisions
→{{header|REXX}}: Refurbished
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
Walterpachl (talk | contribs) (→{{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
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)
Parse Arg size minv maxv old /* obtain optional arguments from CL*/
if size=='' | size=="," then size=20 /*Not specified? Then use the default.*/▼
if
if
do i=1 for size /*generate a list of random numbers. */▼
Do i=1 To size
End
old=space(old)
Say 'Unsorted list:'
new=strand_sort(old) /*sort the list of the random numbers. */▼
Say old
Say
Say 'Sorted list:'
Say new
Exit /* stick a fork in it,
/*--------------------------------------------------------------------*/
strand_sort: Procedure
Parse Arg source
sorted=''
Do While words(source)\==0
w=words(source)
/* Find first word in source that is smaller Than its predecessor */
Do j=1 To w-1
If word(source,j)>word(source,j+1) Then
Leave
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)
source=subword(source,j+1) /* the rest starts with a smaller */
return space(p a.1 a.2)</syntaxhighlight>▼
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
When word(a.1,w1)<=word(a.2,1) Then
Return space(p a.1 a.2)
Return space(p a.2 a.1)
Otherwise Do
/* 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''' when using the input of: <tt> 25 -9 30 1000 2000 3000 </tt>
<pre>
|