<s>
Cocktail	B-Algorithm
shaker	I-Algorithm
sort	I-Algorithm
,	O
also	O
known	O
as	O
bidirectional	B-Algorithm
bubble	I-Algorithm
sort	I-Algorithm
,	O
cocktail	B-Algorithm
sort	I-Algorithm
,	O
shaker	B-Algorithm
sort	I-Algorithm
(	O
which	O
can	O
also	O
refer	O
to	O
a	O
variant	O
of	O
selection	B-Algorithm
sort	I-Algorithm
)	O
,	O
ripple	B-Algorithm
sort	I-Algorithm
,	O
shuffle	B-Algorithm
sort	I-Algorithm
,	O
or	O
shuttle	B-Algorithm
sort	I-Algorithm
,	O
is	O
an	O
extension	O
of	O
bubble	B-Algorithm
sort	I-Algorithm
.	O
</s>
<s>
The	O
algorithm	O
extends	O
bubble	B-Algorithm
sort	I-Algorithm
by	O
operating	O
in	O
two	O
directions	O
.	O
</s>
<s>
While	O
it	O
improves	O
on	O
bubble	B-Algorithm
sort	I-Algorithm
by	O
more	O
quickly	O
moving	O
items	O
to	O
the	O
beginning	O
of	O
the	O
list	O
,	O
it	O
provides	O
only	O
marginal	O
performance	O
improvements	O
.	O
</s>
<s>
Like	O
most	O
variants	O
of	O
bubble	B-Algorithm
sort	I-Algorithm
,	O
cocktail	B-Algorithm
shaker	I-Algorithm
sort	I-Algorithm
is	O
used	O
primarily	O
as	O
an	O
educational	O
tool	O
.	O
</s>
<s>
More	O
performant	O
algorithms	O
such	O
as	O
quicksort	B-Algorithm
,	O
merge	B-Algorithm
sort	I-Algorithm
,	O
or	O
timsort	B-Algorithm
are	O
used	O
by	O
the	O
sorting	B-Algorithm
libraries	O
built	O
into	O
popular	O
programming	O
languages	O
such	O
as	O
Python	O
and	O
Java	O
.	O
</s>
<s>
By	O
shortening	O
the	O
part	O
of	O
the	O
list	O
that	O
is	O
sorted	O
each	O
time	O
,	O
the	O
number	O
of	O
operations	O
can	O
be	O
halved	O
(	O
see	O
bubble	B-Algorithm
sort	I-Algorithm
)	O
.	O
</s>
<s>
Cocktail	B-Algorithm
shaker	I-Algorithm
sort	I-Algorithm
is	O
a	O
slight	O
variation	O
of	O
bubble	B-Algorithm
sort	I-Algorithm
.	O
</s>
<s>
It	O
can	O
achieve	O
slightly	O
better	O
performance	O
than	O
a	O
standard	O
bubble	B-Algorithm
sort	I-Algorithm
.	O
</s>
<s>
The	O
reason	O
for	O
this	O
is	O
that	O
bubble	B-Algorithm
sort	I-Algorithm
only	O
passes	O
through	O
the	O
list	O
in	O
one	O
direction	O
and	O
therefore	O
can	O
only	O
move	O
items	O
backward	O
one	O
step	O
each	O
iteration	O
.	O
</s>
<s>
An	O
example	O
of	O
a	O
list	O
that	O
proves	O
this	O
point	O
is	O
the	O
list	O
(	O
2	O
,	O
3	O
,	O
4	O
,	O
5	O
,	O
1	O
)	O
,	O
which	O
would	O
only	O
need	O
to	O
go	O
through	O
one	O
pass	O
of	O
cocktail	B-Algorithm
sort	I-Algorithm
to	O
become	O
sorted	O
,	O
but	O
if	O
using	O
an	O
ascending	O
bubble	B-Algorithm
sort	I-Algorithm
would	O
take	O
four	O
passes	O
.	O
</s>
<s>
However	O
one	O
cocktail	B-Algorithm
sort	I-Algorithm
pass	O
should	O
be	O
counted	O
as	O
two	O
bubble	B-Algorithm
sort	I-Algorithm
passes	O
.	O
</s>
<s>
Typically	O
cocktail	B-Algorithm
sort	I-Algorithm
is	O
less	O
than	O
two	O
times	O
faster	O
than	O
bubble	B-Algorithm
sort	I-Algorithm
.	O
</s>
<s>
As	O
the	O
cocktail	B-Algorithm
shaker	I-Algorithm
sort	I-Algorithm
goes	O
bidirectionally	O
,	O
the	O
range	O
of	O
possible	O
swaps	O
,	O
which	O
is	O
the	O
range	O
to	O
be	O
tested	O
,	O
will	O
reduce	O
per	O
pass	O
,	O
thus	O
reducing	O
the	O
overall	O
running	O
time	O
slightly	O
.	O
</s>
<s>
The	O
complexity	O
of	O
the	O
cocktail	B-Algorithm
shaker	I-Algorithm
sort	I-Algorithm
in	O
big	O
O	O
notation	O
is	O
for	O
both	O
the	O
worst	O
case	O
and	O
the	O
average	O
case	O
,	O
but	O
it	O
becomes	O
closer	O
to	O
if	O
the	O
list	O
is	O
mostly	O
ordered	O
before	O
applying	O
the	O
sorting	B-Algorithm
algorithm	I-Algorithm
.	O
</s>
<s>
The	O
cocktail	B-Algorithm
shaker	I-Algorithm
sort	I-Algorithm
is	O
also	O
briefly	O
discussed	O
in	O
the	O
book	O
The	B-General_Concept
Art	I-General_Concept
of	I-General_Concept
Computer	I-General_Concept
Programming	I-General_Concept
,	O
along	O
with	O
similar	O
refinements	O
of	O
bubble	B-Algorithm
sort	I-Algorithm
.	O
</s>
<s>
In	O
conclusion	O
,	O
Knuth	O
states	O
about	O
bubble	B-Algorithm
sort	I-Algorithm
and	O
its	O
improvements	O
:	O
</s>
