Sorting algorithms/Cocktail sort: Difference between revisions
Content added Content deleted
Thundergnat (talk | contribs) (Rename Perl 6 -> Raku, alphabetize, minor clean-up) |
m (→{{header|REXX}}: optimized the (two) procedures.) |
||
Line 2,742: | Line 2,742: | ||
exit /*stick a fork in it, we're all done. */ |
exit /*stick a fork in it, we're all done. */ |
||
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
/*──────────────────────────────────────────────────────────────────────────────────────*/ |
||
cocktailSort: procedure expose @.; parse arg N |
cocktailSort: procedure expose @.; parse arg N; nn= N-1 /*N: is number of items. */ |
||
do until done; done= 1 |
do until done; done= 1 |
||
do j=1 |
do j=1 for nn; jp= j+1 |
||
if @.j>@.jp then do; done=0; _=@.j; @.j=@.jp; @.jp=_; end |
if @.j>@.jp then do; done=0; _=@.j; @.j=@.jp; @.jp=_; end |
||
end /*j*/ |
end /*j*/ |
||
if done then leave /*No swaps done? Finished*/ |
if done then leave /*No swaps done? Finished*/ |
||
do k= |
do k=nn for nn by -1; kp= k+1 |
||
if @.k>@.kp then do; done=0; _=@.k; @.k=@.kp; @.kp=_; end |
if @.k>@.kp then do; done=0; _=@.k; @.k=@.kp; @.kp=_; end |
||
end /*k*/ |
end /*k*/ |
||
Line 2,834: | Line 2,834: | ||
This faster REXX version can handle an array that doesn't contain blanks or spaces by using a simpler ''swap'' mechanism. |
This faster REXX version can handle an array that doesn't contain blanks or spaces by using a simpler ''swap'' mechanism. |
||
<lang rexx>/*──────────────────────────────────────────────────────────────────────────────────────*/ |
<lang rexx>/*──────────────────────────────────────────────────────────────────────────────────────*/ |
||
cocktailSort2: procedure expose @.; |
cocktailSort2: procedure expose @.; parse arg N; nn=n-1 /*N: the number of items in @.*/ |
||
do until done; done= 1 /*array items |
do until done; done= 1 /*array items can't have blanks*/ |
||
do j=1 for |
do j=1 for nn; jp= j+1 |
||
if @.j>@.jp then parse value 0 @.j @.jp with done @.jp @.j |
if @.j>@.jp then parse value 0 @.j @.jp with done @.jp @.j |
||
end /*j*/ |
end /*j*/ |
||
if done then leave /* |
if done then leave /*Did swaps? Then we're done.*/ |
||
do k= |
do k=nn for nn by -1; kp= k+1 |
||
if @.k>@.kp then parse value 0 @.k @.kp with done @.kp @.k |
if @.k>@.kp then parse value 0 @.k @.kp with done @.kp @.k |
||
end /*k*/ |
end /*k*/ |