Sorting algorithms/Cocktail sort: Difference between revisions

Content added Content deleted
(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 /*N: is number of items. */
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 for N-1; jp= 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=N-1 for N-1 by -1; kp= k+1
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 @.; parse arg N /*N: is the number of items in @. */
cocktailSort2: procedure expose @.; parse arg N; nn=n-1 /*N: the number of items in @.*/
do until done; done= 1 /*array items may not contain blanks*/
do until done; done= 1 /*array items can't have blanks*/
do j=1 for N-1; jp= j+1
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 /*No swaps done? Then we're done. */
if done then leave /*Did swaps? Then we're done.*/
do k=N-1 for N-1 by -1; kp= k+1
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*/