Sorting algorithms/Cocktail sort: Difference between revisions

added RPL
(Added an Algol W sample)
(added RPL)
(91 intermediate revisions by 43 users not shown)
Line 1:
{{task|Sorting Algorithms}}{{Sorting Algorithm}}{{wikipedia|Cocktail sort}}
{{Sorting Algorithm}}
[[Category:Sorting]]
{{Wikipedia|Cocktail sort}}
 
 
The cocktail shaker sort is an improvement on the [[Bubble Sort]].
 
The improvement is basically that values "bubble" both directions through the array, because on each iteration the cocktail shaker sort bubble sorts once forwards and once backwards. Pseudocode for the algorithm (from [[wp:Cocktail sort|wikipedia]]):
'''function''' ''cocktailSort''( A : list of sortable items )
Line 22 ⟶ 28:
'''while''' swapped; ''// if no elements have been swapped,''
''// then the list is sorted''
 
;Related task:
:*   [https://rosettacode.org/wiki/Sorting_algorithms/Cocktail_sort_with_shifting_bounds cocktail sort with shifting bounds]
<br><br>
 
=={{header|11l}}==
{{trans|Python}}
 
<syntaxhighlight lang="11l">F cocktailSort(&A)
L
L(indices) ((0 .< A.len-1).step(1), (A.len-2 .. 0).step(-1))
V swapped = 0B
L(i) indices
I A[i] > A[i + 1]
swap(&A[i], &A[i + 1])
swapped = 1B
I !swapped
R
 
V test1 = [7, 6, 5, 9, 8, 4, 3, 1, 2, 0]
cocktailSort(&test1)
print(test1)</syntaxhighlight>
 
{{out}}
<pre>
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
</pre>
 
=={{header|360 Assembly}}==
{{trans|PL/I}}
The program uses HLASM structured macros (DO,ENDDO,IF,ELSE,ENDIF) for readability and two ASSIST/360 macros (XDECO,XPRNT) to keep the code as short as possible.
<syntaxhighlight lang="360asm">* Cocktail sort 25/06/2016
COCKTSRT CSECT
USING COCKTSRT,R13 base register
B 72(R15) skip savearea
DC 17F'0' savearea
STM R14,R12,12(R13) prolog
ST R13,4(R15) "
ST R15,8(R13) "
LR R13,R15 "
L R2,N n
BCTR R2,0 n-1
ST R2,NM1 nm1=n-1
DO UNTIL=(CLI,STABLE,EQ,X'01') repeat
MVI STABLE,X'01' stable=true
LA RI,1 i=1
DO WHILE=(C,RI,LE,NM1) do i=1 to n-1
LR R1,RI i
SLA R1,2 .
LA R2,A-4(R1) @a(i)
LA R3,A(R1) @a(i+1)
L R4,0(R2) r4=a(i)
L R5,0(R3) r5=a(i+1)
IF CR,R4,GT,R5 THEN if a(i)>a(i+1) then
MVI STABLE,X'00' stable=false
ST R5,0(R2) a(i)=r5
ST R4,0(R3) a(i+1)=r4
ENDIF , end if
LA RI,1(RI) i=i+1
ENDDO , end do
L RI,NM1 i=n-1
DO WHILE=(C,RI,GE,=F'1') do i=n-1 to 1 by -1
LR R1,RI i
SLA R1,2 .
LA R2,A-4(R1) @a(i)
LA R3,A(R1) @a(i+1)
L R4,0(R2) r4=a(i)
L R5,0(R3) r5=a(i+1)
IF CR,R4,GT,R5 THEN if a(i)>a(i+1) then
MVI STABLE,X'00' stable=false
ST R5,0(R2) a(i)=r5
ST R4,0(R3) a(i+1)=r4
ENDIF , end if
BCTR RI,0 i=i-1
ENDDO , end do
ENDDO , until stable
LA R3,PG pgi=0
LA RI,1 i=1
DO WHILE=(C,RI,LE,N) do i=1 to n
LR R1,RI i
SLA R1,2 .
L R2,A-4(R1) a(i)
XDECO R2,XDEC edit a(i)
MVC 0(4,R3),XDEC+8 output a(i)
LA R3,4(R3) pgi=pgi+4
LA RI,1(RI) i=i+1
ENDDO , end do
XPRNT PG,L'PG print buffer
L R13,4(0,R13) epilog
LM R14,R12,12(R13) "
XR R15,R15 "
BR R14 exit
A DC F'4',F'65',F'2',F'-31',F'0',F'99',F'2',F'83',F'782',F'1'
DC F'45',F'82',F'69',F'82',F'104',F'58',F'88',F'112',F'89',F'74'
N DC A((N-A)/L'A) number of items of a
NM1 DS F n-1
PG DC CL80' ' buffer
XDEC DS CL12 temp for xdeco
STABLE DS X stable
YREGS
RI EQU 6 i
END COCKTSRT</syntaxhighlight>
{{out}}
<pre>
-31 0 1 2 2 4 45 58 65 69 74 82 82 83 88 89 99 104 112 782
</pre>
=={{header|6502 Assembly}}==
Implemented in Easy6502. Output is provided below but it's best to watch this in action. Just copy and paste the code in, hit Assemble then Run. Make sure you check the Monitor box and set the address to 1200 and the length to 100. This takes about half as long as the bubble sort.
<syntaxhighlight lang="6502asm">define z_HL $00
define z_L $00
define z_H $01
define temp $02
define temp2 $03
 
define yINC $04
define yDEC $05
set_table:
dex
txa
sta $1200,y
iny
bne set_table ;stores $ff at $1200, $fe at $1201, etc.
lda #$12
sta z_H
lda #$00
sta z_L ;get the base address of the data table
 
lda #0
tax
tay ;clear regs
sty yINC ;yINC = 0
dey ;LDY #255
sty yDEC ;yDEC = 255
iny ;LDY #0
 
JSR COCKTAILSORT
BRK
COCKTAILSORT:
;yINC starts at the beginning and goes forward, yDEC starts at the end and goes back.
LDY yINC
LDA (z_HL),y ;get item Y
STA temp
INY
LDA (z_HL),y ;get item Y+1
DEY
STA temp2
CMP temp
bcs doNothing_Up ;if Y<=Y+1, do nothing. Otherwise swap them.
 
;we had to re-arrange an item.
lda temp
iny
sta (z_HL),y ;store the higher value at base+y+1
inx ;sort count. If zero at the end, we're done.
dey
lda temp2
sta (z_HL),y ;store the lower value at base+y
 
doNothing_Up:
LDY yDEC
LDA (z_HL),y
STA temp
DEY
LDA (z_HL),y
INY
STA temp2
CMP temp
bcc doNothing_Down ;if Y<=Y+1, do nothing. Otherwise swap them.
 
;we had to re-arrange an item.
lda temp
dey
sta (z_HL),y ;store the higher value at base+y+1
inx ;sort count. If zero at the end, we're done.
iny
lda temp2
sta (z_HL),y ;store the lower value at base+y
 
doNothing_Down:
INC yINC
DEC yDEC
LDA yINC
BPL COCKTAILSORT
 
CPX #0
BEQ doneSorting
LDX #0 ;reset the counter
LDY #0
STY yINC
DEY ;LDY #$FF
STY yDEC
INY ;LDY #0
JMP COCKTAILSORT
doneSorting:
RTS</syntaxhighlight>
 
{{out}}
<pre>
1200: 00 01 02 03 04 05 06 07 08 09 0a 0b 0c 0d 0e 0f
1210: 10 11 12 13 14 15 16 17 18 19 1a 1b 1c 1d 1e 1f
1220: 20 21 22 23 24 25 26 27 28 29 2a 2b 2c 2d 2e 2f
1230: 30 31 32 33 34 35 36 37 38 39 3a 3b 3c 3d 3e 3f
1240: 40 41 42 43 44 45 46 47 48 49 4a 4b 4c 4d 4e 4f
1250: 50 51 52 53 54 55 56 57 58 59 5a 5b 5c 5d 5e 5f
1260: 60 61 62 63 64 65 66 67 68 69 6a 6b 6c 6d 6e 6f
1270: 70 71 72 73 74 75 76 77 78 79 7a 7b 7c 7d 7e 7f
1280: 80 81 82 83 84 85 86 87 88 89 8a 8b 8c 8d 8e 8f
1290: 90 91 92 93 94 95 96 97 98 99 9a 9b 9c 9d 9e 9f
12a0: a0 a1 a2 a3 a4 a5 a6 a7 a8 a9 aa ab ac ad ae af
12b0: b0 b1 b2 b3 b4 b5 b6 b7 b8 b9 ba bb bc bd be bf
12c0: c0 c1 c2 c3 c4 c5 c6 c7 c8 c9 ca cb cc cd ce cf
12d0: d0 d1 d2 d3 d4 d5 d6 d7 d8 d9 da db dc dd de df
12e0: e0 e1 e2 e3 e4 e5 e6 e7 e8 e9 ea eb ec ed ee ef
12f0: f0 f1 f2 f3 f4 f5 f6 f7 f8 f9 fa fb fc fd fe ff
</pre>
=={{header|AArch64 Assembly}}==
{{works with|as|Raspberry Pi 3B version Buster 64 bits}}
<syntaxhighlight lang="aarch64 assembly">
/* ARM assembly AARCH64 Raspberry PI 3B */
/* program cocktailSort64.s */
/*******************************************/
/* Constantes file */
/*******************************************/
/* for this file see task include a file in language AArch64 assembly */
.include "../includeConstantesARM64.inc"
 
/*********************************/
/* Initialized data */
/*********************************/
.data
szMessSortOk: .asciz "Table sorted.\n"
szMessSortNok: .asciz "Table not sorted !!!!!.\n"
sMessResult: .asciz "Value : @ \n"
szCarriageReturn: .asciz "\n"
.align 4
#TableNumber: .quad 1,3,6,2,5,9,10,8,4,7
TableNumber: .quad 10,9,8,7,6,-5,4,3,2,1
.equ NBELEMENTS, (. - TableNumber) / 8
/*********************************/
/* UnInitialized data */
/*********************************/
.bss
sZoneConv: .skip 24
/*********************************/
/* code section */
/*********************************/
.text
.global main
main: // entry of program
ldr x0,qAdrTableNumber // address number table
mov x1,0
mov x2,NBELEMENTS // number of élements
bl cocktailSort
ldr x0,qAdrTableNumber // address number table
bl displayTable
ldr x0,qAdrTableNumber // address number table
mov x1,NBELEMENTS // number of élements
bl isSorted // control sort
cmp x0,1 // sorted ?
beq 1f
ldr x0,qAdrszMessSortNok // no !! error sort
bl affichageMess
b 100f
1: // yes
ldr x0,qAdrszMessSortOk
bl affichageMess
100: // standard end of the program
mov x0,0 // return code
mov x8,EXIT // request to exit program
svc 0 // perform the system call
qAdrsZoneConv: .quad sZoneConv
qAdrszCarriageReturn: .quad szCarriageReturn
qAdrsMessResult: .quad sMessResult
qAdrTableNumber: .quad TableNumber
qAdrszMessSortOk: .quad szMessSortOk
qAdrszMessSortNok: .quad szMessSortNok
/******************************************************************/
/* control sorted table */
/******************************************************************/
/* x0 contains the address of table */
/* x1 contains the number of elements > 0 */
/* x0 return 0 if not sorted 1 if sorted */
isSorted:
stp x2,lr,[sp,-16]! // save registers
stp x3,x4,[sp,-16]! // save registers
mov x2,0
ldr x4,[x0,x2,lsl 3]
1:
add x2,x2,1
cmp x2,x1
bge 99f
ldr x3,[x0,x2, lsl 3]
cmp x3,x4
blt 98f
mov x4,x3
b 1b
98:
mov x0,0 // not sorted
b 100f
99:
mov x0,1 // sorted
100:
ldp x3,x4,[sp],16 // restaur 2 registers
ldp x2,lr,[sp],16 // restaur 2 registers
ret // return to address lr x30
/******************************************************************/
/* cocktail sort */
/******************************************************************/
/* x0 contains the address of table */
/* x1 contains the first element */
/* x2 contains the number of element */
cocktailSort:
stp x1,lr,[sp,-16]! // save registers
stp x2,x3,[sp,-16]! // save registers
stp x4,x5,[sp,-16]! // save registers
stp x6,x7,[sp,-16]! // save registers
stp x8,x9,[sp,-16]! // save registers
sub x2,x2,1 // compute i = n - 1
1: // start loop 1
mov x3,x1 // start index
mov x9,0
sub x7,x2,1
2: // start loop 2
add x4,x3,1
ldr x5,[x0,x3,lsl 3] // load value A[j]
ldr x6,[x0,x4,lsl 3] // load value A[j+1]
cmp x6,x5 // compare value
bge 3f
str x6,[x0,x3,lsl 3] // if smaller inversion
str x5,[x0,x4,lsl 3]
mov x9,1 // top table not sorted
3:
add x3,x3,1 // increment index j
cmp x3,x7 // end ?
ble 2b // no -> loop 2
cmp x9,0 // table sorted ?
beq 100f // yes -> end
mov x9,0
mov x3,x7
4:
add x4,x3,1
ldr x5,[x0,x3,lsl 3] // load value A[j]
ldr x6,[x0,x4,lsl 3] // load value A[j+1]
cmp x6,x5 // compare value
bge 5f
str x6,[x0,x3,lsl 3] // if smaller inversion
str x5,[x0,x4,lsl 3]
mov x9,1 // top table not sorted
5:
sub x3,x3,1 // decrement index j
cmp x3,x1 // end ?
bge 4b // no -> loop 2
 
cmp x9,0 // table sorted ?
bne 1b // no -> loop 1
100:
ldp x8,x9,[sp],16 // restaur 2 registers
ldp x6,x7,[sp],16 // restaur 2 registers
ldp x4,x5,[sp],16 // restaur 2 registers
ldp x2,x3,[sp],16 // restaur 2 registers
ldp x1,lr,[sp],16 // restaur 2 registers
ret // return to address lr x30
/******************************************************************/
/* Display table elements */
/******************************************************************/
/* x0 contains the address of table */
displayTable:
stp x1,lr,[sp,-16]! // save registers
stp x2,x3,[sp,-16]! // save registers
mov x2,x0 // table address
mov x3,0
1: // loop display table
ldr x0,[x2,x3,lsl 3]
ldr x1,qAdrsZoneConv
bl conversion10S // décimal conversion
ldr x0,qAdrsMessResult
ldr x1,qAdrsZoneConv
bl strInsertAtCharInc // insert result at @ character
bl affichageMess // display message
add x3,x3,1
cmp x3,NBELEMENTS - 1
ble 1b
ldr x0,qAdrszCarriageReturn
bl affichageMess
100:
ldp x2,x3,[sp],16 // restaur 2 registers
ldp x1,lr,[sp],16 // restaur 2 registers
ret // return to address lr x30
/********************************************************/
/* File Include fonctions */
/********************************************************/
/* for this file see task include a file in language AArch64 assembly */
.include "../includeARM64.inc"
 
</syntaxhighlight>
=={{header|Action!}}==
<syntaxhighlight lang="action!">PROC PrintArray(INT ARRAY a INT size)
INT i
 
Put('[)
FOR i=0 TO size-1
DO
IF i>0 THEN Put(' ) FI
PrintI(a(i))
OD
Put(']) PutE()
RETURN
 
PROC CoctailSort(INT ARRAY a INT size)
INT i,tmp
BYTE swapped
 
DO
swapped=0
i=0
WHILE i<size-1
DO
IF a(i)>a(i+1) THEN
tmp=a(i) a(i)=a(i+1) a(i+1)=tmp
swapped=1
FI
i==+1
OD
 
IF swapped=0 THEN EXIT FI
 
swapped=0
i=size-1
WHILE i>=0
DO
IF a(i)>a(i+1) THEN
tmp=a(i) a(i)=a(i+1) a(i+1)=tmp
swapped=1
FI
i==-1
OD
 
UNTIL swapped=0
OD
RETURN
 
PROC Test(INT ARRAY a INT size)
PrintE("Array before sort:")
PrintArray(a,size)
CoctailSort(a,size)
PrintE("Array after sort:")
PrintArray(a,size)
PutE()
RETURN
 
PROC Main()
INT ARRAY
a(10)=[1 4 65535 0 3 7 4 8 20 65530],
b(21)=[10 9 8 7 6 5 4 3 2 1 0
65535 65534 65533 65532 65531
65530 65529 65528 65527 65526],
c(8)=[101 102 103 104 105 106 107 108],
d(12)=[1 65535 1 65535 1 65535 1
65535 1 65535 1 65535]
Test(a,10)
Test(b,21)
Test(c,8)
Test(d,12)
RETURN</syntaxhighlight>
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Cocktail_Sort.png Screenshot from Atari 8-bit computer]
<pre>
Array before sort:
[1 4 -1 0 3 7 4 8 20 -6]
Array after sort:
[-6 -1 0 1 3 4 4 7 8 20]
 
Array before sort:
[10 9 8 7 6 5 4 3 2 1 0 -1 -2 -3 -4 -5 -6 -7 -8 -9 -10]
Array after sort:
[-10 -9 -8 -7 -6 -5 -4 -3 -2 -1 0 1 2 3 4 5 6 7 8 9 10]
 
Array before sort:
[101 102 103 104 105 106 107 108]
Array after sort:
[101 102 103 104 105 106 107 108]
 
Array before sort:
[1 -1 1 -1 1 -1 1 -1 1 -1 1 -1]
Array after sort:
[-1 -1 -1 -1 -1 -1 1 1 1 1 1 1]
</pre>
 
=={{header|ActionScript}}==
<langsyntaxhighlight ActionScriptlang="actionscript">function cocktailSort(input:Array):Array {
do {
var swapped:Boolean=false;
Line 48 ⟶ 552:
} while (swapped);
return input;
}</langsyntaxhighlight>
 
=={{header|Ada}}==
<langsyntaxhighlight Adalang="ada">with Ada.Text_Io; use Ada.Text_Io;
 
procedure Cocktail_Sort_Test is
Line 87 ⟶ 591:
Cocktail_Sort(Data);
Put_Line(Data);
end Cocktail_Sort_Test;</langsyntaxhighlight>
 
=={{header|ALGOL 60}}==
{{works with|A60}}
<syntaxhighlight lang="algol60">begin
comment Sorting algorithms/Cocktail sort - Algol 60;
integer nA;
nA:=20;
begin
integer array A[1:nA];
 
procedure cocktailsort(lb,ub);
value lb,ub; integer lb,ub;
begin
integer i,lbx,ubx;
boolean swapped;
lbx:=lb; ubx:=ub-1; swapped :=true;
for i:=1 while swapped do begin
procedure swap(i); value i; integer i;
begin
integer temp;
temp :=A[i];
A[i] :=A[i+1];
A[i+1]:=temp;
swapped:=true
end swap;
swapped:=false;
for i:=lbx step 1 until ubx do if A[i]>A[i+1] then swap(i);
if swapped
then begin
for i:=ubx step -1 until lbx do if A[i]>A[i+1] then swap(i);
ubx:=ubx-1; lbx:=lbx+1
end
end
end cocktailsort;
procedure inittable(lb,ub);
value lb,ub; integer lb,ub;
begin
integer i;
for i:=lb step 1 until ub do A[i]:=entier(rand*100)
end inittable;
procedure writetable(lb,ub);
value lb,ub; integer lb,ub;
begin
integer i;
for i:=lb step 1 until ub do outinteger(1,A[i]);
outstring(1,"\n")
end writetable;
nA:=20;
inittable(1,nA);
writetable(1,nA);
cocktailsort(1,nA);
writetable(1,nA)
end
end </syntaxhighlight>
{{out}}
<pre>
6 92 61 64 73 3 81 28 62 67 83 33 77 14 16 23 47 19 33 78
3 6 14 16 19 23 28 33 33 47 61 62 64 67 73 77 78 81 83 92
</pre>
 
=={{header|ALGOL 68}}==
<langsyntaxhighlight lang="algol68">MODE DATA = CHAR;
PROC swap = (REF DATA a,b)VOID:(
DATA tmp:=a; a:=b; b:=tmp
Line 122 ⟶ 691:
[32]CHAR data := "big fjords vex quick waltz nymph";
cocktail sort(data);
print(data)</langsyntaxhighlight>
{{out}}
<pre> abcdefghiijklmnopqrstuvwxyz</pre>
Line 128 ⟶ 697:
indirectly, thus removing the need to actually swap large chunks of memory
as only addresses are swapped.
<langsyntaxhighlight lang="algol68">MODE DATA = REF CHAR;
PROC swap = (REF DATA a,b)VOID:(
DATA tmp:=a; a:=b; b:=tmp
Line 139 ⟶ 708:
cocktail sort(ref data);
FOR i TO UPB ref data DO print(ref data[i]) OD; print(new line);
print((data))</langsyntaxhighlight>
{{out}}<pre> abcdefghiijklmnopqrstuvwxyz
big fjords vex quick waltz nymph</pre>
The above two routines both scan the entire width of the array, in both directions, even though the first and last elements of each sweep had already reached their final destination during the previous pass. The solution is to zig-zag, but have the sweeps shorten and converge on the centre element. This reduces the number of comparisons required by about 50%.
<langsyntaxhighlight lang="algol68">PROC odd even sort = (REF []DATA a)VOID: (
FOR offset FROM 0 DO
BOOL swapped := FALSE;
Line 165 ⟶ 734:
OD;
break do od loop: SKIP
);</langsyntaxhighlight>
 
=={{header|ALGOL W}}==
As noted in the ALGOL 68 sample above, the highest and lowest elements are sorted into their correct positions each time through the main loop.
This implementation optimises by reducing the number of elements to sort on each pass through the main loop.
<langsyntaxhighlight lang="algolw">begin
% As algol W does not allow overloading, we have to have type-specific %
% sorting procedures - this coctail sorts an integer array %
Line 234 ⟶ 803:
writeData;
end test ;
end.</langsyntaxhighlight>
{{out}}
<pre>
Line 240 ⟶ 809:
-6 0 2 8 9 9 14 16 23 90
</pre>
=={{header|ARM Assembly}}==
{{works with|as|Raspberry Pi}}
<syntaxhighlight lang="arm assembly">
/* ARM assembly Raspberry PI */
/* program cocktailSort.s */
/* REMARK 1 : this program use routines in a include file
see task Include a file language arm assembly
for the routine affichageMess conversion10
see at end of this program the instruction include */
/* for constantes see task include a file in arm assembly */
/************************************/
/* Constantes */
/************************************/
.include "../constantes.inc"
 
/*********************************/
/* Initialized data */
/*********************************/
.data
szMessSortOk: .asciz "Table sorted.\n"
szMessSortNok: .asciz "Table not sorted !!!!!.\n"
sMessResult: .asciz "Value : @ \n"
szCarriageReturn: .asciz "\n"
.align 4
#TableNumber: .int 1,3,6,2,5,9,10,8,4,7
TableNumber: .int 10,9,8,7,6,5,4,3,2,1
.equ NBELEMENTS, (. - TableNumber) / 4
/*********************************/
/* UnInitialized data */
/*********************************/
.bss
sZoneConv: .skip 24
/*********************************/
/* code section */
/*********************************/
.text
.global main
main: @ entry of program
1:
ldr r0,iAdrTableNumber @ address number table
mov r1,#0
mov r2,#NBELEMENTS @ number of élements
bl cocktailSort
ldr r0,iAdrTableNumber @ address number table
bl displayTable
ldr r0,iAdrTableNumber @ address number table
mov r1,#NBELEMENTS @ number of élements
bl isSorted @ control sort
cmp r0,#1 @ sorted ?
beq 2f
ldr r0,iAdrszMessSortNok @ no !! error sort
bl affichageMess
b 100f
2: @ yes
ldr r0,iAdrszMessSortOk
bl affichageMess
100: @ standard end of the program
mov r0, #0 @ return code
mov r7, #EXIT @ request to exit program
svc #0 @ perform the system call
iAdrszCarriageReturn: .int szCarriageReturn
iAdrsMessResult: .int sMessResult
iAdrTableNumber: .int TableNumber
iAdrszMessSortOk: .int szMessSortOk
iAdrszMessSortNok: .int szMessSortNok
/******************************************************************/
/* control sorted table */
/******************************************************************/
/* r0 contains the address of table */
/* r1 contains the number of elements > 0 */
/* r0 return 0 if not sorted 1 if sorted */
isSorted:
push {r2-r4,lr} @ save registers
mov r2,#0
ldr r4,[r0,r2,lsl #2]
1:
add r2,#1
cmp r2,r1
movge r0,#1
bge 100f
ldr r3,[r0,r2, lsl #2]
cmp r3,r4
movlt r0,#0
blt 100f
mov r4,r3
b 1b
100:
pop {r2-r4,lr}
bx lr @ return
/******************************************************************/
/* cocktail Sort */
/******************************************************************/
/* r0 contains the address of table */
/* r1 contains the first element */
/* r2 contains the number of element */
cocktailSort:
push {r1-r9,lr} @ save registers
sub r2,r2,#1 @ compute i = n - 1
add r8,r1,#1
1: @ start loop 1
mov r3,r1 @ start index
mov r9,#0
sub r7,r2,#1 @ max
2: @ start loop 2
add r4,r3,#1
ldr r5,[r0,r3,lsl #2] @ load value A[j]
ldr r6,[r0,r4,lsl #2] @ load value A[j+1]
cmp r6,r5 @ compare value
strlt r6,[r0,r3,lsl #2] @ if smaller inversion
strlt r5,[r0,r4,lsl #2]
movlt r9,#1 @ top table not sorted
add r3,#1 @ increment index j
cmp r3,r7 @ end ?
ble 2b @ no -> loop 2
cmp r9,#0 @ table sorted ?
beq 100f @ yes -> end
@ bl displayTable
mov r9,#0
mov r3,r7
3:
add r4,r3,#1
ldr r5,[r0,r3,lsl #2] @ load value A[j]
ldr r6,[r0,r4,lsl #2] @ load value A[j+1]
cmp r6,r5 @ compare value
strlt r6,[r0,r3,lsl #2] @ if smaller inversion
strlt r5,[r0,r4,lsl #2]
movlt r9,#1 @ top table not sorted
sub r3,#1 @ decrement index j
cmp r3,r1 @ end ?
bge 3b @ no -> loop 2
@ bl displayTable
cmp r9,#0 @ table sorted ?
bne 1b @ no -> loop 1
100:
pop {r1-r9,lr}
bx lr @ return
/******************************************************************/
/* Display table elements */
/******************************************************************/
/* r0 contains the address of table */
displayTable:
push {r0-r3,lr} @ save registers
mov r2,r0 @ table address
mov r3,#0
1: @ loop display table
ldr r0,[r2,r3,lsl #2]
ldr r1,iAdrsZoneConv @
bl conversion10S @ décimal conversion signed
ldr r0,iAdrsMessResult
ldr r1,iAdrsZoneConv @ insert conversion
bl strInsertAtCharInc
bl affichageMess @ display message
add r3,#1
cmp r3,#NBELEMENTS - 1
ble 1b
ldr r0,iAdrszCarriageReturn
bl affichageMess
100:
pop {r0-r3,lr}
bx lr
iAdrsZoneConv: .int sZoneConv
/***************************************************/
/* ROUTINES INCLUDE */
/***************************************************/
.include "../affichage.inc"
 
</syntaxhighlight>
=={{header|Arturo}}==
 
<syntaxhighlight lang="rebol">trySwap: function [arr,i][
if arr\[i] < arr\[i-1] [
tmp: arr\[i]
arr\[i]: arr\[i-1]
arr\[i-1]: tmp
return null
]
return true
]
cocktailSort: function [items][
t: false
l: size items
while [not? t][
t: true
loop 1..dec l 'i [
if null? trySwap items i ->
t: false
]
if t -> break
loop (l-1)..1 'i [
if null? trySwap items i ->
t: false
]
]
return items
]
 
print cocktailSort [3 1 2 8 5 7 9 4 6]</syntaxhighlight>
 
{{out}}
 
<pre>1 2 3 4 5 6 7 8 9</pre>
 
=={{header|AutoHotkey}}==
contributed by Laszlo on the ahk [http://www.autohotkey.com/forum/post-276379.html#276379 forum]
<langsyntaxhighlight AutoHotkeylang="autohotkey">MsgBox % CocktailSort("")
MsgBox % CocktailSort("xxx")
MsgBox % CocktailSort("3,2,1")
Line 274 ⟶ 1,052:
sorted .= "," . array%A_Index%
Return SubStr(sorted,2) ; drop leading comma
}</syntaxhighlight>
}</lang>
 
=={{header|AWK}}==
Sort the standard input and print it to standard output
<langsyntaxhighlight lang="awk">{
line[NR] = $0
}
Line 307 ⟶ 1,085:
print line[i]
}
}</langsyntaxhighlight>
 
=={{header|BBC BASIC}}==
Sorting an integer array. '%' indicates integer variable in BBC BASIC
<langsyntaxhighlight BBClang="bbc BASICbasic">DEF PROC_ShakerSort(Size%)
 
Start%=2
Line 329 ⟶ 1,107:
UNTIL ( ( End% * Direction% ) < ( Start% * Direction% ) )
 
ENDPROC</langsyntaxhighlight>
 
=={{header|C}}==
<langsyntaxhighlight lang="c">#include <stdio.h>
 
// can be any swap function. This swap is optimized for numbers.
#define try_swap { if (a[i] < a[i - 1])\
void swap(int *x, int *y) {
{ t = a[i]; a[i] = a[i - 1]; a[i - 1] = t; t = 0;} }
if(x == y)
 
return;
void cocktailsort(int *a, size_t len)
*x ^= *y;
{
size_t*y i^= *x;
int t*x ^= 0*y;
}
while (!t) {
void cocktailsort(int *a, size_t n) {
for (i = 1, t = 1; i < len; i++) try_swap;
while(1) {
if (t) break;
// packing two similar loops into one
for (i = len - 1, t = 1; i; i--) try_swap;
char flag;
size_t start[2] = {1, n - 1},
end[2] = {n, 0},
inc[2] = {1, -1};
for(int it = 0; it < 2; ++it) {
flag = 1;
for(int i = start[it]; i != end[it]; i += inc[it])
if(a[i - 1] > a[i]) {
swap(a + i - 1, a + i);
flag = 0;
}
if(flag)
return;
}
}
}
 
int main(void) {
int a[] = { 5, -1, 101, -4, 0, 1, 8, 6, 2, 3 };
{
size_t n = sizeof(a)/sizeof(a[0]);
int x[] = { 5, -1, 101, -4, 0, 1, 8, 6, 2, 3 };
size_t i, len = sizeof(x)/sizeof(x[0]);
 
cocktailsort(xa, lenn);
for (size_t i = 0; i < lenn; i++i)
printf("%d\n ", xa[i]);
return 0;
}</langsyntaxhighlight>
 
'''Output''':
 
<syntaxhighlight lang="text">-4 -1 0 1 2 3 5 6 8 101</syntaxhighlight>
 
===Generic version===
This version can be used with arrays of any type, like the standard library function qsort.
<syntaxhighlight lang="c">#include <stdbool.h>
#include <stdio.h>
#include <string.h>
 
void swap(char* p1, char* p2, size_t size) {
for (; size-- > 0; ++p1, ++p2) {
char tmp = *p1;
*p1 = *p2;
*p2 = tmp;
}
}
 
void cocktail_sort(void* base, size_t count, size_t size,
int (*cmp)(const void*, const void*)) {
char* begin = base;
char* end = base + size * count;
if (end == begin)
return;
bool swapped = true;
for (end -= size; swapped; ) {
swapped = false;
for (char* p = begin; p < end; p += size) {
char* q = p + size;
if (cmp(p, q) > 0) {
swap(p, q, size);
swapped = true;
}
}
if (!swapped)
break;
swapped = false;
for (char* p = end; p > begin; p -= size) {
char* q = p - size;
if (cmp(q, p) > 0) {
swap(p, q, size);
swapped = true;
}
}
}
}
 
int string_compare(const void* p1, const void* p2) {
const char* const* s1 = p1;
const char* const* s2 = p2;
return strcmp(*s1, *s2);
}
 
void print(const char** a, size_t len) {
for (size_t i = 0; i < len; ++i)
printf("%s ", a[i]);
printf("\n");
}
 
int main() {
const char* a[] = { "one", "two", "three", "four", "five",
"six", "seven", "eight", "nine", "ten" };
const size_t len = sizeof(a)/sizeof(a[0]);
printf("before: ");
print(a, len);
cocktail_sort(a, len, sizeof(char*), string_compare);
printf("after: ");
print(a, len);
return 0;
}</syntaxhighlight>
 
{{out}}
<pre>
before: one two three four five six seven eight nine ten
after: eight five four nine one seven six ten three two
</pre>
 
=={{header|C sharp|C#}}==
<syntaxhighlight lang="csharp">public static void cocktailSort(int[] A)
{
bool swapped;
do
{
swapped = false;
for (int i = 0; i <= A.Length - 2; i++)
{
if (A[i] > A[i + 1])
{
//test whether the two elements are in the wrong order
int temp = A[i];
A[i] = A[i + 1];
A[i + 1] = temp;
swapped = true;
}
}
if (!swapped)
{
//we can exit the outer loop here if no swaps occurred.
break;
}
swapped = false;
for (int i = A.Length - 2; i >= 0; i--)
{
if (A[i] > A[i + 1])
{
int temp = A[i];
A[i] = A[i + 1];
A[i + 1] = temp;
swapped = true;
}
}
//if no elements have been swapped, then the list is sorted
} while (swapped);
}</syntaxhighlight>
 
=={{header|C++}}==
<langsyntaxhighlight lang="cpp">
#include <iostream>
#include <windows.h>
Line 441 ⟶ 1,347:
return 0;
}
</syntaxhighlight>
</lang>
 
=== Alternate version ===
Uses C++11. Compile with
g++ -std=c++11 cocktail.cpp
<langsyntaxhighlight lang="cpp">#include <algorithm>
#include <iostream>
#include <iterator>
Line 480 ⟶ 1,386:
copy(std::begin(a), std::end(a), std::ostream_iterator<int>(std::cout, " "));
std::cout << "\n";
}</langsyntaxhighlight>
{{out}}
<pre>
-199 -52 2 3 33 56 99 100 177 200
</pre>
 
=={{header|C sharp|C#}}==
<lang csharp>public static void cocktailSort(int[] A)
{
bool swapped;
do
{
swapped = false;
for (int i = 0; i <= A.Length - 2; i++)
{
if (A[i] > A[i + 1])
{
//test whether the two elements are in the wrong order
int temp = A[i];
A[i] = A[i + 1];
A[i + 1] = temp;
swapped = true;
}
}
if (!swapped)
{
//we can exit the outer loop here if no swaps occurred.
break;
}
swapped = false;
for (int i = A.Length - 2; i >= 0; i--)
{
if (A[i] > A[i + 1])
{
int temp = A[i];
A[i] = A[i + 1];
A[i + 1] = temp;
swapped = true;
}
}
//if no elements have been swapped, then the list is sorted
} while (swapped);
}</lang>
 
=={{header|COBOL}}==
This is procedure division only.
<langsyntaxhighlight lang="cobol"> C-SORT SECTION.
C-000.
DISPLAY "SORT STARTING".
Line 563 ⟶ 1,431:
 
F-999.
EXIT.</langsyntaxhighlight>
 
=={{header|Common Lisp}}==
This version works on lists and vectors alike. The vector implementation is coded directly. The list version uses an intermediate vector.
<langsyntaxhighlight lang="lisp">(defun cocktail-sort-vector (vector predicate &aux (len (length vector)))
(labels ((scan (start step &aux swapped)
(loop for i = start then (+ i step) while (< 0 i len) do
Line 585 ⟶ 1,453:
(list (map-into sequence 'identity
(cocktail-sort-vector (coerce sequence 'vector)
predicate)))))</langsyntaxhighlight>
 
=={{header|D}}==
<langsyntaxhighlight lang="d">// Written in the D programming language.
module rosettaCode.sortingAlgorithms.cocktailSort;
 
Line 625 ⟶ 1,493:
["Alice", "Jane", "Joe", "John", "Kate", "Zerg"]);
}
</syntaxhighlight>
</lang>
 
{{out}}
<syntaxhighlight lang="d">
<lang d>
import rosettaCode.sortingAlgorithms.cocktailSort;
 
Line 635 ⟶ 1,503:
//generate 10 sorted random numbers in [0 .. 10)
rndGen.take(10).map!(a=>a%10).array.cocktailSort.writeln();
}</langsyntaxhighlight>
<pre>[2, 2, 3, 4, 5, 5, 7, 7, 7, 8]</pre>
 
Line 642 ⟶ 1,510:
 
Static array is an arbitrary-based array of fixed length
<langsyntaxhighlight Delphilang="delphi">program TestShakerSort;
 
{$APPTYPE CONSOLE}
Line 705 ⟶ 1,573:
Writeln;
Readln;
end.</langsyntaxhighlight>
{{out}}
<pre>
Line 713 ⟶ 1,581:
 
=={{header|E}}==
<langsyntaxhighlight lang="e">/** Cocktail sort (in-place) */
def cocktailSort(array) {
def swapIndexes := 0..(array.size() - 2)
Line 729 ⟶ 1,597:
}
}
}</langsyntaxhighlight>
Note that this solution contains no repeated code to handle the forward and reverse passes.
 
=={{header|Eiffel}}==
<syntaxhighlight lang="eiffel">
<lang Eiffel>
 
class
Line 825 ⟶ 1,694:
end
 
</syntaxhighlight>
</lang>
Test:
<syntaxhighlight lang="eiffel">
<lang Eiffel>
 
class
Line 863 ⟶ 1,732:
end
 
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 870 ⟶ 1,739:
sorted
1 2 3 5 99
</pre>
 
=={{header|Elena}}==
ELENA 6.x :
<syntaxhighlight lang="elena">import extensions;
import system'math;
import system'routines;
extension op
{
cocktailSort()
{
var list := self.clone();
bool swapped := true;
while(swapped)
{
swapped := false;
for(int i := 0; i <= list.Length - 2; i += 1)
{
if (list[i]>list[i+1])
{
list.exchange(i,i+1);
swapped := true
}
};
ifnot (swapped)
{
^ list
};
swapped := false;
for(int i := list.Length - 2; i >= 0; i -= 1)
{
if (list[i]>list[i+1])
{
list.exchange(i,i+1);
swapped := true
}
}
};
^ list
}
}
public program()
{
var list := new int[]{3, 5, 1, 9, 7, 6, 8, 2, 4 };
console.printLine("before:", list.asEnumerable());
console.printLine("after :", list.cocktailSort().asEnumerable())
}</syntaxhighlight>
{{out}}
<pre>
before:3,5,1,9,7,6,8,2,4
after :1,2,3,4,5,6,7,8,9
</pre>
 
=={{header|Elixir}}==
<syntaxhighlight lang="elixir">defmodule Sort do
def cocktail_sort(list) when is_list(list), do: cocktail_sort(list, [], [])
defp cocktail_sort([], minlist, maxlist), do: Enum.reverse(minlist, maxlist)
defp cocktail_sort([x], minlist, maxlist), do: Enum.reverse(minlist, [x | maxlist])
defp cocktail_sort(list, minlist, maxlist) do
{max, rev} = cocktail_max(list, [])
{min, rest} = cocktail_min(rev, [])
cocktail_sort(rest, [min | minlist], [max | maxlist])
end
defp cocktail_max([max], list), do: {max, list}
defp cocktail_max([x,y | t], list) when x<y, do: cocktail_max([y | t], [x | list])
defp cocktail_max([x,y | t], list) , do: cocktail_max([x | t], [y | list])
defp cocktail_min([min], list), do: {min, list}
defp cocktail_min([x,y | t], list) when x>y, do: cocktail_min([y | t], [x | list])
defp cocktail_min([x,y | t], list) , do: cocktail_min([x | t], [y | list])
end
 
IO.inspect Sort.cocktail_sort([5,3,9,4,1,6,8,2,7])</syntaxhighlight>
 
{{out}}
<pre>
[1, 2, 3, 4, 5, 6, 7, 8, 9]
</pre>
 
=={{header|Euphoria}}==
<langsyntaxhighlight lang="euphoria">function cocktail_sort(sequence s)
integer swapped, d
object temp
Line 897 ⟶ 1,852:
constant s = rand(repeat(1000,10))
? s
? cocktail_sort(s)</langsyntaxhighlight>
{{out}}
<pre>{963,398,374,455,53,210,611,285,984,308}
{53,210,285,308,374,398,455,611,963,984}
</pre>
 
=={{header|Factor}}==
===Pseudocode translation===
This is a faithful translation of the pseudocode given in the task description. It uses lexical variables.
<syntaxhighlight lang="factor">USING: kernel locals math math.ranges sequences ;
 
:: cocktail-sort! ( seq -- seq' )
f :> swapped! ! bind false to mutable lexical variable 'swapped'. This must be done outside both while quotations so it is in scope of both.
[ swapped ] [ ! is swapped true? Then execute body quotation. 'do' executes body quotation before predicate on first pass.
f swapped! ! set swapped to false
seq length 2 - [| i | ! for each i in 0 to seq length - 2 do
i i 1 + [ seq nth ] bi@ > ! is element at index i greater than element at index i + 1?
[ i i 1 + seq exchange t swapped! ] when ! if so, swap them and set swapped to true
] each-integer
swapped [ ! skip to end of loop if swapped is false
seq length 2 - 0 [a,b] [| i | ! for each i in seq length - 2 to 0 do
i i 1 + [ seq nth ] bi@ > ! is element at index i greater than element at index i + 1?
[ i i 1 + seq exchange t swapped! ] when ! if so, swap them and set swapped to true
] each
] when
] do while
seq ; ! return the sequence</syntaxhighlight>
 
===More idiomatic===
This is perhaps a more idiomatic solution, eschewing the use of lexical variables. If we had tried to translate the pseudocode directly, we'd be dealing with a data stack that is 6+ values deep. Our main strategy against this is to factor the problem into short words that deal with only a few items at a time. When writing mutating words, we can simplify matters by writing words that return nothing, and letting the caller decide if and how to leave references on the data stack.
<syntaxhighlight lang="factor">USING: fry kernel math math.ranges namespaces sequences ;
 
SYMBOL: swapped?
 
: dupd+ ( m obj -- m n obj ) [ dup 1 + ] dip ;
 
: 2nth ( n seq -- elt1 elt2 ) dupd+ [ nth ] curry bi@ ;
 
: ?exchange ( n seq -- )
2dup 2nth > [ dupd+ exchange swapped? on ] [ 2drop ] if ;
 
: cocktail-pass ( seq forward? -- )
'[ length 2 - 0 _ [ swap ] when [a,b] ] [ ] bi
[ ?exchange ] curry each ;
 
: (cocktail-sort!) ( seq -- seq' )
swapped? off dup t cocktail-pass
swapped? get [ dup f cocktail-pass ] when ;
 
: cocktail-sort! ( seq -- seq' )
[ swapped? get ] [ (cocktail-sort!) ] do while ;</syntaxhighlight>
 
=={{header|Forth}}==
<langsyntaxhighlight lang="forth">defer precedes ( addr addr -- flag )
\ e.g. ' < is precedes
: sort ( a n --)
Line 922 ⟶ 1,923:
-1 cells +loop
repeat then drop 2r> 2drop
;</langsyntaxhighlight>
This is an optimized version:
<langsyntaxhighlight lang="forth">: sort
1- cells bounds 1
begin
Line 935 ⟶ 1,936:
>r negate >r swap r@ cells + r> r>
until drop drop drop
;</langsyntaxhighlight>
 
=={{header|Fortran}}==
{{works with|Fortran|90 and later}}
<langsyntaxhighlight lang="fortran">PROGRAM COCKTAIL
IMPLICIT NONE
 
Line 982 ⟶ 1,983:
END SUBROUTINE Cocktail_sort
 
END PROGRAM COCKTAIL</langsyntaxhighlight>
 
=={{header|FreeBASIC}}==
<syntaxhighlight lang="freebasic">' version 21-10-2016
' compile with: fbc -s console
' for boundry checks on array's compile with: fbc -s console -exx
 
Sub cocktailsort(bs() As Long)
' sort from lower bound to the highter bound
' array's can have subscript range from -2147483648 to +2147483647
Dim As Long lb = LBound(bs)
Dim As Long ub = UBound(bs) -1
Dim As Long done, i
 
Do
done = 0 ' going up
For i = lb To ub
If bs(i) > bs(i +1) Then
Swap bs(i), bs(i +1)
done = 1
End If
Next
ub = ub -1
If done = 0 Then Exit Do ' 0 means the array is sorted
done = 0 ' going down
For i = ub To lb Step -1
If bs(i) > bs(i +1) Then
Swap bs(i), bs(i +1)
done = 1
End If
Next
lb = lb +1
Loop Until done = 0 ' 0 means the array is sorted
 
End Sub
 
' ------=< MAIN >=------
 
Dim As Long i, array(-7 To 7)
 
Dim As Long a = LBound(array), b = UBound(array)
 
Randomize Timer
For i = a To b : array(i) = i : Next
For i = a To b ' little shuffle
Swap array(i), array(Int(Rnd * (b - a +1)) + a)
Next
 
 
Print "unsorted ";
For i = a To b : Print Using "####"; array(i); : Next : Print
cocktailsort(array()) ' sort the array
Print " sorted ";
For i = a To b : Print Using "####"; array(i); : Next : Print
 
 
' empty keyboard buffer
While Inkey <> "" : Wend
Print : Print "hit any key to end program"
Sleep
End</syntaxhighlight>
{{out}}
<pre>unsorted -2 -4 7 -5 -7 4 2 -1 5 -6 6 1 0 -3 3
sorted -7 -6 -5 -4 -3 -2 -1 0 1 2 3 4 5 6 7</pre>
 
=={{header|Gambas}}==
'''[https://gambas-playground.proko.eu/?gist=ee5467e58f0ef649373eed5a2503b988 Click this link to run this code]'''
<syntaxhighlight lang="gambas">Public Sub Main()
Dim siCount, siRev, siProcess As Short
Dim bSorting As Boolean
Dim byToSort As Byte[] = [249, 28, 111, 36, 171, 98, 29, 448, 44, 154, 147, 102, 46, 183, 24,
120, 19, 123, 2, 17, 226, 11, 211, 25, 191, 205, 77]
 
Print "To sort: -"
ShowWorking(byToSort)
Print
Repeat
bSorting = False
siRev = byToSort.Max - 1
For siCount = 0 To byToSort.Max - 1
siProcess = siCount
GoSub Check
siProcess = siRev
GoSub Check
Dec siRev
Next
If bSorting Then ShowWorking(byToSort)
Until bSorting = False
 
Return
 
Check:
 
If byToSort[siProcess] > byToSort[siProcess + 1] Then
Swap byToSort[siProcess], byToSort[siProcess + 1]
bSorting = True
Endif
 
Return
 
End
'-----------------------------------------
Public Sub ShowWorking(byToSort As Byte[])
Dim siCount As Byte
For siCount = 0 To byToSort.Max
Print Str(byToSort[siCount]);
If siCount <> byToSort.Max Then Print ",";
Next
Print
End</syntaxhighlight>
Output:
<pre>
To sort: -
249,28,111,36,171,98,29,192,44,154,147,102,46,183,24,120,19,123,2,17,226,11,211,25,191,205,77
 
2,28,111,36,171,98,29,192,44,154,147,102,46,183,24,120,19,123,11,17,226,25,211,77,191,205,249
2,11,28,36,111,98,29,171,44,154,147,102,46,183,24,120,19,123,17,25,192,77,211,191,205,226,249
2,11,17,28,36,98,29,111,44,154,147,102,46,171,24,120,19,123,25,77,183,191,192,205,211,226,249
2,11,17,19,28,36,29,98,44,111,147,102,46,154,24,120,25,123,77,171,183,191,192,205,211,226,249
2,11,17,19,24,28,29,36,44,98,111,102,46,147,25,120,77,123,154,171,183,191,192,205,211,226,249
2,11,17,19,24,25,28,29,36,44,98,102,46,111,77,120,123,147,154,171,183,191,192,205,211,226,249
2,11,17,19,24,25,28,29,36,44,46,98,77,102,111,120,123,147,154,171,183,191,192,205,211,226,249
2,11,17,19,24,25,28,29,36,44,46,77,98,102,111,120,123,147,154,171,183,191,192,205,211,226,249
</pre>
 
=={{header|Go}}==
<langsyntaxhighlight lang="go">package main
 
import "fmt"
Line 1,020 ⟶ 2,148:
}
}
}</langsyntaxhighlight>
More generic version that sorts anything that implements <code>sort.Interface</code>:
<langsyntaxhighlight lang="go">package main
 
import (
Line 1,060 ⟶ 2,188:
}
}
}</langsyntaxhighlight>
 
=={{header|Groovy}}==
Solution:
<langsyntaxhighlight lang="groovy">def makeSwap = { a, i, j = i+1 -> print "."; a[[j,i]] = a[[i,j]] }
def checkSwap = { a, i, j = i+1 -> [(a[i] > a[j])].find{ it }.each { makeSwap(a, i, j) } }
Line 1,077 ⟶ 2,205:
}
list
}</langsyntaxhighlight>
Test:
<langsyntaxhighlight lang="groovy">println cocktailSort([23,76,99,58,97,57,35,89,51,38,95,92,24,46,31,24,14,12,57,78,4])
println cocktailSort([88,18,31,44,4,0,8,81,14,78,20,76,84,33,73,75,82,5,62,70,12,7,1])
 
println cocktailSort([ 3, 14, 1, 5, 9, 2, 6, 3 ])
println cocktailSort([ 3, 14 ])
println cocktailSort([ 33, 14 ])</langsyntaxhighlight>
{{out}}
<pre>..............................................................................................................................................[4, 12, 14, 23, 24, 24, 31, 35, 38, 46, 51, 57, 57, 58, 76, 78, 89, 92, 95, 97, 99]
Line 1,093 ⟶ 2,221:
 
=={{header|Haskell}}==
<langsyntaxhighlight lang="haskell">cocktailSort :: Ord a => [a] -> [a]
cocktailSort l
| not swapped1 = l
Line 1,106 ⟶ 2,234:
| otherwise = swappingPass op (swapped, x1 : l) (x2 : xs)
swappingPass _ (swapped, l) [x] = (swapped, x : l)
swappingPass _ pair [] = pair</langsyntaxhighlight>
 
=={{header|IoKsh}}==
<syntaxhighlight lang="ksh">#!/bin/ksh
<lang io>List do (
cocktailSortInPlace := method(
start := 0
end := size - 2
 
# cocktail shaker sort
loop(
swapped := false
 
# # Variables:
for(idx, start, end,
#
if(at(idx) > at(idx + 1),
integer TRUE=1
swapped := true
integer FALSE=0
swapIndices(idx, idx + 1)
typeset -a arr=( 5 -1 101 -4 0 1 8 6 2 3 )
)
 
# # Functions:
if(swapped not, break, end := end - 1)
#
function _swap {
typeset _i ; integer _i=$1
typeset _j ; integer _j=$2
typeset _array ; nameref _array="$3"
typeset _swapped ; nameref _swapped=$4
 
typeset _tmp ; _tmp=${_array[_i]}
for (idx, end, start, -1,
_array[_i]=${_array[_j]}
if(at(idx) > at(idx + 1),
_array[_j]=${_tmp}
swapped := true
_swapped=$TRUE
swapIndices(idx, idx + 1)
}
)
)
 
######
if(swapped not, break, start := start + 1)
# main #
)
######
self)
)
 
print "( ${arr[*]} )"
l := list(2, 3, 4, 5, 1)
 
l cocktailSortInPlace println # ==> list(1, 2, 3, 4, 5)</lang>
integer i j
integer swapped=$TRUE
while (( swapped )); do
swapped=$FALSE
for (( i=0 ; i<${#arr[*]}-2 ; i++ , j=i+1 )); do
(( arr[i] > arr[j] )) && _swap ${i} ${j} arr swapped
done
(( ! swapped )) && break
 
swapped=$FALSE
for (( i=${#arr[*]}-2 ; i>0 ; i-- , j=i+1 )); do
(( arr[i] > arr[j] )) && _swap ${i} ${j} arr swapped
done
done
 
print "( ${arr[*]} )"</syntaxhighlight>
{{out}}
<pre>( 5 -1 101 -4 0 1 8 6 2 3 )
( -4 -1 0 1 2 3 5 6 8 101 )</pre>
 
 
=={{header|Haxe}}==
<syntaxhighlight lang="haxe">class CocktailSort {
@:generic
public static function sort<T>(arr:Array<T>) {
var swapped = false;
do {
swapped = false;
for (i in 0...(arr.length - 1)) {
if (Reflect.compare(arr[i], arr[i + 1]) > 0) {
var temp = arr[i];
arr[i] = arr[i + 1];
arr[i + 1] = temp;
swapped = true;
}
}
if (!swapped) break;
swapped = false;
var i = arr.length - 2;
while (i >= 0) {
if (Reflect.compare(arr[i], arr[i + 1]) > 0) {
var temp = arr[i];
arr[i] = arr[i + 1];
arr[i + 1] = temp;
swapped = true;
}
i--;
}
} while (swapped);
}
}
 
class Main {
static function main() {
var integerArray = [1, 10, 2, 5, -1, 5, -19, 4, 23, 0];
var floatArray = [1.0, -3.2, 5.2, 10.8, -5.7, 7.3,
3.5, 0.0, -4.1, -9.5];
var stringArray = ['We', 'hold', 'these', 'truths', 'to',
'be', 'self-evident', 'that', 'all',
'men', 'are', 'created', 'equal'];
Sys.println('Unsorted Integers: ' + integerArray);
CocktailSort.sort(integerArray);
Sys.println('Sorted Integers: ' + integerArray);
Sys.println('Unsorted Floats: ' + floatArray);
CocktailSort.sort(floatArray);
Sys.println('Sorted Floats: ' + floatArray);
Sys.println('Unsorted Strings: ' + stringArray);
CocktailSort.sort(stringArray);
Sys.println('Sorted Strings: ' + stringArray);
}
}</syntaxhighlight>
 
{{out}}
<pre>
Unsorted Integers: [1,10,2,5,-1,5,-19,4,23,0]
Sorted Integers: [-19,-1,0,1,2,4,5,5,10,23]
Unsorted Floats: [1,-3.2,5.2,10.8,-5.7,7.3,3.5,0,-4.1,-9.5]
Sorted Floats: [-9.5,-5.7,-4.1,-3.2,0,1,3.5,5.2,7.3,10.8]
Unsorted Strings: [We,hold,these,truths,to,be,self-evident,that,all,men,are,created,equal]
Sorted Strings: [We,all,are,be,created,equal,hold,men,self-evident,that,these,to,truths]
</pre>
 
=={{header|Icon}} and {{header|Unicon}}==
<langsyntaxhighlight Iconlang="icon">procedure main() #: demonstrate various ways to sort a list and string
demosort(cocktailsort,[3, 14, 1, 5, 9, 2, 6, 3],"qwerty")
end
Line 1,158 ⟶ 2,366:
X[i+1] :=: X[swapped := i]
return X
end</langsyntaxhighlight>
 
Note: This example relies on [[Sorting_algorithms/Bubble_sort#Icon| the supporting procedures 'sortop', and 'demosort' in Bubble Sort]].
Line 1,169 ⟶ 2,377:
on string : "qwerty"
with op = &null: "eqrtwy" (0 ms)</pre>
 
=={{header|Io}}==
<syntaxhighlight lang="io">List do (
cocktailSortInPlace := method(
start := 0
end := size - 2
 
loop(
swapped := false
 
for(idx, start, end,
if(at(idx) > at(idx + 1),
swapped := true
swapIndices(idx, idx + 1)
)
)
 
if(swapped not, break, end := end - 1)
 
for (idx, end, start, -1,
if(at(idx) > at(idx + 1),
swapped := true
swapIndices(idx, idx + 1)
)
)
 
if(swapped not, break, start := start + 1)
)
self)
)
 
l := list(2, 3, 4, 5, 1)
l cocktailSortInPlace println # ==> list(1, 2, 3, 4, 5)</syntaxhighlight>
 
=={{header|IS-BASIC}}==
<syntaxhighlight lang="is-basic">100 PROGRAM "CocktSrt.bas"
110 RANDOMIZE
120 NUMERIC ARRAY(5 TO 24)
130 CALL INIT(ARRAY)
140 CALL WRITE(ARRAY)
150 CALL COCKTAILSORT(ARRAY)
160 CALL WRITE(ARRAY)
170 DEF INIT(REF A)
180 FOR I=LBOUND(A) TO UBOUND(A)
190 LET A(I)=RND(98)+1
200 NEXT
210 END DEF
220 DEF WRITE(REF A)
230 FOR I=LBOUND(A) TO UBOUND(A)
240 PRINT A(I);
250 NEXT
260 PRINT
270 END DEF
280 DEF COCKTAILSORT(REF A)
290 LET ST=LBOUND(A)+1:LET EN=UBOUND(A):LET D,CH=1
300 DO
310 FOR J=ST TO EN STEP D
320 IF A(J-1)>A(J) THEN LET T=A(J-1):LET A(J-1)=A(J):LET A(J)=T:LET CH=J
330 NEXT
340 LET EN=ST:LET ST=CH-D:LET D=-1*D
350 LOOP UNTIL EN*D<ST*D
360 END DEF</syntaxhighlight>
 
=={{header|J}}==
{{eff note|J|/:~}}
<langsyntaxhighlight lang="j">bigToLeft=: (([ (>. , <.) {.@]) , }.@])/
smallToLeft=: (([ (<. , >.) {.@]) , }.@])/
cocktailSort=: |. @: (|. @: smallToLeft @: |. @: bigToLeft ^:_)</langsyntaxhighlight>
Test run:
<langsyntaxhighlight lang="j"> ?. 10 $ 10
4 6 8 6 5 8 6 6 6 9
cocktailSort ?. 10 $ 10
4 5 6 6 6 6 6 8 8 9</langsyntaxhighlight>
As is usual with J, <code>/:~</code> is the preferred method of sorting in practice.
 
Line 1,185 ⟶ 2,455:
This algorithm sorts in place.
Call it with a copy of the array to preserve the unsorted order.
<langsyntaxhighlight lang="java">public static void cocktailSort( int[] A ){
boolean swapped;
do {
Line 1,213 ⟶ 2,483:
//if no elements have been swapped, then the list is sorted
} while (swapped);
}</langsyntaxhighlight>
 
=={{header|JavaScript}}==
<syntaxhighlight lang="javascript">
// Node 5.4.1 tested implementation (ES6)
"use strict";
 
let arr = [4, 9, 0, 3, 1, 5];
let isSorted = true;
while (isSorted){
for (let i = 0; i< arr.length - 1;i++){
if (arr[i] > arr[i + 1])
{
let temp = arr[i];
arr[i] = arr[i + 1];
arr[i+1] = temp;
isSorted = true;
}
}
 
if (!isSorted)
break;
isSorted = false;
 
for (let j = arr.length - 1; j > 0; j--){
if (arr[j-1] > arr[j])
{
let temp = arr[j];
arr[j] = arr[j - 1];
arr[j - 1] = temp;
isSorted = true;
}
}
}
console.log(arr);
 
}</syntaxhighlight>
 
 
{{out}}
<pre>
[0, 1, 3, 4, 5, 9]
</pre>
 
=={{header|jq}}==
{{ works with|jq|1.4}}
<langsyntaxhighlight lang="jq"># In case your jq does not have "until" defined:
def until(cond; next):
def _until:
if cond then . else (next|_until) end;
_until;</langsyntaxhighlight>
<langsyntaxhighlight lang="jq">def cocktailSort:
def swap(i;j): .[i] as $t | .[i] = .[j] | .[j] = $t;
 
Line 1,244 ⟶ 2,557:
else .
end )
| .[1];</langsyntaxhighlight>
'''Tests:'''
<langsyntaxhighlight lang="jq">def verify: if cocktailSort == sort then empty else . end;
 
([],
Line 1,258 ⟶ 2,571:
[1.5, -1.5],
["cocktail", ["sort"], null, {}]
) | verify</langsyntaxhighlight>
{{out}}
<langsyntaxhighlight lang="sh">$ jq -n -c -f cocktail_sort.jq
$</langsyntaxhighlight>
 
=={{header|Julia}}==
<lang{{works with|Julia>|0.6}}
 
function coctailsort{T<:Real}(a::Array{T,1})
<syntaxhighlight lang="julia">function cocktailsort(a::Vector)
b = copy(a)
isordered = false
lo, hi = one1, length(Intb)
hi = length(b)
while !isordered && hi > lo
isordered = true
for i in lo+1:hi
if b[i] < b[i-1]
tb[i-1], b[i] = b[i], b[i-1]
b[i] = b[i-1]
b[i-1] = t
isordered = false
end
end
hi -= 1
if isordered || hi <= lo break end
break
end
for i in hi:-1:lo+1
if b[i-1] > b[i]
tb[i-1], b[i] = b[i], b[i-1]
b[i] = b[i-1]
b[i-1] = t
isordered = false
end
Line 1,297 ⟶ 2,604:
end
 
av = [rand(-2^10:2^10, 10) for i in 1:20]
println("# unordered: $v\n -> ordered: ", cocktailsort(v))</syntaxhighlight>
println("Before Sort:")
println(a)
a = coctailsort(a)
println("\nAfter Sort:")
println(a)
</lang>
 
{{out}}
<pre># unordered: [6, -9, 5, -10, 2, 4, 6, -6, -3, -8]
<pre>
-> ordered: [-10, -9, -8, -6, -3, 2, 4, 5, 6, 6]</pre>
Before Sort:
[238,663,-301,-1011,-14,-819,985,836,-164,-335,-307,89,-110,-618,391,76,521,954,441,625]
 
=={{header|Kotlin}}==
After Sort:
<syntaxhighlight lang="scala">// version 1.1.0
[-1011,-819,-618,-335,-307,-301,-164,-110,-14,76,89,238,391,441,521,625,663,836,954,985]
</pre>
 
fun cocktailSort(a: IntArray) {
'''Room for Improvement'''
fun swap(i: Int, j: Int) {
* Because the swap code is used twice, implement it as a macro
val temp = a[i]
* As written this sort applies only to lists of real numbers, but <code>cocktailsort</code> should be applicable to other objects (any for which <code>isless</code> and <code>isequal</code> are defined).
a[i] = a[j]
a[j] = temp
}
do {
var swapped = false
for (i in 0 until a.size - 1)
if (a[i] > a[i + 1]) {
swap(i, i + 1)
swapped = true
}
if (!swapped) break
swapped = false
for (i in a.size - 2 downTo 0)
if (a[i] > a[i + 1]) {
swap(i, i + 1)
swapped = true
}
}
while (swapped)
}
 
fun main(args: Array<String>) {
val aa = arrayOf(
intArrayOf(100, 2, 56, 200, -52, 3, 99, 33, 177, -199),
intArrayOf(4, 65, 2, -31, 0, 99, 2, 83, 782, 1),
intArrayOf(62, 83, 18, 53, 7, 17, 95, 86, 47, 69, 25, 28)
)
for (a in aa) {
cocktailSort(a)
println(a.joinToString(", "))
}
}</syntaxhighlight>
 
{{out}}
<pre>
-199, -52, 2, 3, 33, 56, 99, 100, 177, 200
-31, 0, 1, 2, 2, 4, 65, 83, 99, 782
7, 17, 18, 25, 28, 47, 53, 62, 69, 83, 86, 95
</pre>
 
=={{header|Lua}}==
<langsyntaxhighlight Lualang="lua">function cocktailSort( A )
local swapped
repeat
Line 1,341 ⟶ 2,680:
 
until swapped==false
end</langsyntaxhighlight>
{{out|Example}}
<langsyntaxhighlight lang="lua">list = { 5, 6, 1, 2, 9, 14, 2, 15, 6, 7, 8, 97 }
cocktailSort(list)
for i=1,#list do
print(list[i]j)
end</langsyntaxhighlight>
=={{header|M2000 Interpreter}}==
<syntaxhighlight lang="m2000 interpreter">
Module cocktailSort {
k=(3,2,1)
print k
cocktailSort(k)
print k
k=("c","b","a")
print k
cocktailSortString(k)
print k
Dim a(5)
a(0)=1,2,5,6,3
print a()
cocktailSort(a())
print a()
End
Sub cocktailSort(a)
\\ this is like Link a to a() but using new for a() - shadow any a()
push &a : read new &a()
local swapped, lenA2=len(a)-2
do
for i=0 to lenA2
if a(i) > a(i+1) then swap a(i), a(i+1): swapped = true
next
if swapped then
swapped~
for i=lenA2 to 0
if a(i) > a(i+1) then swap a(i), a(i+1): swapped = true
next
end if
until not swapped
End Sub
Sub cocktailSortString(a)
push &a : read new &a$()
local swapped, lenA2=len(a)-2
do
for i=0 to lenA2
if a$(i) > a$(i+1) then
swap a$(i), a$(i+1)
swapped = true
end if
next
if swapped then
swapped~
for i=lenA2 to 0
if a$(i) > a$(i+1) then
swap a$(i), a$(i+1)
swapped = true
end if
next
end if
until not swapped
End Sub
}
cocktailSort
</syntaxhighlight>
{{out}}
<pre>
3 2 1
1 2 3
c b a
a b c
1 2 5 6 3
1 2 3 5 6
</pre>
 
=={{header|Maple}}==
<syntaxhighlight lang="maple">arr := Array([17,3,72,0,36,2,3,8,40,0]):
len := numelems(arr):
swap := proc(arr, a, b)
local temp := arr[a]:
arr[a] := arr[b]:
arr[b] := temp:
end proc:
while(true) do
swapped := false:
for i to len-1 do
if arr[i] > arr[i+1] then:
swap(arr, i, i+1):
swapped := true:
end if:
end do:
if (not swapped) then break: end if:
swapped := false:
for j from len-1 to 1 by -1 do
if arr[j] > arr[j+1] then
swap(arr,j,j+1):
swapped := true:
end if:
end do:
if (not swapped) then break: end if:
end do:
arr;</syntaxhighlight>
{{Output|Out}}
<pre>[0,0,2,3,3,8,17,36,40,72]</pre>
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<langsyntaxhighlight Mathematicalang="mathematica">cocktailSort[A_List] := Module[ { swapped = True },
While[ swapped == True,
swapped=False;
Line 1,360 ⟶ 2,796:
For [ i= Length[A]-1, i > 0, i--,
If[ A[[i]] > A[[i+1]], A[[i;;i+1]] = A[[i+1;;i;;-1]]; swapped = True;]
]]]</langsyntaxhighlight>
{{out|Example}}
<pre>cocktailSort[{2,1,5,3,6}]
Line 1,366 ⟶ 2,802:
 
=={{header|MATLAB}} / {{header|Octave}}==
<langsyntaxhighlight MATLABlang="matlab">function list = cocktailSort(list)
%We have to do this because the do...while loop doesn't exist in MATLAB
Line 1,395 ⟶ 2,831:
end %for
end %while
end %cocktail sort</langsyntaxhighlight>
{{out|Sample usage}}
<langsyntaxhighlight MATLABlang="matlab">cocktailSort([6 3 7 8 5 1 2 4 9])
 
ans =
 
1 2 3 4 5 6 7 8 9</langsyntaxhighlight>
 
=={{header|MAXScript}}==
<langsyntaxhighlight lang="maxscript">fn cocktailSort arr =
(
local swapped = true
Line 1,429 ⟶ 2,865:
)
return arr
)</langsyntaxhighlight>
 
=={{header|NetRexx}}==
<langsyntaxhighlight NetRexxlang="netrexx">/* NetRexx */
options replace format comments java crossref savelog symbols binary
 
Line 1,488 ⟶ 2,924:
 
method isFalse public constant binary returns boolean
return \isTrue</langsyntaxhighlight>
{{out}}
<pre>
Line 1,511 ⟶ 2,947:
 
=={{header|Nim}}==
<syntaxhighlight lang ="nim">template trySwap(): stmt {.immediate.}untyped =
if a[i] < a[i-1]:
swap a[i], a[i-1]
Line 1,521 ⟶ 2,957:
while not t:
t = true
for i in 1 .. < l: trySwap
if t: break
for i in countdown(l-1, 1): trySwap
Line 1,527 ⟶ 2,963:
var a = @[4, 65, 2, -31, 0, 99, 2, 83, 782]
cocktailSort a
echo a</langsyntaxhighlight>
{{out}}
<pre>@[-31, 0, 2, 2, 4, 65, 83, 99, 782]</pre>
 
=={{header|Objeck}}==
<langsyntaxhighlight lang="objeck">bundle Default {
class Cocktail {
function : Main(args : String[]) ~ Nil {
Line 1,573 ⟶ 3,009:
}
}
}</langsyntaxhighlight>
 
=={{header|OCaml}}==
<langsyntaxhighlight lang="ocaml">let swap a i j =
let tmp = a.(i) in
a.(i) <- a.(j);
Line 1,612 ⟶ 3,048:
Array.iter (Printf.printf " %d") a;
print_newline();
;;</langsyntaxhighlight>
{{out}}
<pre>
Line 1,619 ⟶ 3,055:
 
=={{header|Octave}}==
<langsyntaxhighlight lang="octave">function sl = cocktailsort(l)
swapped = true;
while(swapped)
Line 1,645 ⟶ 3,081:
endwhile
sl = l;
endfunction</langsyntaxhighlight>
{{out|Example}}
<langsyntaxhighlight lang="octave">s = cocktailsort([5, -1, 101, -4, 0, \
1, 8, 6, 2, 3 ]);
disp(s);</langsyntaxhighlight>
 
=={{header|ooRexx}}==
{{Trans|NetRexx}}
<langsyntaxhighlight ooRexxlang="oorexx">/* Rexx */
 
placesList = .array~of( -
Line 1,703 ⟶ 3,139:
end swaps
 
return A</langsyntaxhighlight>
{{out}}
<pre>
Line 1,726 ⟶ 3,162:
 
=={{header|Oz}}==
<langsyntaxhighlight lang="oz">declare
proc {CocktailSort Arr}
proc {Swap I J}
Line 1,751 ⟶ 3,187:
in
{CocktailSort Arr}
{Inspect Arr}</langsyntaxhighlight>
 
=={{header|PARI/GP}}==
<langsyntaxhighlight lang="parigp">cocktailSort(v)={
while(1,
my(done=1);
Line 1,777 ⟶ 3,213:
if(done, return(v))
)
};</langsyntaxhighlight>
 
=={{header|Pascal}}==
See [[Sorting_algorithms/Cocktail_sort#Delphi | Delphi]]
 
=={{header|Perl}}==
<langsyntaxhighlight lang="perl">use strict;
use warnings;
use feature 'say';
 
sub cocktail_sort {
my @B=qw(t h e q u i c k b r o w n f o x j u m p s o v e r t h e l a z y d o g);
my @a = @_;
print "@B\n";
while (1) {
my @C=cocktailSort(@B);
print "@C\n";
################### cocktailSort #####################
sub cocktailSort { #( A : list of sortable items ) defined as:
my @A = @_;
my $swapped = 1;
while ($swapped == 1) {
$swapped = 0;
for (my $i=0; $i<($#A-1); $i+=1) {
 
if ($A[$i] gt $A[$i+1]) { # test whether the two
# elements are in the wrong
# order
 
($A[$i+1], $A[$i])=($A[$i], $A[$i+1]); # let the two elements
# change places
$swapped = 1;
}
}
if ($swapped == 0) {
# we can exit the outer loop here if no swaps occurred.
print "no more swaps";
}
else {
$swapped = 0;
for (my $i=($#A-1); $i>0 ; $i-=1) {
 
if($A[$i] gt $A[$i+1]) {
 
($A[$i+1], $A[$i])=($A[$i], $A[$i+1]);
$swapped = 1;
}
}
}
# if no elements have been swapped,
# then the list is sorted
}
return (@A);
#end sub
}</lang>
 
=={{header|Perl 6}}==
<lang perl6>sub cocktail_sort ( @a ) {
my $range = 0 ..^ @a.end;
loop {
my $swapped_forward = 0;
for $range.list ->my $i (0..$#a-1) {
if @($a[$i] >gt @$a[$i+1]) {
@a[ $i, $i+1 ] .= reverse@a[$i+1, $i];
$swapped_forward = 1;
}
}
last if not $swapped_forward;
 
my $swapped_backward = 0;
for my $range.i (reverse -> 0..$i#a-1) {
if @($a[$i] >gt @$a[$i+1]) {
@a[ $i, $i+1 ] .= reverse@a[$i+1, $i];
$swapped_backward = 1;
}
Line 1,851 ⟶ 3,244:
last if not $swapped_backward;
}
return @a;
}
 
say join ' ', cocktail_sort( <t h e q u i c k b r o w n f o x j u m p s o v e r t h e l a z y d o g> );</syntaxhighlight>
my @weights = (^50).map: { 100 + ( 1000.rand.Int / 10 ) };
{{out}}
say @weights.sort.Str eq @weights.&cocktail_sort.Str ?? 'ok' !! 'not ok';</lang>
<pre>a b c d e e e f g h h i j k l m n o o o o p q r r s t t u u v w x y z</pre>
 
=={{header|Phix}}==
<!--<syntaxhighlight lang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">cocktail_sort</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">s</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">deep_copy</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">swapped</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">f</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">t</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">)-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">d</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">1</span>
<span style="color: #008080;">while</span> <span style="color: #000000;">swapped</span> <span style="color: #008080;">do</span>
<span style="color: #000000;">swapped</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">0</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">f</span> <span style="color: #008080;">to</span> <span style="color: #000000;">t</span> <span style="color: #008080;">by</span> <span style="color: #000000;">d</span> <span style="color: #008080;">do</span>
<span style="color: #004080;">object</span> <span style="color: #000000;">si</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">],</span>
<span style="color: #000000;">sn</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">si</span><span style="color: #0000FF;">></span><span style="color: #000000;">sn</span> <span style="color: #008080;">then</span>
<span style="color: #000000;">s</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">sn</span>
<span style="color: #000000;">s</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">si</span>
<span style="color: #000000;">swapped</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">1</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #000080;font-style:italic;">-- swap to and from, and flip direction.
-- additionally, we can reduce one element to be
-- examined, depending on which way we just went.</span>
<span style="color: #0000FF;">{</span><span style="color: #000000;">f</span><span style="color: #0000FF;">,</span><span style="color: #000000;">t</span><span style="color: #0000FF;">,</span><span style="color: #000000;">d</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">t</span><span style="color: #0000FF;">-</span><span style="color: #000000;">d</span><span style="color: #0000FF;">,</span><span style="color: #000000;">f</span><span style="color: #0000FF;">,-</span><span style="color: #000000;">d</span><span style="color: #0000FF;">}</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">while</span>
<span style="color: #008080;">return</span> <span style="color: #000000;">s</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">constant</span> <span style="color: #000000;">s</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">sq_rand</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1000</span><span style="color: #0000FF;">,</span><span style="color: #000000;">10</span><span style="color: #0000FF;">))</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"original: %V\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">s</span><span style="color: #0000FF;">})</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">" sorted: %V\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">cocktail_sort</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">)})</span>
<!--</syntaxhighlight>-->
{{out}}
<pre>
original: {157,371,822,436,89,506,46,791,22,147}
sorted: {22,46,89,147,157,371,436,506,791,822}
</pre>
 
=={{header|PHP}}==
<langsyntaxhighlight lang="php">function cocktailSort($arr){
if (is_string($arr)) $arr = str_split(preg_replace('/\s+/','',$arr));
 
Line 1,895 ⟶ 3,325:
echo implode(',',$arr) . '<br>';
echo implode(',',$arr2) . '<br>';
echo implode('',$strg) . '<br>';</langsyntaxhighlight>
{{out}}
<pre>
Line 1,904 ⟶ 3,334:
 
=={{header|PicoLisp}}==
<langsyntaxhighlight PicoLisplang="picolisp">(de cocktailSort (Lst)
(use (Swapped L)
(loop
Line 1,922 ⟶ 3,352:
(on Swapped) )
(T (== Lst L)) )
(NIL Swapped Lst) ) ) )</langsyntaxhighlight>
{{out}}
<pre>: (cocktailSort (make (do 9 (link (rand 1 999)))))
Line 1,930 ⟶ 3,360:
 
=={{header|PL/I}}==
<langsyntaxhighlight PLlang="pl/Ii">cocktail: procedure (A);
declare A(*) fixed;
declare t fixed;
Line 1,946 ⟶ 3,376:
end;
end;
end cocktail;</langsyntaxhighlight>
 
=={{header|PowerShell}}==
Based on the entry for PowerShell in [[Bubble Sort]]
<langsyntaxhighlight PowerShelllang="powershell">function CocktailSort ($a) {
$l = $a.Length
$m = 0
Line 1,981 ⟶ 3,411:
}
 
$l = 10; CocktailSort ( 1..$l | ForEach-Object { $Rand = New-Object Random }{ $Rand.Next( -( $l - 1 ), $l - 1 ) } )</langsyntaxhighlight>
 
=={{header|Prolog}}==
<langsyntaxhighlight Prologlang="prolog">ctail(_, [], Rev, Rev, sorted) :- write(Rev), nl.
ctail(fwrd, [A,B|T], In, Rev, unsorted) :- A > B, !,
ctail(fwrd, [B,A|T], In, Rev, _).
Line 2,002 ⟶ 3,432:
cocktail(In, R),
writef('-> %w\n', [R]).
</syntaxhighlight>
</lang>
{{out|Example}}
<pre>?- test.
Line 2,016 ⟶ 3,446:
=={{header|PureBasic}}==
The following approach improves on the method in the pseudo-code by not examining indexes on either end of the array that have already been sorted by reducing the index range on each subsequent pass.
<langsyntaxhighlight PureBasiclang="purebasic">;sorts an array of integers
Procedure cocktailSort(Array a(1))
Protected index, hasChanged, low, high
Line 2,046 ⟶ 3,476:
low + 1
Until hasChanged = #False ;if no elements have been changed, then the array is sorted
EndProcedure</langsyntaxhighlight>
 
=={{header|Python}}==
This implementation takes advantage of the identical processing of the two ''for'' loops and that a ''range'' is a first-class object in Python.
<langsyntaxhighlight lang="python">def cocktailSort(A):
up = range(len(A)-1)
while True:
Line 2,060 ⟶ 3,490:
swapped = True
if not swapped:
return</langsyntaxhighlight>
{{out|Usage}}
<langsyntaxhighlight lang="python">test1 = [7, 6, 5, 9, 8, 4, 3, 1, 2, 0]
cocktailSort(test1)
print test1
Line 2,070 ⟶ 3,500:
cocktailSort(test2)
print ''.join(test2)
#>>> abcdefghiijklmnopqrstuvwxyz</langsyntaxhighlight>
 
This implementation is clearer in structure to it's bubblesort origins while also being ever so slightly faster, in python 3.5.2 at least.
<syntaxhighlight lang="python">def cocktail(a):
for i in range(len(a)//2):
swap = False
for j in range(1+i, len(a)-i):
if a[j] < a[j-1]:
a[j], a[j-1] = a[j-1], a[j]
swap = True
if not swap:
break
swap = False
for j in range(len(a)-i-1, i, -1):
if a[j] < a[j-1]:
a[j], a[j-1] = a[j-1], a[j]
swap = True
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}}==
The previously solution missed out on a cool R trick for swapping items. As R is 1-indexed, we have made some minor adjustments to the given pseudo-code. Otherwise, we have aimed to be faithful to it.
<lang R>cocktailsort <- function(x)
<syntaxhighlight lang="rsplus">cocktailSort <- function(A)
{
repeat
lenx <- length(x)
repeat{
swapped <- FALSE
{
for(i in swappedseq_len(length(A) <- FALSE1))
{
for(i in 1:(lenx-1))
if(A[i] > A[i + 1])
{
if(xA[c(i, i + 1)] ><- xA[c(i + 1], i)]#The cool trick mentioned above.
swapped <- TRUE {
temp <- x[i]
x[i] <- x[i+1]
x[i+1] <- temp
swapped <- TRUE
}
}
}
if(!swapped) break
if(!swapped) break
swapped <- FALSE
for(i in (lenxlength(A)-1):1)
{
if(A[i] > A[i + 1])
{
if(xA[c(i, i + 1)] ><- xA[c(i + 1], i)]
swapped <- TRUE {
temp <- x[i]
x[i] <- x[i+1]
x[i+1] <- temp
swapped <- TRUE
}
}
}
if(!swapped) break
} if(!swapped) break
x }
A
}
#Examples taken from the Haxe solution.
ints <- c(1, 10, 2, 5, -1, 5, -19, 4, 23, 0)
numerics <- c(1, -3.2, 5.2, 10.8, -5.7, 7.3, 3.5, 0, -4.1, -9.5)
strings <- c("We", "hold", "these", "truths", "to", "be", "self-evident", "that", "all", "men", "are", "created", "equal")</syntaxhighlight>
 
{{out}}
print(cocktailsort(c(5, -1, 101, -4, 0, 1, 8, 6, 2, 3)))</lang>
<pre>> cocktailSort(ints)
[1] -19 -1 0 1 2 4 5 5 10 23
> cocktailSort(numerics)
[1] -9.5 -5.7 -4.1 -3.2 0.0 1.0 3.5 5.2 7.3 10.8
> cocktailSort(strings)
[1] "all" "are" "be" "created" "equal" "hold" "men"
[8] "self-evident" "that" "these" "to" "truths" "We"</pre>
 
=={{header|Racket}}==
<langsyntaxhighlight lang="racket">
#lang racket
(require (only-in srfi/43 vector-swap!))
Line 2,127 ⟶ 3,618:
[(zero? (bubble (- len 2) 0 -1)) xs]
[(loop)])))
</syntaxhighlight>
</lang>
 
=={{header|REXXRaku}}==
(formerly Perl 6)
===version handles blanks===
<syntaxhighlight lang="raku" line>sub cocktail_sort ( @a ) {
This REXX version can handle an array that may contain blanks or spaces.
my $range = 0 ..^ @a.end;
<lang rexx>/*REXX program sorts an array using the cocktail-sort method, a.k.a.: */
loop {
/* bidirectional bubble sort, */
my $swapped_forward = 0;
/* cocktail shaker sort, */
for $range.list -> $i {
/* a selection sort variation,*/
if @a[$i] > @a[$i+1] {
/* ripple sort, */
@a[ /* shuffle sort$i, $i+1 ] .= */reverse;
$swapped_forward = 1;
/* shuttle sort, */
}
/* happy hour sort, */
}
/* a bubble sort variation. */
last if not $swapped_forward;
 
my $swapped_backward = 0;
call gen@ /*generate some array elements. */
for $range.reverse -> $i {
call show@ 'before sort' /*show unsorted array elements.*/
if @a[$i] > @a[$i+1] {
call cocktailSort highItem /*invoke cocktail sort subroutine*/
call show@ ' after sort' @a[ $i, $i+1 /*show] .= sorted array elements.*/reverse;
$swapped_backward = 1;
exit /*stick a fork in it, we're done.*/
}
/*──────────────────────────────────COCKTAILSORT subroutine─────────────*/
}
cocktailSort: procedure expose @.; parse arg N /*N=# of items.*/
last if not $swapped_backward;
}
return @a;
}
 
my @weights = (^50).map: { 100 + ( 1000.rand.Int / 10 ) };
do until done; done=1
say @weights.sort.Str eq @weights.&cocktail_sort.Str ?? 'ok' !! 'not ok';</syntaxhighlight>
 
=={{header|REXX}}==
do j=1 for N-1; jp=j+1
===version handles blanks===
if @.j>@.jp then do; done=0; _=@.j; @.j=@.jp; @.jp=_; end
This REXX version can handle array elements that may contain blanks or spaces.
end /*j*/
<syntaxhighlight lang="rexx">/*REXX program sorts an array using the cocktail─sort method, A.K.A.: happy hour sort,*/
/* bidirectional bubble sort, */
/* cocktail shaker sort, ripple sort,*/
/* a selection sort variation, */
/* shuffle sort, shuttle sort, or */
/* a bubble sort variation. */
call genItems /*generate some array elements. */
call showItems 'before sort' /*show unsorted array elements. */
say copies('█', 101) /*show a separator line (a fence). */
call cocktailSort /*invoke the cocktail sort subroutine. */
call showItems ' after sort' /*show sorted array elements. */
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
cocktailSort: procedure expose items.
nn = items.0 - 1 /*items.0: is number of items. */
do until done
done = 1
do j = 1 for nn
jp = j + 1 /* Rexx doesn't allow "items.(j+1)", so use this instead. */
if items.j > items.jp then do
done = 0
temp = items.j
items.j = items.jp
items.jp = temp
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.3 ='the juggler ◄─── I'
items.4 ='the high priestess [Popess] ◄─── II'
items.5 ='the empress ◄─── III'
items.6 ='the emperor ◄─── IV'
items.7 ='the hierophant [Pope] ◄─── V'
items.8 ='the lovers ◄─── VI'
items.9 ='the chariot ◄─── VII'
items.10='justice ◄─── VIII'
items.11='the hermit ◄─── IX'
items.12='fortune [the wheel of] ◄─── X'
items.13='strength ◄─── XI'
items.14='the hanging man ◄─── XII'
items.15='death [often unlabeled] ◄─── XIII'
items.16='temperance ◄─── XIV'
items.17='the devil ◄─── XV'
items.18='lightning [the tower] ◄─── XVI'
items.19='the stars ◄─── XVII'
items.20='the moon ◄─── XVIII'
items.21='the sun ◄─── XIX'
items.22='judgment ◄─── XX'
items.23='the world ◄─── XXI'
items.24='the fool [often unnumbered] ◄─── XXII'
items.0 =24 /* number of entries in the array. */
 
return
if done then leave /*No swaps done? Then we're done*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
showitems: procedure expose items.
parse arg phase
width = length(items.0)
do j=1 to items.0 /* items.0 is the number of items in items. */
say 'element' right(j, width) phase || ":" items.j
end /*j*/ /* ↑ */
/* └─────max width of any line number. */
return</syntaxhighlight>
{{out|output|text=&nbsp; when using the internal default inputs:}}
 
(Shown at three-quarter size.)
do k=N-1 for N-1 by -1; kp=k+1
<pre style="font-size:75%">
if @.k>@.kp then do; done=0; _=@.k; @.k=@.kp; @.kp=_; end
end /*k*/
 
end /*until*/
return
/*──────────────────────────────────GEN@ subroutine─────────────────────*/
gen@: @.='' /*assign default value for stem. */
@.1 ='---the 22 card tarot deck (larger deck has 56 additional cards in 4 suits)---'
@.2 ='==========symbol====================pip======================================'
@.3 ='the juggler ◄─── I'
@.4 ='the high priestess [Popess] ◄─── II'
@.5 ='the empress ◄─── III'
@.6 ='the emperor ◄─── IV'
@.7 ='the hierophant [Pope] ◄─── V'
@.8 ='the lovers ◄─── VI'
@.9 ='the chariot ◄─── VII'
@.10='justice ◄─── VIII'
@.11='the hermit ◄─── IX'
@.12='fortune [the wheel of] ◄─── X'
@.13='strength ◄─── XI'
@.14='the hanging man ◄─── XII'
@.15='death [often unlabeled] ◄─── XIII'
@.16='temperance ◄─── XIV'
@.17='the devil ◄─── XV'
@.18='lightning [the tower] ◄─── XVI'
@.18='the stars ◄─── XVII'
@.20='the moon ◄─── XVIII'
@.21='the sun ◄─── XIX'
@.22='judgment ◄─── XX'
@.23='the world ◄─── XXI'
@.24='the fool [often unnumbered] ◄─── XXII'
 
do i=1 while @.i\==''; end /*find how many entries in array.*/
 
highItem=i-1 /*adjust for DO loop advancement.*/
return
/*──────────────────────────────────SHOW@ subroutine────────────────────*/
show@: widthH=length(highItem) /*the maximum width of any line. */
 
do j=1 for highItem
say 'element' right(j,widthH) arg(1)":" @.j
end
say copies('─',79) /*show a separator line or fence.*/
return</lang>
{{out}}
<pre style="height:30ex;overflow:scroll">
element 1 before sort: ---the 22 card tarot deck (larger deck has 56 additional cards in 4 suits)---
element 2 before sort: ==========symbol=====================pip======================================
element 3 before sort: the juggler ◄─── I
element 4 before sort: the high priestess [Popess] ◄─── II
element 5 before sort: the empress ◄─── III
element 6 before sort: the emperor ◄─── IV
element 7 before sort: the hierophant [Pope] ◄─── V
element 8 before sort: the lovers ◄─── VI
element 9 before sort: the chariot ◄─── VII
element 10 before sort: justice ◄─── VIII
element 11 before sort: the hermit ◄─── IX
element 12 before sort: fortune [the wheel of] ◄─── X
element 13 before sort: strength ◄─── XI
element 14 before sort: the hanging man ◄─── XII
element 15 before sort: death [often unlabeled] ◄─── XIII
element 16 before sort: temperance ◄─── XIV
element 17 before sort: the devil ◄─── XV
element 18 before sort: thelightning stars [the tower] ◄─── ◄─── XVIIXVI
element 19 before sort: the stars ◄─── XVII
────────────────────────────────────────────────────────────────────────────────
element 20 before sort: the moon ◄─── XVIII
element 21 before sort: the sun ◄─── XIX
element 22 before sort: judgment ◄─── XX
element 23 before sort: the world ◄─── XXI
element 24 before sort: the fool [often unnumbered] ◄─── XXII
█████████████████████████████████████████████████████████████████████████████████████████████████████
element 1 after sort: ---the 22 card tarot deck (larger deck has 56 additional cards in 4 suits)---
element 2 after sort: ==========symbol=====================pip======================================
element 3 after sort: death [often unlabeled] ◄─── XIII
element 4 after sort: fortune [the wheel of] ◄─── X
element 5 after sort: justicejudgment ◄─── ◄─── VIIIXX
element 6 after sort: strengthjustice ◄─── XIVIII
element 7 after sort: temperance lightning [the tower] ◄─── XIVXVI
element 8 after sort: thestrength chariot ◄─── VII XI
element 9 after sort: the devil temperance ◄─── XVXIV
element 10 after sort: the emperor chariot ◄─── IVVII
element 11 after sort: the empressdevil ◄─── III XV
element 12 after sort: the hangingemperor man ◄─── XII IV
element 13 after sort: the hermit empress ◄─── IXIII
element 14 after sort: the hierophantfool [Popeoften unnumbered] ◄─── VXXII
element 15 after sort: the highhanging priestessman [Popess] ◄─── II ◄─── XII
element 16 after sort: the jugglerhermit ◄─── IIX
element 17 after sort: the lovershierophant [Pope] ◄─── ◄─── VIV
element 18 after sort: the starshigh priestess [Popess] ◄─── ◄─── XVIIII
element 19 after sort: the juggler ◄─── I
────────────────────────────────────────────────────────────────────────────────
element 20 after sort: the lovers ◄─── VI
element 21 after sort: the moon ◄─── XVIII
element 22 after sort: the stars ◄─── XVII
element 23 after sort: the sun ◄─── XIX
element 24 after sort: the world ◄─── XXI
</pre>
 
===version handles non-blanks===
This faster REXX version can handle an array elements that doendon't contain blanks or spaces by using a simpliersimpler ''swap'' mechanism.
The REXX ''PARSE'' instruction separates an input into parts and assigns them to
<lang rexx>/*──────────────────────────────────COCKTAILSORT2 subroutine────────────*/
variables, in a single operation. Thus
cocktailSort2: procedure expose @.; parse arg N /*N=# of items.*/
<syntaxhighlight lang="rexx">PARSE VALUE 0 items.j items.jp WITH done items.jp items.j</syntaxhighlight>
/*array items may not contain blanks*/
sets ''done'' to 0, ''items.jp'' to ''items.j'', and ''items.j'' to ''items.jp'', as long as none of the input
do until done; done=1
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 nn
jp = j + 1 /* Rexx doesn't allow "items.(j+1)", so use this instead. */
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 */
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>
do j=1 for N-1; jp=j+1
if @.j>@.jp then parse value 0 @.j @.jp with done @.jp @.j
end /*j*/
 
=={{header|Ring}}==
if done then leave /*No swaps done? Then we're done*/
<syntaxhighlight lang="ring">
aList = [ 5, 6, 1, 2, 9, 14, 2, 15, 6, 7, 8, 97]
flag = 0
cocktailSort(aList)
for i=1 to len(aList)
see "" + aList[i] + " "
next
 
func cocktailSort A
do k=N-1 for N-1 by -1; kp=k+1
n = len(A)
if @.k>@.kp then parse value 0 @.k @.kp with done @.kp @.k
while flag = end /*k*/0
flag = 1
for i = 1 to n-1
if A[i] > A[i+1]
temp = A[i]
A[i] = A[i+1]
A [i+1] = temp
flag = 0
ok
next
end
</syntaxhighlight>
 
=={{header|RPL}}==
end /*until*/
{{works with|RPL|HP-49C}}
return</lang>
« '''DO''' 1 CF
1 OVER SIZE 1 - '''FOR''' h
DUP h DUP 1 + SUB
'''IF''' DUP EVAL > '''THEN'''
h SWAP REVLIST REPL
1 SF
'''ELSE''' DROP '''END'''
'''NEXT'''
'''IF''' 1 FS? '''THEN'''
DUP SIZE 1 - 1 '''FOR''' h
DUP h DUP 1 + SUB
'''IF''' DUP EVAL > '''THEN'''
h SWAP REVLIST REPL
1 SF
'''ELSE''' DROP '''END'''
-1 '''STEP'''
'''END'''
'''UNTIL''' 1 FC? '''END'''
» '<span style="color:blue">COCKTAILSORT</span>' STO
 
{ 7 6 5 9 8 4 3 1 2 0 } <span style="color:blue">COCKTAILSORT</span>
{{out}}
<pre>
1: { 0 1 2 3 4 5 6 7 8 9 }
</pre>
This implementation is 40 times slower than the built-in <code>SORT</code> function on an HP-50g.
 
=={{header|Ruby}}==
<langsyntaxhighlight lang="ruby">class Array
def cocktailsort!
begin
Line 2,288 ⟶ 3,887:
self
end
end</langsyntaxhighlight>
 
Another way
<langsyntaxhighlight lang="ruby">class Array
def cocktailsort!
start, finish, way = 0, size-1, 1
Line 2,307 ⟶ 3,906:
self
end
end</langsyntaxhighlight>
 
Test:
<langsyntaxhighlight lang="ruby">ary = [7,6,5,9,8,4,3,1,2,0]
p ary.cocktailsort!
ary = ["John", "Kate", "Zerg", "Alice", "Joe", "Jane"]
p ary.cocktailsort!</langsyntaxhighlight>
 
{{out}}
Line 2,322 ⟶ 3,921:
 
=={{header|Run BASIC}}==
<langsyntaxhighlight lang="runbasic">for i = 1 to 100 ' fill array
a(i) = rnd(0) * 100
next i
Line 2,348 ⟶ 3,947:
print i;" ";a(i)
next i
end</langsyntaxhighlight>
 
=={{header|Rust}}==
<syntaxhighlight lang="rust">fn cocktail_sort<T: PartialOrd>(a: &mut [T]) {
let len = a.len();
loop {
let mut swapped = false;
let mut i = 0;
while i + 1 < len {
if a[i] > a[i + 1] {
a.swap(i, i + 1);
swapped = true;
}
i += 1;
}
if swapped {
swapped = false;
i = len - 1;
while i > 0 {
if a[i - 1] > a[i] {
a.swap(i - 1, i);
swapped = true;
}
i -= 1;
}
}
if !swapped {
break;
}
}
}
 
fn main() {
let mut v = vec![10, 8, 4, 3, 1, 9, 0, 2, 7, 5, 6];
println!("before: {:?}", v);
cocktail_sort(&mut v);
println!("after: {:?}", v);
}</syntaxhighlight>
 
{{out}}
<pre>
before: [10, 8, 4, 3, 1, 9, 0, 2, 7, 5, 6]
after: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
</pre>
 
=={{header|Scala}}==
<syntaxhighlight lang="scala">object CocktailSort extends App {
def sort(arr: Array[Int]) = {
var swapped = false
do {
def swap(i: Int) {
val temp = arr(i)
arr(i) = arr(i + 1)
arr(i + 1) = temp
swapped = true
}
 
swapped = false
for (i <- 0 to (arr.length - 2)) if (arr(i) > arr(i + 1)) swap(i)
 
if (swapped) {
swapped = false
for (j <- arr.length - 2 to 0 by -1) if (arr(j) > arr(j + 1)) swap(j)
//if no elements have been swapped, then the list is sorted
}
} while (swapped)
arr
}
 
println(sort(Array(170, 45, 75, -90, -802, 24, 2, 66)).mkString(", "))
 
}</syntaxhighlight>
 
=={{header|Scilab}}==
<syntaxhighlight lang="text">function varargout=cocktailSort(list_in)
swapped = %T;
while swapped
swapped = %F;
for i = [1:length(list_in)-1]
if list_in(i) > list_in(i+1) then
list_in([i i+1])=list_in([i+1 i]);
swapped = %T;
end
end
if ~swapped
break
end
swapped = %F;
for i = [length(list_in)-1:-1:1]
if list_in(i) > list_in(i+1)
list_in([i i+1]) = list_in([i+1 i])
swapped = %T;
end
end
end
varargout=list(list_in)
endfunction
 
disp(cocktailSort([6 3 7 8 5 1 2 4 9]));</syntaxhighlight>
{{out}}
<pre> 1. 2. 3. 4. 5. 6. 7. 8. 9.</pre>
 
=={{header|Seed7}}==
<langsyntaxhighlight lang="seed7">const proc: cocktailSort (inout array elemType: arr) is func
local
var boolean: swapped is FALSE;
Line 2,379 ⟶ 4,078:
end if;
until not swapped;
end func;</langsyntaxhighlight>
 
Original source: [http://seed7.sourceforge.net/algorith/sorting.htm#cocktailSort]
 
=={{header|Sidef}}==
<langsyntaxhighlight lang="ruby">func cocktailsort(a) {
var swapped = false;
func cmpsw(i) {
if (a[i] > a[i+1]) {
a[i, i+1] = a[i+1, i];
swapped = true;
}
};
var max = a.offset;end
do {
{ |i| cmpsw(i-1) } *<< ^max;
swapped.not! && break;
{ |i| cmpsw(max-i) } *<< 1..max;
} do while {(swapped};)
return a;
}</langsyntaxhighlight>
Test:
<langsyntaxhighlight lang="ruby">var numbers = [7,6,5,9,8,4,3,1,2,0];
say cocktailsort(numbers).dump;
 
var strs = ["John", "Kate", "Zerg", "Alice", "Joe", "Jane"];
say cocktailsort(strs).dump;</langsyntaxhighlight>
{{out}}
<pre>[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Line 2,411 ⟶ 4,110:
 
=={{header|Slate}}==
<langsyntaxhighlight lang="slate">s@(Sequence traits) cocktailSort
[ |swapped|
swapped: False.
Line 2,420 ⟶ 4,119:
swapped: False].
] loop
].</langsyntaxhighlight>
{{out|Example}}
<langsyntaxhighlight lang="slate">slate[1]> #( 10 9 8 7 6 0 -5) cocktailSort.
{-5. 0. 6. 7. 8. 9. 10}</langsyntaxhighlight>
 
=={{header|Smalltalk}}==
{{works with|GNU Smalltalk}}
<langsyntaxhighlight lang="smalltalk">OrderedCollection extend [
swap: indexA and: indexB [
|t|
Line 2,456 ⟶ 4,155:
^ self
]
].</langsyntaxhighlight>
{{out|Example}}
<langsyntaxhighlight lang="smalltalk">(#( 10 9 8 7 6 0 -5) asOrderedCollection cocktailSort) printNl.</langsyntaxhighlight>
 
=={{header|Swift}}==
 
<syntaxhighlight lang="swift">extension Collection where Element: Comparable {
public func cocktailSorted() -> [Element] {
var swapped = false
var ret = Array(self)
 
guard count > 1 else {
return ret
}
repeat {
for i in 0...ret.count-2 where ret[i] > ret[i + 1] {
(ret[i], ret[i + 1]) = (ret[i + 1], ret[i])
swapped = true
}
 
guard swapped else {
break
}
 
swapped = false
 
for i in stride(from: ret.count - 2, through: 0, by: -1) where ret[i] > ret[i + 1] {
(ret[i], ret[i + 1]) = (ret[i + 1], ret[i])
swapped = true
}
} while swapped
 
return ret
}
}
 
let arr = (1...10).shuffled()
 
print("Before: \(arr)")
print("Cocktail sort: \(arr.cocktailSorted())")</syntaxhighlight>
 
{{out}}
 
<pre>Before: [5, 4, 7, 10, 9, 8, 1, 6, 2, 3]
Cocktail sort: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]</pre>
 
=={{header|Tcl}}==
{{tcllib|struct::list}}<!-- convenient element swapping only -->
<langsyntaxhighlight lang="tcl">package require Tcl 8.5
package require struct::list
 
Line 2,490 ⟶ 4,232:
}
return $A
}</langsyntaxhighlight>
{{out|Example}}
<langsyntaxhighlight lang="tcl">puts [cocktailsort {8 6 4 2 1 3 5 7 9}] ;# => 1 2 3 4 5 6 7 8 9</langsyntaxhighlight>
 
=={{header|uBasic/4tH}}==
<syntaxhighlight lang="text">PRINT "Cocktail sort:"
n = FUNC (_InitArray)
PROC _ShowArray (n)
PROC _Cocktailsort (n)
PROC _ShowArray (n)
PRINT
END
 
 
_Cocktailsort PARAM (1) ' Cocktail sort
LOCAL (2)
b@ = 0
 
DO WHILE b@ = 0
b@ = 1
FOR c@=1 TO a@-1
IF @(c@) < @(c@-1) THEN PROC _Swap (c@, c@-1) : b@ = 0
NEXT
UNTIL b@
b@ = 1
FOR c@=a@-1 TO 1 STEP -1
IF @(c@) < @(c@-1) THEN PROC _Swap (c@, c@-1) : b@ = 0
NEXT
LOOP
RETURN
_Swap PARAM(2) ' Swap two array elements
PUSH @(a@)
@(a@) = @(b@)
@(b@) = POP()
RETURN
_InitArray ' Init example array
PUSH 4, 65, 2, -31, 0, 99, 2, 83, 782, 1
FOR i = 0 TO 9
@(i) = POP()
NEXT
RETURN (i)
_ShowArray PARAM (1) ' Show array subroutine
FOR i = 0 TO a@-1
PRINT @(i),
NEXT
PRINT
RETURN</syntaxhighlight>
 
=={{header|Ursala}}==
The same function is used for the traversal in each direction, in one case parameterized by the given predicate and in the other by its negation. Lists are used rather than arrays. The fold combinator (<code>=></code>) avoids explicit recursion.
<langsyntaxhighlight Ursalalang="ursala">#import std
 
ctsort = ^=+ +^|(==?/~&l+ @r+ ~&x++ @x,^/~&)+ ("p". =><> ~&r?\~&C "p"?lrhPX/~&C ~&rhPlrtPCC)^~/not ~&</langsyntaxhighlight>
test program:
<langsyntaxhighlight Ursalalang="ursala">#cast %s
 
test = ctsort(lleq) 'mdfdguldxisgbxjtqkadfkslakwkyioukdledbig'</langsyntaxhighlight>
{{out}}
<pre>
'aabbddddddeffgggiiijkkkkklllmoqsstuuwxxy'
</pre>
 
=={{header|Vala}}==
{{trans|C}}
<syntaxhighlight lang="vala">void swap(int[] array, int i1, int i2) {
if (array[i1] == array[i2])
return;
var tmp = array[i1];
array[i1] = array[i2];
array[i2] = tmp;
}
 
void cocktail_sort(int[] array) {
while(true) {
bool flag = false;
int[] start = {1, array.length - 1};
int[] end = {array.length, 0};
int[] inc = {1, -1};
for (int it = 0; it < 2; ++it) {
flag = true;
for (int i = start[it]; i != end[it]; i += inc[it])
if (array[i - 1] > array[i]) {
swap(array, i - 1, i);
flag = false;
}
}
if (flag) return;
}
}
 
void main() {
int[] array = {5, -1, 101, -4, 0, 1, 8, 6, 2, 3};
cocktail_sort(array);
foreach (int i in array) {
stdout.printf("%d ", i);
}
}</syntaxhighlight>
 
{{out}}
<pre>
-4 -1 0 1 2 3 5 6 8 101
</pre>
 
=={{header|VBA}}==
{{trans|Phix}}<syntaxhighlight lang="vb">Function cocktail_sort(ByVal s As Variant) As Variant
Dim swapped As Boolean
Dim f As Integer, t As Integer, d As Integer, tmp As Integer
swapped = True
f = 1
t = UBound(s) - 1
d = 1
Do While swapped
swapped = 0
For i = f To t Step d
If Val(s(i)) > Val(s(i + 1)) Then
tmp = s(i)
s(i) = s(i + 1)
s(i + 1) = tmp
swapped = True
End If
Next i
'-- swap to and from, and flip direction.
'-- additionally, we can reduce one element to be
'-- examined, depending on which way we just went.
tmp = f
f = t + (d = 1)
t = tmp + (d = -1)
d = -d
Loop
cocktail_sort = s
End Function
Public Sub main()
Dim s(9) As Variant
For i = 0 To 9
s(i) = CStr(Int(1000 * Rnd))
Next i
Debug.Print Join(s, ", ")
Debug.Print Join(cocktail_sort(s), ", ")
End Sub</syntaxhighlight>{{out}}
<pre>45, 414, 862, 790, 373, 961, 871, 56, 949, 364
45, 56, 364, 373, 414, 790, 862, 871, 949, 961</pre>
 
=={{header|VBScript}}==
Line 2,512 ⟶ 4,389:
 
;Implementation
<langsyntaxhighlight lang="vb">function cocktailSort( byval a )
dim swapped
dim i
Line 2,541 ⟶ 4,418:
a = b
b = tmp
end sub</langsyntaxhighlight>
;Invocation
<langsyntaxhighlight lang="vb">dim a
a = array( "portcullis", "redoubt", "palissade", "turret", "collins", "the parapet", "the quarterdeck")
wscript.echo join( a, ", ")
Line 2,549 ⟶ 4,426:
dim b
b = cocktailSort( a )
wscript.echo join( b, ", ")</langsyntaxhighlight>
{{out}}
<pre>
portcullis, redoubt, palissade, turret, collins, the parapet, the quarterdeck
collins, palissade, portcullis, redoubt, the parapet, the quarterdeck, turret
</pre>
 
=={{header|V (Vlang)}}==
<syntaxhighlight lang="v (vlang)">fn cocktail(mut arr []int) {
input := 'Input'
output := 'Output'
println('${input : -7s}: ${arr.str()}')
for {
mut swapped := false
for i := 0; i < arr.len - 1; i++ {
if arr[i] > arr[i + 1] {
temp := arr[i]
arr[i] = arr[i + 1]
arr[i + 1] = temp
swapped = true
}
}
if !swapped {
break
}
swapped = false
for i := arr.len - 2; i >= 0; i-- {
if arr[i] > arr[i + 1] {
temp := arr[i]
arr[i] = arr[i + 1]
arr[i + 1] = temp
swapped = true
}
}
if !swapped {
break
}
}
println('${output : -7s}: ${arr.str()}')
}
 
fn main() {
mut arr := [6, 9, 1, 4]
cocktail(mut arr)
arr = [-8, 15, 1, 4, -3, 20]
cocktail(mut arr)
}</syntaxhighlight>
{{out}}
<pre>Input : [6, 9, 1, 4]
Output : [1, 4, 6, 9]
Input : [-8, 15, 1, 4, -3, 20]
Output : [-8, -3, 1, 4, 15, 20]</pre>
 
=={{header|Wren}}==
{{trans|Go}}
<syntaxhighlight lang="wren">var cocktailSort = Fn.new { |a|
var last = a.count - 1
while (true) {
var swapped = false
for (i in 0...last) {
if (a[i] > a[i+1]) {
var t = a[i]
a[i] = a[i+1]
a[i+1] = t
swapped = true
}
}
if (!swapped) return
swapped = false
if (last >= 1) {
for (i in last-1..0) {
if (a[i] > a[i+1]) {
var t = a[i]
a[i] = a[i+1]
a[i+1] = t
swapped = true
}
}
}
if (!swapped) return
}
}
 
var a = [170, 45, 75, -90, -802, 24, 2, 66]
System.print("Before: %(a)")
cocktailSort.call(a)
System.print("After : %(a)")</syntaxhighlight>
 
{{out}}
<pre>
Before: [170, 45, 75, -90, -802, 24, 2, 66]
After : [-802, -90, 2, 24, 45, 66, 75, 170]
</pre>
 
=={{header|XPL0}}==
<langsyntaxhighlight XPL0lang="xpl0">code ChOut=8, IntOut=11;
 
proc CocktailSort(A, L); \Sort array A of length L
Line 2,584 ⟶ 4,548:
CocktailSort(A, 10);
for I:= 0 to 10-1 do [IntOut(0, A(I)); ChOut(0, ^ )];
]</langsyntaxhighlight>
 
{{out}}
Line 2,593 ⟶ 4,557:
=={{header|zkl}}==
This has the Wikipedia optimizations.
<langsyntaxhighlight lang="zkl">fcn cocktailSort(a){
swapped,begin,end:=False,-1,a.len() - 2;
do{
Line 2,607 ⟶ 4,571:
}while(swapped);
a
}</langsyntaxhighlight>
<langsyntaxhighlight lang="zkl">cocktailSort(List(2,1)).println();
x:=List(5, -1, 101, -4, 0, 1, 8, 6, 2, 3 );
cocktailSort(x).println();
x="the lazy fox jumped over the brown dog".split(" ").copy();
cocktailSort(x).println();</langsyntaxhighlight>
{{out}}
<pre>
Line 2,620 ⟶ 4,584:
</pre>
 
=={{header| ZX Spectrum Basic}}==
its a "cocktail" bubble sort, but the writer called it 'zigzag' since the name was unknown
<langsyntaxhighlight lang="zxbasic">5000 CLS
5002 LET a$="": FOR f=1 TO 64: LET a$=a$+CHR$ (32+INT (RND*96)): NEXT f
5004 PRINT a$; AT 10,0;"ZigZag BubbleSORT"
Line 2,642 ⟶ 4,606:
5064 IF d AND i<la THEN GO TO 5020
5072 PRINT AT 12,0;a$
9000 STOP</langsyntaxhighlight>
Next is an optimisation by using the margin value's as swop comparative aswell
so its swops inside and at the edges from the file
Line 2,648 ⟶ 4,612:
its a " Sticky (edge) Cocktail Sort"
By C. Born (freeware)
<langsyntaxhighlight lang="zxbasic">5000 CLS : PRINT ;"Jumping Zig Zag BubbleSORT"'"aka Sticky Cocktail Sort"
5002 LET a$="": FOR f=1 TO 96: LET a$=a$+CHR$ (48+INT (RND*48)): NEXT f
5004 PRINT 'a$
Line 2,680 ⟶ 4,644:
5510 IF i-1>=1 THEN IF a$(i)=a$(i-1) THEN LET i=i+1: GO TO 5500
5520 RETURN
9999 CLEAR : SAVE "JZZB" LINE 0</langsyntaxhighlight>
 
{{omit from|GUISS}}
1,150

edits