<s>
Bubble	B-Algorithm
sort	I-Algorithm
,	O
sometimes	O
referred	O
to	O
as	O
sinking	B-Algorithm
sort	I-Algorithm
,	O
is	O
a	O
simple	O
sorting	B-Algorithm
algorithm	I-Algorithm
that	O
repeatedly	O
steps	O
through	O
the	O
input	O
list	O
element	O
by	O
element	O
,	O
comparing	O
the	O
current	O
element	O
with	O
the	O
one	O
after	O
it	O
,	O
swapping	O
their	O
values	O
if	O
needed	O
.	O
</s>
<s>
The	O
algorithm	O
,	O
which	O
is	O
a	O
comparison	B-Algorithm
sort	I-Algorithm
,	O
is	O
named	O
for	O
the	O
way	O
the	O
larger	O
elements	O
"	O
bubble	O
"	O
up	O
to	O
the	O
top	O
of	O
the	O
list	O
.	O
</s>
<s>
More	O
efficient	O
algorithms	O
such	O
as	O
quicksort	B-Algorithm
,	O
timsort	B-Algorithm
,	O
or	O
merge	B-Algorithm
sort	I-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	B-Language
.	O
</s>
<s>
Bubble	B-Algorithm
sort	I-Algorithm
has	O
a	O
worst-case	O
and	O
average	O
complexity	O
of	O
,	O
where	O
is	O
the	O
number	O
of	O
items	O
being	O
sorted	O
.	O
</s>
<s>
Most	O
practical	O
sorting	B-Algorithm
algorithms	I-Algorithm
have	O
substantially	O
better	O
worst-case	O
or	O
average	O
complexity	O
,	O
often	O
.	O
</s>
<s>
Even	O
other	O
sorting	B-Algorithm
algorithms	I-Algorithm
,	O
such	O
as	O
insertion	B-Algorithm
sort	I-Algorithm
,	O
generally	O
run	O
faster	O
than	O
bubble	B-Algorithm
sort	I-Algorithm
,	O
and	O
are	O
no	O
more	O
complex	O
.	O
</s>
<s>
For	O
this	O
reason	O
,	O
bubble	B-Algorithm
sort	I-Algorithm
is	O
rarely	O
used	O
in	O
practice	O
.	O
</s>
<s>
Like	O
insertion	B-Algorithm
sort	I-Algorithm
,	O
bubble	B-Algorithm
sort	I-Algorithm
is	O
adaptive	B-Algorithm
,	O
giving	O
it	O
an	O
advantage	O
over	O
algorithms	O
like	O
quicksort	B-Algorithm
.	O
</s>
<s>
This	O
means	O
that	O
it	O
may	O
outperform	O
those	O
algorithms	O
in	O
cases	O
where	O
the	O
list	O
is	O
already	O
mostly	O
sorted	O
(	O
having	O
a	O
small	O
number	O
of	O
inversions	B-Algorithm
)	O
,	O
despite	O
the	O
fact	O
that	O
it	O
has	O
worse	O
average-case	O
time	O
complexity	O
.	O
</s>
<s>
For	O
example	O
,	O
bubble	B-Algorithm
sort	I-Algorithm
is	O
on	O
a	O
list	O
that	O
is	O
already	O
sorted	O
,	O
while	O
quicksort	B-Algorithm
would	O
still	O
perform	O
its	O
entire	O
sorting	B-Algorithm
process	O
.	O
</s>
<s>
While	O
any	O
sorting	B-Algorithm
algorithm	I-Algorithm
can	O
be	O
made	O
on	O
a	O
presorted	O
list	O
simply	O
by	O
checking	O
the	O
list	O
before	O
the	O
algorithm	O
runs	O
,	O
improved	O
performance	O
on	O
almost-sorted	O
lists	O
is	O
harder	O
to	O
replicate	O
.	O
</s>
<s>
The	O
distance	O
and	O
direction	O
that	O
elements	O
must	O
move	O
during	O
the	O
sort	O
determine	O
bubble	B-Algorithm
sort	I-Algorithm
's	O
performance	O
because	O
elements	O
move	O
in	O
different	O
directions	O
at	O
different	O
speeds	O
.	O
</s>
<s>
Various	O
efforts	O
have	O
been	O
made	O
to	O
eliminate	O
turtles	O
to	O
improve	O
upon	O
the	O
speed	O
of	O
bubble	B-Algorithm
sort	I-Algorithm
.	O
</s>
<s>
Cocktail	B-Algorithm
sort	I-Algorithm
is	O
a	O
bi-directional	O
bubble	B-Algorithm
sort	I-Algorithm
that	O
goes	O
from	O
beginning	O
to	O
end	O
,	O
and	O
then	O
reverses	O
itself	O
,	O
going	O
end	O
to	O
beginning	O
.	O
</s>
<s>
Comb	B-Algorithm
sort	I-Algorithm
compares	O
elements	O
separated	O
by	O
large	O
gaps	O
,	O
and	O
can	O
move	O
turtles	O
extremely	O
quickly	O
before	O
proceeding	O
to	O
smaller	O
and	O
smaller	O
gaps	O
to	O
smooth	O
out	O
the	O
list	O
.	O
</s>
<s>
Its	O
average	O
speed	O
is	O
comparable	O
to	O
faster	O
algorithms	O
like	O
quicksort	B-Algorithm
.	O
</s>
<s>
Take	O
an	O
array	B-Data_Structure
of	O
numbers	O
"	O
5	O
1	O
4	O
2	O
8	O
"	O
,	O
and	O
sort	O
the	O
array	B-Data_Structure
from	O
lowest	O
number	O
to	O
greatest	O
number	O
using	O
bubble	B-Algorithm
sort	I-Algorithm
.	O
</s>
<s>
Now	O
,	O
the	O
array	B-Data_Structure
is	O
already	O
sorted	O
,	O
but	O
the	O
algorithm	O
does	O
not	O
know	O
if	O
it	O
is	O
completed	O
.	O
</s>
<s>
In	O
pseudocode	B-Language
the	O
algorithm	O
can	O
be	O
expressed	O
as	O
(	O
0-based	O
array	B-Data_Structure
)	O
:	O
</s>
<s>
The	O
bubble	B-Algorithm
sort	I-Algorithm
algorithm	I-Algorithm
can	O
be	O
optimized	O
by	O
observing	O
that	O
the	O
n-th	O
pass	O
finds	O
the	O
n-th	O
largest	O
element	O
and	O
puts	O
it	O
into	O
its	O
final	O
place	O
.	O
</s>
<s>
To	O
accomplish	O
this	O
in	O
pseudocode	B-Language
,	O
the	O
following	O
can	O
be	O
written	O
:	O
</s>
<s>
Alternate	O
modifications	O
,	O
such	O
as	O
the	O
cocktail	B-Algorithm
shaker	I-Algorithm
sort	I-Algorithm
attempt	O
to	O
improve	O
on	O
the	O
bubble	B-Algorithm
sort	I-Algorithm
performance	O
while	O
keeping	O
the	O
same	O
idea	O
of	O
repeatedly	O
comparing	O
and	O
swapping	O
adjacent	O
items	O
.	O
</s>
<s>
Although	O
bubble	B-Algorithm
sort	I-Algorithm
is	O
one	O
of	O
the	O
simplest	O
sorting	B-Algorithm
algorithms	I-Algorithm
to	O
understand	O
and	O
implement	O
,	O
its	O
O(n2 )	O
complexity	O
means	O
that	O
its	O
efficiency	O
decreases	O
dramatically	O
on	O
lists	O
of	O
more	O
than	O
a	O
small	O
number	O
of	O
elements	O
.	O
</s>
<s>
Even	O
among	O
simple	O
O(n2 )	O
sorting	B-Algorithm
algorithms	I-Algorithm
,	O
algorithms	O
like	O
insertion	B-Algorithm
sort	I-Algorithm
are	O
usually	O
considerably	O
more	O
efficient	O
.	O
</s>
<s>
Due	O
to	O
its	O
simplicity	O
,	O
bubble	B-Algorithm
sort	I-Algorithm
is	O
often	O
used	O
to	O
introduce	O
the	O
concept	O
of	O
an	O
algorithm	O
,	O
or	O
a	O
sorting	B-Algorithm
algorithm	I-Algorithm
,	O
to	O
introductory	O
computer	B-General_Concept
science	I-General_Concept
students	O
.	O
</s>
<s>
However	O
,	O
some	O
researchers	O
such	O
as	O
Owen	O
Astrachan	O
have	O
gone	O
to	O
great	O
lengths	O
to	O
disparage	O
bubble	B-Algorithm
sort	I-Algorithm
and	O
its	O
continued	O
popularity	O
in	O
computer	B-General_Concept
science	I-General_Concept
education	O
,	O
recommending	O
that	O
it	O
no	O
longer	O
even	O
be	O
taught	O
.	O
</s>
<s>
The	O
Jargon	O
File	O
,	O
which	O
famously	O
calls	O
bogosort	O
"	O
the	O
archetypical	O
 [ sic ] 	O
