Jump to content

Sorting algorithms/Cocktail sort: Difference between revisions

→‎version handles blanks: De-obfuscate Rexx version
(→‎version handles blanks: It is a Rexx idiom to use <stem>.0 to hold the number of items in numerically-suffixed stem arrays.)
(→‎version handles blanks: De-obfuscate Rexx version)
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 @.0 /*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
end /*j*/
@.0 =24 if done then leave /*No numberswaps ofdone? entries in the array. 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.
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'
@.0 =24 /* number of entries in the array. */
items.0 =24 /* number of entries in the array. */
 
return
/*──────────────────────────────────────────────────────────────────────────────────────*/
showitems: procedure expose items.
show@: w= length(@.0)
parse arg phase
do j=1 to @.0 /*@.0 is the number of items in @. */
width = length(items.0)
say 'element' right(j, w) arg(1)":" @.j
do end /*j*/=1 to items.0 /* ↑ /* items.0 is the number of items in items. */
say 'element' right(j, wwidth) phase || arg(1)":" @items.j
return /* └─────max width of any line.*/</syntaxhighlight>
end /*j*/ /* do j=1 for nn; jp= j+1 */
return /* └─────max width of any line number. */</syntaxhighlight>
return</syntaxhighlight>
{{out|output|text=&nbsp; when using the internal default inputs:}}
 
Cookies help us deliver our services. By using our services, you agree to our use of cookies.