Sorting algorithms/Cocktail sort: Difference between revisions

m
→‎{{header|Wren}}: Changed to Wren S/H
(Added Quackery.)
m (→‎{{header|Wren}}: Changed to Wren S/H)
(5 intermediate revisions by 2 users not shown)
Line 1,742:
 
=={{header|Elena}}==
ELENA 56.0x :
<syntaxhighlight lang="elena">import extensions;
import system'math;
Line 1,758:
swapped := false;
for(int i := 0,; i <= list.Length - 2,; i += 1)
{
if (list[i]>list[i+1])
Line 1,772:
swapped := false;
for(int i := list.Length - 2,; i >= 0,; i -= 1)
{
if (list[i]>list[i+1])
Line 3,658:
/* shuffle sort, shuttle sort, or */
/* a bubble sort variation. */
call gen@ genItems /*generate some array elements. */
call show@showItems 'before sort' /*show unsorted array elements. */
say copies('█', 101) /*show a separator line (a fence). */
call cocktailSort # /*invoke the cocktail sort subroutine. */
call show@showItems ' after sort' /*show sorted array elements. */
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
cocktailSort: procedure expose @.; parse arg N; nn= N-1 /*N: is number of items. */
nn = items.0 - 1 do until done; done= 1 /*items.0: is number of items. */
do until done
do j=1 for nn; jp= j+1
done = 1
if @.j>@.jp then do; done=0; _=@.j; @.j=@.jp; @.jp=_; end
do j = 1 for end /*j*/nn
jp = j + 1 if done then/* leaveRexx doesn't allow "items.(j+1)", so use this instead. /*No swaps done? Finished*/
if items.j > items.jp then do k=nn for nn by -1; kp= k+1
if @.k>@.kp then do; done=0; _=@.k; @.k=@.kp; @.kp=_; end0
temp = end /*k*/items.j
items.j = end /*until*/items.jp
return items.jp = temp
end /*j*/
end /*j*/
if done then leave /*No swaps done? Finished*/
do k = nn for nn by -1; kp= k+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.
gen@: @.= 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======================================'
@.3 ='the juggler ◄─── I'
@items.43 ='the highjuggler priestess [Popess] ◄─── II I'
@items.54 ='the empresshigh priestess [Popess] ◄─── ◄─── IIIII'
@items.65 ='the emperorempress ◄─── IVIII'
@items.76 ='the hierophantemperor [Pope] ◄─── V ◄─── IV'
@items.87 ='the lovershierophant [Pope] ◄─── ◄─── VIV'
@items.98 ='the chariotlovers ◄─── VII VI'
items.9 @.10='justicethe chariot ◄─── ◄─── VIIIVII'
@items.1110='thejustice hermit ◄─── ◄─── IXVIII'
@items.1211='fortunethe hermit [the wheel of] ◄─── X ◄─── IX'
@items.1312='strengthfortune [the wheel of] ◄─── ◄─── XIX'
@items.1413='thestrength hanging man ◄─── XII XI'
@items.1514='deaththe hanging man [often unlabeled] ◄─── XIII XII'
@items.1615='temperance death [often unlabeled] ◄─── XIVXIII'
@items.1716='the devil temperance ◄─── XVXIV'
@items.1817='lightningthe devil [the tower] ◄─── XVI XV'
@items.1918='thelightning stars [the tower] ◄─── ◄─── XVIIXVI'
@items.2019='the moon stars ◄─── XVIII XVII'
@items.2120='the sun moon ◄─── XIXXVIII'
@items.2221='judgmentthe sun ◄─── ◄─── XXXIX'
@items.2322='thejudgment world ◄─── XXI XX'
@items.2423='the foolworld [often unnumbered] ◄─── XXII XXI'
items.24='the fool [often unnumbered] ◄─── XXII'
items.0 =24 return /* [↑]number of adjustentries forin DOthe loop advancementarray. */
 
return
do #=1 until @.#==''; end; #= #-1 /*find how many entries in the array. */
return /* [↑] adjust for DO loop advancement.*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
showitems: procedure expose items.
show@: w= length(#); do j=1 for # /*#: is the number of items in @. */
parse arg phase
say 'element' right(j, w) arg(1)":" @.j
width = length(items.0)
end /*j*/ /* ↑ */
do j=1 to return items.0 /* items.0 is the number └─────maxof widthitems ofin anyitems. line.*/</syntaxhighlight>
say 'element' right(j, wwidth) phase || arg(1)":" @items.j
end /*j*/ /* do j=1 for nn; jp= j+1 */
/* └─────max width of any line number. */
return</syntaxhighlight>
{{out|output|text=&nbsp; when using the internal default inputs:}}
 
Line 3,767 ⟶ 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
<syntaxhighlight lang="rexx">/*──────────────────────────────────────────────────────────────────────────────────────*/
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>
do until done; done= 1 /*array items can't have blanks*/
sets ''done'' to 0, ''items.jp'' to ''items.j'', and ''items.j'' to ''items.jp'', as long as none of the input
do j=1 for nn; jp= j+1
variables contain any blanks.
if @.j>@.jp then parse value 0 @.j @.jp with done @.jp @.j
<syntaxhighlight lang="rexx">cocktailSort2: procedure expose items.
end /*j*/
cocktailSort2: procedure expose @.; parsenn arg= N;items.0 nn=n- 1 /*N: the number of items in @items.*/
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 if @.k>@.kp then parse value 0 @.k @.kp with done @.kp @.k
do j = 1 for end /*k*/nn
jp = j + 1 end /* Rexx doesn't allow "items.(j+1)", so use this instead. /*until*/
if items.j return</syntaxhighlight> <br><br>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 */
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,421 ⟶ 4,452:
=={{header|Wren}}==
{{trans|Go}}
<syntaxhighlight lang="ecmascriptwren">var cocktailSort = Fn.new { |a|
var last = a.count - 1
while (true) {
9,476

edits