Jump to content

Sorting algorithms/Cocktail sort: Difference between revisions

m
→‎{{header|REXX}}: optimized the (two) procedures.
(Rename Perl 6 -> Raku, alphabetize, minor clean-up)
m (→‎{{header|REXX}}: optimized the (two) procedures.)
Line 2,742:
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
cocktailSort: procedure expose @.; parse arg N; nn= N-1 /*N: is number of items. */
do until done; done= 1
do j=1 for N-1nn; jp= j+1
if @.j>@.jp then do; done=0; _=@.j; @.j=@.jp; @.jp=_; end
end /*j*/
if done then leave /*No swaps done? Finished*/
do k=N-1nn for N-1nn by -1; kp= k+1
if @.k>@.kp then do; done=0; _=@.k; @.k=@.kp; @.kp=_; end
end /*k*/
Line 2,834:
This faster REXX version can handle an array that doesn't contain blanks or spaces by using a simpler ''swap'' mechanism.
<lang rexx>/*──────────────────────────────────────────────────────────────────────────────────────*/
cocktailSort2: procedure expose @.; parse arg N; nn=n-1 /*N: is the number of items in @. */
do until done; done= 1 /*array items may notcan't containhave blanks*/
do j=1 for N-1nn; jp= j+1
if @.j>@.jp then parse value 0 @.j @.jp with done @.jp @.j
end /*j*/
if done then leave /*NoDid swaps done? Then we're done. */
do k=N-1nn for N-1nn by -1; kp= k+1
if @.k>@.kp then parse value 0 @.k @.kp with done @.kp @.k
end /*k*/
Cookies help us deliver our services. By using our services, you agree to our use of cookies.