Anonymous user
Sorting algorithms/Cycle sort: Difference between revisions
m
→version 2: added/changed comments and whitespace, use a subroutine for common code, used a template for the output.
(Added Wren) |
m (→version 2: added/changed comments and whitespace, use a subroutine for common code, used a template for the output.) |
||
Line 1,570:
===version 2===
This REXX version demonstrates the use of negative numbers and
As a default, the program uses (for the input list) some digits of pi, which for practical purposes, appear random.
<lang rexx>/*REXX program
parse arg z /*obtain
if z='' then z= -3.14 3 1 4 1 5 9 2 6 5 3 5 8 9 7 9 3 2 3 8 4 6 2 6 4 3 3 8 3 2 7 9 5 0 2 8 8 4
say 'unsorted list: ' z /*show the original unsorted numbers. */
w= sortCycle(z)
say '
say 'and required ' w " writes." /*show number of writes done in sort. */
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
sortCycle: procedure expose
do i=1 for #; @.i= word(y,i)
end
do c=1 for #; x=@.c; p=
if p==c then iterate
call .putX
end /*while p\==c*/
▲ end /*while p\==c*/
end /*c*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
▲_=@.1; do j=2 to #; _=_ @.j; end; say ' sorted list: ' _
.putX: mv= mv+1; do p=p while x==@.p; end; parse value @.p x with x @.p; return</lang>
<pre>
unsorted list: -3.14 3 1 4 1 5 9 2 6 5 3 5 8 9 7 9 3 2 3 8 4 6 2 6 4 3 3 8 3 2 7 9 5 0 2 8 8 4
sorted list: -3.14 0 1 1 2 2 2 2 2 3 3 3 3 3 3 3 4 4 4 4 5 5 5 5 6 6 6 7 7 8 8 8 8 8 9 9 9 9
and
</pre>
<pre>
unsorted list: FM Stereo has been around since 1961.
sorted list: 1961. FM Stereo around been has since
and
</pre>
Note (for the above output). This REXX program was executed on an '''ASCII''' machine.
<br>On an '''ASCII''' machine, the order of sorting is numbers, uppercase letters, lowercase letters.<br>
On an '''EBCDIC''' machine, the order of sorting is lowercase letters, uppercase letters, numbers.<br>
Other (special) characters may also be in a different order. <br><br>
===version 3===
|