Sorting algorithms/Cocktail sort: Difference between revisions

Content added Content deleted
(→‎version handles blanks: De-obfuscate Rexx version)
(→‎version handles non-blanks: De-obfuscate Rexx version)
Line 3,787: Line 3,787:
===version handles non-blanks===
===version handles non-blanks===
This faster REXX version can handle array elements that don't contain blanks or spaces by using a simpler ''swap'' mechanism.
This faster REXX version can handle array elements that don't contain blanks or spaces by using a simpler ''swap'' mechanism.
<syntaxhighlight lang="rexx">cocktailSort2: procedure expose items.
<syntaxhighlight lang="rexx">/*──────────────────────────────────────────────────────────────────────────────────────*/
cocktailSort2: procedure expose @.; parse arg N; nn=n-1 /*N: the number of items in @.*/
nn = items.0 - 1 /*N: the number of items in items.*/
do until done
do until done; done= 1 /*array items can't have blanks*/
do j=1 for nn; jp= j+1
done = 1
do j = 1 for nn
if @.j>@.jp then parse value 0 @.j @.jp with done @.jp @.j
end /*j*/
jp = j + 1 /* Rexx doesn't allow "items.(j+1)", so use this instead. */
if done then leave /*Did swaps? Then we're done.*/
if items.j > items.jp then ,
parse value 0 items.j items.jp with done items.jp items.j /* swap items.j and items.jp, and set done to 0 */
do k=nn for nn by -1; kp= k+1
end /*j*/
if @.k>@.kp then parse value 0 @.k @.kp with done @.kp @.k
end /*k*/
if done then leave /*Did swaps? Then we're done.*/
end /*until*/
do k = nn for nn by -1
kp = k + 1 /* Rexx doesn't allow "items.(k+1)", so use this instead. */
return</syntaxhighlight> <br><br>
if items.k > items.kp then ,
parse value 0 items.k items.kp with done items.kp items.k /* swap items.k and items.kp, and set done to 0 */
end /*k*/
end /*until*/

return</syntaxhighlight> <br><br>


=={{header|Ring}}==
=={{header|Ring}}==