perversely	O
awful	O
algorithm	O
"	O
,	O
also	O
calls	O
bubble	B-Algorithm
sort	I-Algorithm
"	O
the	O
generic	O
bad	O
algorithm	O
"	O
.	O
</s>
<s>
Donald	O
Knuth	O
,	O
in	O
The	B-General_Concept
Art	I-General_Concept
of	I-General_Concept
Computer	I-General_Concept
Programming	I-General_Concept
,	O
concluded	O
that	O
"	O
the	O
bubble	B-Algorithm
sort	I-Algorithm
seems	O
to	O
have	O
nothing	O
to	O
recommend	O
it	O
,	O
except	O
a	O
catchy	O
name	O
and	O
the	O
fact	O
that	O
it	O
leads	O
to	O
some	O
interesting	O
theoretical	O
problems	O
"	O
,	O
some	O
of	O
which	O
he	O
then	O
discusses	O
.	O
</s>
<s>
Bubble	B-Algorithm
sort	I-Algorithm
is	O
asymptotically	O
equivalent	O
in	O
running	O
time	O
to	O
insertion	B-Algorithm
sort	I-Algorithm
in	O
the	O
worst	O
case	O
,	O
but	O
the	O
two	O
algorithms	O
differ	O
greatly	O
in	O
the	O
number	O
of	O
swaps	O
necessary	O
.	O
</s>
<s>
Experimental	O
results	O
such	O
as	O
those	O
of	O
Astrachan	O
have	O
also	O
shown	O
that	O
insertion	B-Algorithm
sort	I-Algorithm
performs	O
considerably	O
better	O
even	O
on	O
random	O
lists	O
.	O
</s>
<s>
For	O
these	O
reasons	O
many	O
modern	O
algorithm	O
textbooks	O
avoid	O
using	O
the	O
bubble	B-Algorithm
sort	I-Algorithm
algorithm	I-Algorithm
in	O
favor	O
of	O
insertion	B-Algorithm
sort	I-Algorithm
.	O
</s>
<s>
Bubble	B-Algorithm
sort	I-Algorithm
also	O
interacts	O
poorly	O
with	O
modern	O
CPU	O
hardware	O
.	O
</s>
<s>
It	O
produces	O
at	O
least	O
twice	O
as	O
many	O
writes	O
as	O
insertion	B-Algorithm
sort	I-Algorithm
,	O
twice	O
as	O
many	O
cache	O
misses	O
,	O
and	O
asymptotically	O
more	O
branch	B-General_Concept
mispredictions	I-General_Concept
.	O
</s>
<s>
Experiments	O
by	O
Astrachan	O
sorting	B-Algorithm
strings	O
in	O
Java	B-Language
show	O
bubble	B-Algorithm
sort	I-Algorithm
to	O
be	O
roughly	O
one-fifth	O
as	O
fast	O
as	O
an	O
insertion	B-Algorithm
sort	I-Algorithm
and	O
70%	O
as	O
fast	O
as	O
a	O
selection	B-Algorithm
sort	I-Algorithm
.	O
</s>
<s>
In	O
computer	O
graphics	O
bubble	B-Algorithm
sort	I-Algorithm
is	O
popular	O
for	O
its	O
capability	O
to	O
detect	O
a	O
very	O
small	O
error	O
(	O
like	O
swap	O
of	O
just	O
two	O
elements	O
)	O
in	O
almost-sorted	O
arrays	O
and	O
fix	O
it	O
with	O
just	O
linear	O
complexity	O
(	O
2n	O
)	O
.	O
</s>
<s>
Bubble	B-Algorithm
sort	I-Algorithm
is	O
a	O
stable	O
sort	B-Algorithm
algorithm	I-Algorithm
,	O
like	O
insertion	B-Algorithm
sort	I-Algorithm
.	O
</s>
<s>
Odd	B-Algorithm
–	I-Algorithm
even	I-Algorithm
sort	I-Algorithm
is	O
a	O
parallel	O
version	O
of	O
bubble	B-Algorithm
sort	I-Algorithm
,	O
for	O
message	O
passing	O
systems	O
.	O
</s>
<s>
Cocktail	B-Algorithm
shaker	I-Algorithm
sort	I-Algorithm
alternates	O
leftwards	O
and	O
rightwards	O
passes	O
.	O
</s>
<s>
I	O
ca	O
n't	O
believe	O
it	O
can	O
sort	O
is	O
a	O
sorting	B-Algorithm
algorithm	I-Algorithm
that	O
appears	O
to	O
be	O
an	O
incorrect	O
version	O
of	O
bubble	B-Algorithm
sort	I-Algorithm
,	O
but	O
can	O
be	O
formally	O
proven	O
to	O
work	O
in	O
a	O
way	O
more	O
akin	O
to	O
insertion	B-Algorithm
sort	I-Algorithm
.	O
</s>
<s>
Bubble	B-Algorithm
sort	I-Algorithm
has	O
been	O
occasionally	O
referred	O
to	O
as	O
a	O
"	O
sinking	B-Algorithm
sort	I-Algorithm
"	O
.	O
</s>
<s>
For	O
example	O
,	O
Donald	O
Knuth	O
describes	O
the	O
insertion	O
of	O
values	O
at	O
or	O
towards	O
their	O
desired	O
location	O
as	O
letting	O
"	O
[	O
the	O
value ]	O
settle	O
to	O
its	O
proper	O
level	O
"	O
,	O
and	O
that	O
"	O
this	O
method	O
of	O
sorting	B-Algorithm
has	O
sometimes	O
been	O
called	O
the	O
sifting	O
or	O
sinking	O
technique	O
.	O
</s>
<s>
In	O
2007	O
,	O
former	O
Google	B-Application
CEO	O
Eric	O
Schmidt	O
asked	O
then-presidential	O
candidate	O
Barack	O
Obama	O
during	O
an	O
interview	O
about	O
the	O
best	O
way	O
to	O
sort	O
one	O
million	O
integers	O
;	O
Obama	O
paused	O
for	O
a	O
moment	O
and	O
replied	O
:	O
"	O
I	O
think	O
the	O
bubble	B-Algorithm
sort	I-Algorithm
would	O
be	O
the	O
wrong	O
way	O
to	O
go.	O
"	O
</s>
