Sorting algorithms/Cocktail sort: Difference between revisions
m
→{{header|Wren}}: Changed to Wren S/H
(→version handles blanks: De-obfuscate Rexx version) |
m (→{{header|Wren}}: Changed to Wren S/H) |
||
(3 intermediate revisions by 2 users not shown) | |||
Line 1,742:
=={{header|Elena}}==
ELENA
<syntaxhighlight lang="elena">import extensions;
import system'math;
Line 1,758:
swapped := false;
for(int i := 0
{
if (list[i]>list[i+1])
Line 1,772:
swapped := false;
for(int i := list.Length - 2
{
if (list[i]>list[i+1])
Line 3,787:
===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.
The REXX ''PARSE'' instruction separates an input into parts and assigns them to
variables, in a single operation. Thus
cocktailSort2: procedure expose @.; parse arg N; nn=n-1 /*N: the number of items in @.*/▼
<syntaxhighlight lang="rexx">PARSE VALUE 0 items.j items.jp WITH done items.jp items.j</syntaxhighlight>
sets ''done'' to 0, ''items.jp'' to ''items.j'', and ''items.j'' to ''items.jp'', as long as none of the input
variables contain any blanks.
<syntaxhighlight lang="rexx">cocktailSort2: procedure expose items.
end /*j*/▼
▲
if done then leave /*Did swaps? Then we're done.*/▼
do until done
do k=nn for nn by -1; kp= k+1▼
done = 1
do j = 1 for
jp = j + 1
if items.j
parse value 0 items.j items.jp with done items.jp items.j /* swap items.j and items.jp, and set done to 0 */
kp = k + 1 /* Rexx doesn't allow "items.(k+1)", so use this instead. */
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}}==
Line 4,441 ⟶ 4,452:
=={{header|Wren}}==
{{trans|Go}}
<syntaxhighlight lang="
var last = a.count - 1
while (true) {
|