Sorting algorithms/Cocktail sort: Difference between revisions
m
→{{header|Wren}}: Changed to Wren S/H
(→{{header|Vlang}}: Rename "Vlang" in "V (Vlang)") |
m (→{{header|Wren}}: Changed to Wren S/H) |
||
(6 intermediate revisions by 3 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,519:
if not swap:
break</syntaxhighlight>
=={{header|Quackery}}==
<syntaxhighlight lang="Quackery">
[ 2dup 1+ peek dip peek > ] is compare ( [ n --> b )
[ dup 1+ unrot
2dup peek
dip
[ 2dup 1+ peek
unrot poke
swap ]
unrot poke ] is exchange ( [ n --> [ )
[ [ 0 swap
dup size 1 - times
[ dup i^ compare if
[ i^ exchange
dip 1+ ] ]
over while
dup size 1 - times
[ dup i compare if
[ i exchange
dip 1+ ] ]
over while
nip again ]
nip ] is cocktail ( [ --> [ )
randomise
[] 20 times [ 89 random 10 + join ]
dup echo cr
cocktail echo</syntaxhighlight>
{{out}}
<pre>[ 46 42 73 92 95 19 27 52 33 12 60 70 34 45 93 15 64 41 12 55 ]
[ 12 12 15 19 27 33 34 41 42 45 46 52 55 60 64 70 73 92 93 95 ]</pre>
=={{header|R}}==
Line 3,621 ⟶ 3,658:
/* shuffle sort, shuttle sort, or */
/* a bubble sort variation. */
call
call
call cocktailSort
call
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
cocktailSort: procedure expose
nn = items.0 - 1
do until done
done = 1
do j = 1 for
jp = j + 1
if items.j > items.jp
temp =
items.j =
end
end /*j*/
if done then leave /*No swaps done? Finished*/
do k = nn for nn by -1
kp = k + 1 /* Rexx doesn't allow "items.(k+1)", so use this instead. */
if items.k > items.kp then do
done = 0
temp = items.k
items.k = items.kp
items.kp = temp
end
end /*k*/
end /*until*/
return
/*──────────────────────────────────────────────────────────────────────────────────────*/
genitems: procedure expose items.
items.= /*assign a default value for the stem. */
items.1 ='---the 22 card tarot deck (larger deck has 56 additional cards in 4 suits)---'
items.2 ='==========symbol====================pip======================================'
items.9
items.24='the fool [often unnumbered] ◄─── XXII'
items.0 =24 /* number of entries in the array. */
return
/*──────────────────────────────────────────────────────────────────────────────────────*/
showitems: procedure expose items.
parse arg phase
width = length(items.0)
do j=1 to
say 'element' right(j, width) phase || ":" items.j
end /*j*/ /* ↑ */
/* └─────max width of any line number. */
return</syntaxhighlight>
{{out|output|text= when using the internal default inputs:}}
Line 3,730 ⟶ 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
<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.
nn = items.0 - 1 /*N: the number of items in items.*/
do until done
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 */
end /*j*/
if done then leave /*Did swaps? Then we're done.*/
do k = nn for nn by -1
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,384 ⟶ 4,452:
=={{header|Wren}}==
{{trans|Go}}
<syntaxhighlight lang="
var last = a.count - 1
while (true) {
|