<s>
In	O
computer	B-General_Concept
science	I-General_Concept
,	O
a	O
sorting	B-Algorithm
algorithm	I-Algorithm
is	O
an	O
algorithm	O
that	O
puts	O
elements	O
of	O
a	O
list	O
into	O
an	O
order	O
.	O
</s>
<s>
Efficient	O
sorting	B-Algorithm
is	O
important	O
for	O
optimizing	O
the	O
efficiency	B-General_Concept
of	O
other	O
algorithms	O
(	O
such	O
as	O
search	B-Application
and	O
merge	B-Algorithm
algorithms	I-Algorithm
)	O
that	O
require	O
input	O
data	O
to	O
be	O
in	O
sorted	B-Algorithm
lists	I-Algorithm
.	O
</s>
<s>
Sorting	B-Algorithm
is	O
also	O
often	O
useful	O
for	O
canonicalizing	B-General_Concept
data	O
and	O
for	O
producing	O
human-readable	O
output	O
.	O
</s>
<s>
Formally	O
,	O
the	O
output	O
of	O
any	O
sorting	B-Algorithm
algorithm	I-Algorithm
must	O
satisfy	O
two	O
conditions	O
:	O
</s>
<s>
The	O
output	O
is	O
a	O
permutation	B-Algorithm
(	O
a	O
reordering	O
,	O
yet	O
retaining	O
all	O
of	O
the	O
original	O
elements	O
)	O
of	O
the	O
input	O
.	O
</s>
<s>
For	O
optimum	O
efficiency	B-General_Concept
,	O
the	O
input	O
data	O
should	O
be	O
stored	O
in	O
a	O
data	B-General_Concept
structure	I-General_Concept
which	O
allows	O
random	B-General_Concept
access	I-General_Concept
rather	O
than	O
one	O
that	O
allows	O
only	O
sequential	B-General_Concept
access	I-General_Concept
.	O
</s>
<s>
From	O
the	O
beginning	O
of	O
computing	O
,	O
the	O
sorting	B-Algorithm
problem	I-Algorithm
has	O
attracted	O
a	O
great	O
deal	O
of	O
research	O
,	O
perhaps	O
due	O
to	O
the	O
complexity	O
of	O
solving	O
it	O
efficiently	O
despite	O
its	O
simple	O
,	O
familiar	O
statement	O
.	O
</s>
<s>
Among	O
the	O
authors	O
of	O
early	O
sorting	B-Algorithm
algorithms	I-Algorithm
around	O
1951	O
was	O
Betty	O
Holberton	O
,	O
who	O
worked	O
on	O
ENIAC	B-Device
and	O
UNIVAC	O
.	O
</s>
<s>
Bubble	B-Algorithm
sort	I-Algorithm
was	O
analyzed	O
as	O
early	O
as	O
1956	O
.	O
</s>
<s>
Asymptotically	O
optimal	O
algorithms	O
have	O
been	O
known	O
since	O
the	O
mid-20th	O
century	O
new	O
algorithms	O
are	O
still	O
being	O
invented	O
,	O
with	O
the	O
widely	O
used	O
Timsort	B-Algorithm
dating	O
to	O
2002	O
,	O
and	O
the	O
library	B-Algorithm
sort	I-Algorithm
being	O
first	O
published	O
in	O
2006	O
.	O
</s>
<s>
Comparison	B-Algorithm
sorting	I-Algorithm
algorithms	O
have	O
a	O
fundamental	O
requirement	O
of	O
Ω(n log n )	O
comparisons	O
(	O
some	O
input	O
sequences	O
will	O
require	O
a	O
multiple	O
of	O
n	O
log	O
n	O
comparisons	O
,	O
where	O
n	O
is	O
the	O
number	O
of	O
elements	O
in	O
the	O
array	O
to	O
be	O
sorted	O
)	O
.	O
</s>
<s>
Algorithms	O
not	O
based	O
on	O
comparisons	O
,	O
such	O
as	O
counting	B-Algorithm
sort	I-Algorithm
,	O
can	O
have	O
better	O
performance	O
.	O
</s>
<s>
Sorting	B-Algorithm
algorithms	I-Algorithm
are	O
prevalent	O
in	O
introductory	O
computer	B-General_Concept
science	I-General_Concept
classes	O
,	O
where	O
the	O
abundance	O
of	O
algorithms	O
for	O
the	O
problem	O
provides	O
a	O
gentle	O
introduction	O
to	O
a	O
variety	O
of	O
core	O
algorithm	O
concepts	O
,	O
such	O
as	O
big	O
O	O
notation	O
,	O
divide-and-conquer	B-Algorithm
algorithms	I-Algorithm
,	O
data	B-General_Concept
structures	I-General_Concept
such	O
as	O
heaps	B-Application
and	O
binary	O
trees	O
,	O
randomized	B-General_Concept
algorithms	I-General_Concept
,	O
best	B-General_Concept
,	I-General_Concept
worst	I-General_Concept
and	I-General_Concept
average	I-General_Concept
case	I-General_Concept
analysis	O
,	O
time	O
–	O
space	O
tradeoffs	O
,	O
and	O
upper	O
and	O
lower	O
bounds	O
.	O
</s>
<s>
Sorting	B-Algorithm
small	O
arrays	O
optimally	O
(	O
in	O
fewest	O
comparisons	O
and	O
swaps	O
)	O
or	O
fast	O
(	O
i.e.	O
</s>
<s>
Similarly	O
optimal	O
(	O
by	O
various	O
definitions	O
)	O
sorting	B-Algorithm
on	O
a	O
parallel	O
machine	O
is	O
an	O
open	O
research	O
topic	O
.	O
</s>
<s>
Sorting	B-Algorithm
algorithms	I-Algorithm
can	O
be	O
classified	O
by	O
:	O
</s>
<s>
Best	B-General_Concept
,	I-General_Concept
worst	I-General_Concept
and	I-General_Concept
average	I-General_Concept
case	I-General_Concept
behavior	O
in	O
terms	O
of	O
the	O
size	O
of	O
the	O
list	O
.	O
</s>
<s>
For	O
typical	O
serial	O
sorting	B-Algorithm
algorithms	I-Algorithm
,	O
good	O
behavior	O
is	O
O(nlogn )	O
,	O
with	O
parallel	O
sort	O
in	O
O(log2n )	O
,	O
and	O
bad	O
behavior	O
is	O
O(n2 )	O
.	O
</s>
<s>
Ideal	O
behavior	O
for	O
a	O
serial	O
sort	O
is	O
O(n )	O
,	O
but	O
this	O
is	O
not	O
possible	O
in	O
the	O
average	B-General_Concept
case	I-General_Concept
.	O
</s>
<s>
Optimal	O
parallel	O
sorting	B-Algorithm
is	O
O(logn )	O
.	O
</s>
<s>
Swaps	O
for	O
"	O
in-place	B-Algorithm
"	O
algorithms	O
.	O
</s>
<s>
Memory	B-General_Concept
usage	O
(	O
and	O
use	O
of	O
other	O
computer	O
resources	O
)	O
.	O
</s>
<s>
In	O
particular	O
,	O
some	O
sorting	B-Algorithm
algorithms	I-Algorithm
are	O
"	O
in-place	B-Algorithm
"	O
.	O
</s>
<s>
Strictly	O
,	O
an	O
in-place	B-Algorithm
sort	I-Algorithm
needs	O
only	O
O(1 )	O
memory	B-General_Concept
beyond	O
the	O
items	O
being	O
sorted	O
;	O
sometimes	O
O(logn )	O
additional	O
memory	B-General_Concept
is	O
considered	O
"	O
in-place	B-Algorithm
"	O
.	O
</s>
<s>
Recursion	O
:	O
Some	O
algorithms	O
are	O
either	O
recursive	O
or	O
non-recursive	O
,	O
while	O
others	O
may	O
be	O
both	O
(	O
e.g.	O
,	O
merge	B-Algorithm
sort	I-Algorithm
)	O
.	O
</s>
<s>
Stability	O
:	O
stable	O
sorting	B-Algorithm
algorithms	I-Algorithm
maintain	O
the	O
relative	O
order	O
of	O
records	O
with	O
equal	O
keys	O
(	O
i.e.	O
,	O
values	O
)	O
.	O
</s>
<s>
Whether	O
or	O
not	O
they	O
are	O
a	O
comparison	B-Algorithm
sort	I-Algorithm
.	O
</s>
<s>
A	O
comparison	B-Algorithm
sort	I-Algorithm
examines	O
the	O
data	O
only	O
by	O
comparing	O
two	O
elements	O
with	O
a	O
comparison	O
operator	O
.	O
</s>
<s>
General	O
method	O
:	O
insertion	O
,	O
exchange	O
,	O
selection	B-Algorithm
,	O
merging	O
,	O
etc	O
.	O
</s>
<s>
Exchange	B-Algorithm
sorts	I-Algorithm
include	O
bubble	B-Algorithm
sort	I-Algorithm
and	O
quicksort	B-Algorithm
.	O
</s>
<s>
Selection	B-Algorithm
sorts	I-Algorithm
include	O
cycle	B-Language
sort	I-Language
and	O
heapsort	B-Application
.	O
</s>
<s>
Algorithms	O
that	O
take	O
this	O
into	O
account	O
are	O
known	O
to	O
be	O
adaptive	B-Algorithm
.	O
</s>
<s>
Online	O
:	O
An	O
algorithm	O
such	O
as	O
Insertion	B-Algorithm
Sort	I-Algorithm
that	O
is	O
online	O
can	O
sort	O
a	O
constant	O
stream	O
of	O
input	O
.	O
</s>
<s>
Stable	O
sort	B-Algorithm
algorithms	I-Algorithm
sort	O
equal	O
elements	O
in	O
the	O
same	O
order	O
that	O
they	O
appear	O
in	O
the	O
input	O
.	O
</s>
<s>
For	O
example	O
,	O
in	O
the	O
card	O
sorting	B-Algorithm
example	O
to	O
the	O
right	O
,	O
the	O
cards	O
are	O
being	O
sorted	O
by	O
their	O
rank	O
,	O
and	O
their	O
suit	O
is	O
being	O
ignored	O
.	O
</s>
<s>
Stable	O
sorting	B-Algorithm
algorithms	I-Algorithm
choose	O
one	O
of	O
these	O
,	O
according	O
to	O
the	O
following	O
rule	O
:	O
if	O
two	O
items	O
compare	O
as	O
equal	O
(	O
like	O
the	O
two	O
5	O
cards	O
)	O
,	O
then	O
their	O
relative	O
order	O
will	O
be	O
preserved	O
,	O
i.e.	O
</s>
<s>
Stability	O
is	O
important	O
to	O
preserve	O
order	O
over	O
multiple	O
sorts	O
on	O
the	O
same	O
data	B-General_Concept
set	I-General_Concept
.	O
</s>
<s>
If	O
a	O
stable	O
sorting	B-Algorithm
algorithm	I-Algorithm
is	O
used	O
in	O
both	O
cases	O
,	O
the	O
sort-by-class-section	O
operation	O
will	O
not	O
change	O
the	O
name	O
order	O
;	O
with	O
an	O
unstable	O
sort	O
,	O
it	O
could	O
be	O
that	O
sorting	B-Algorithm
by	O
section	O
shuffles	O
the	O
name	O
order	O
,	O
resulting	O
in	O
a	O
nonalphabetical	O
list	O
of	O
students	O
.	O
</s>
<s>
More	O
formally	O
,	O
the	O
data	O
being	O
sorted	O
can	O
be	O
represented	O
as	O
a	O
record	O
or	O
tuple	O
of	O
values	O
,	O
and	O
the	O
part	O
of	O
the	O
data	O
that	O
is	O
used	O
for	O
sorting	B-Algorithm
is	O
called	O
the	O
key	O
.	O
</s>
<s>
A	O
sorting	B-Algorithm
algorithm	I-Algorithm
is	O
stable	O
if	O
whenever	O
there	O
are	O
two	O
records	O
R	O
and	O
S	O
with	O
the	O
same	O
key	O
,	O
and	O
R	O
appears	O
before	O
S	O
in	O
the	O
original	O
list	O
,	O
then	O
R	O
will	O
always	O
appear	O
before	O
S	O
in	O
the	O
sorted	B-Algorithm
list	I-Algorithm
.	O
</s>
<s>
Unstable	O
sorting	B-Algorithm
algorithms	I-Algorithm
can	O
be	O
specially	O
implemented	O
to	O
be	O
stable	O
.	O
</s>
<s>
One	O
application	O
for	O
stable	O
sorting	B-Algorithm
algorithms	I-Algorithm
is	O
sorting	B-Algorithm
a	O
list	O
using	O
a	O
primary	O
and	O
secondary	O
key	O
.	O
</s>
<s>
This	O
can	O
be	O
done	O
by	O
first	O
sorting	B-Algorithm
the	O
cards	O
by	O
rank	O
(	O
using	O
any	O
sort	O
)	O
,	O
and	O
then	O
doing	O
a	O
stable	O
sort	O
by	O
suit	O
:	O
</s>
<s>
This	O
idea	O
can	O
be	O
extended	O
to	O
any	O
number	O
of	O
keys	O
and	O
is	O
utilised	O
by	O
radix	B-Algorithm
sort	I-Algorithm
.	O
</s>
<s>
"	O
Memory	B-General_Concept
"	O
denotes	O
the	O
amount	O
of	O
extra	O
storage	O
needed	O
additionally	O
to	O
that	O
used	O
by	O
the	O
list	O
itself	O
,	O
under	O
the	O
same	O
assumption	O
.	O
</s>
<s>
The	O
run	O
times	O
and	O
the	O
memory	B-General_Concept
requirements	O
listed	O
are	O
inside	O
big	O
O	O
notation	O
,	O
hence	O
the	O
base	O
of	O
the	O
logarithms	O
does	O
not	O
matter	O
.	O
</s>
<s>
Below	O
is	O
a	O
table	O
of	O
comparison	B-Algorithm
sorts	I-Algorithm
.	O
</s>
<s>
A	O
comparison	B-Algorithm
sort	I-Algorithm
cannot	O
perform	O
better	O
than	O
on	O
average	O
.	O
</s>
<s>
+	O
Comparison	B-Algorithm
sorts	I-Algorithm
Name	O
Best	O
Average	O
Worst	O
Memory	B-General_Concept
Stable	O
Method	O
Other	O
notes	O
Quicksort	B-Algorithm
No	O
Partitioning	O
Quicksort	B-Algorithm
is	O
usually	O
done	O
in-place	B-Algorithm
with	O
stack	O
space	O
.	O
</s>
<s>
Merge	B-Algorithm
sort	I-Algorithm
Yes	O
Merging	O
Highly	O
parallelizable	O
(	O
up	O
to	O
using	O
the	O
Three	O
Hungarians	O
 '	O
Algorithm	O
)	O
.	O
</s>
<s>
In-place	B-Algorithm
merge	B-Algorithm
sort	I-Algorithm
—	O
—	O
Yes	O
Merging	O
Can	O
be	O
implemented	O
as	O
a	O
stable	O
sort	O
based	O
on	O
stable	O
in-place	B-Algorithm
merging	O
.	O
</s>
<s>
Introsort	O
No	O
Partitioning	O
&	O
Selection	B-Algorithm
Used	O
in	O
several	O
STL	B-Application
implementations	O
.	O
</s>
<s>
Heapsort	B-Application
No	O
Selection	B-Algorithm
Insertion	B-Algorithm
sort	I-Algorithm
Yes	O
Insertion	O
,	O
in	O
the	O
worst	B-General_Concept
case	I-General_Concept
over	O
sequences	O
that	O
have	O
d	O
inversions	B-Algorithm
.	O
</s>
<s>
Block	B-Algorithm
sort	I-Algorithm
Yes	O
Insertion	O
&	O
Merging	O
Combine	O
a	O
block-based	O
in-place	B-Algorithm
merge	B-Algorithm
algorithm	I-Algorithm
with	O
a	O
bottom-up	O
merge	B-Algorithm
sort	I-Algorithm
.	O
</s>
<s>
Timsort	B-Algorithm
Yes	O
Insertion	O
&	O
Merging	O
Makes	O
n-1	O
comparisons	O
when	O
the	O
data	O
is	O
already	O
sorted	O
or	O
reverse	O
sorted	O
.	O
</s>
<s>
Selection	B-Algorithm
sort	I-Algorithm
No	O
Selection	B-Algorithm
Stable	O
with	O
extra	O
space	O
,	O
when	O
using	O
linked	O
lists	O
,	O
or	O
when	O
made	O
as	O
a	O
variant	O
of	O
Insertion	B-Algorithm
Sort	I-Algorithm
instead	O
of	O
swapping	O
the	O
two	O
items	O
.	O
</s>
<s>
Cubesort	B-Algorithm
Yes	O
Insertion	O
Makes	O
n-1	O
comparisons	O
when	O
the	O
data	O
is	O
already	O
sorted	O
or	O
reverse	O
sorted	O
.	O
</s>
<s>
Shellsort	B-Algorithm
No	O
Insertion	O
Small	O
code	O
size	O
.	O
</s>
<s>
Bubble	B-Algorithm
sort	I-Algorithm
Yes	O
Exchanging	O
Tiny	O
code	O
size	O
.	O
</s>
<s>
Exchange	B-Algorithm
sort	I-Algorithm
No	O
Exchanging	O
Tiny	O
code	O
size	O
.	O
</s>
<s>
Tree	B-Algorithm
sort	I-Algorithm
Yes	O
Insertion	O
When	O
using	O
a	O
self-balancing	B-Data_Structure
binary	I-Data_Structure
search	I-Data_Structure
tree	I-Data_Structure
.	O
</s>
<s>
Cycle	B-Language
sort	I-Language
No	O
Selection	B-Algorithm
In-place	B-Algorithm
with	O
theoretically	O
optimal	O
number	O
of	O
writes	O
.	O
</s>
<s>
Library	B-Algorithm
sort	I-Algorithm
No	O
InsertionSimilar	O
to	O
a	O
gapped	B-Algorithm
insertion	I-Algorithm
sort	I-Algorithm
.	O
</s>
<s>
Patience	O
sorting	B-Algorithm
No	O
Insertion	O
&	O
Selection	B-Algorithm
Finds	O
all	O
the	O
longest	O
increasing	O
subsequences	O
in	O
.	O
</s>
<s>
Smoothsort	B-Application
No	O
Selection	B-Algorithm
An	O
adaptive	B-Algorithm
variant	O
of	O
heapsort	B-Application
based	O
upon	O
the	O
Leonardo	B-Algorithm
sequence	I-Algorithm
rather	O
than	O
a	O
traditional	O
binary	B-Application
heap	I-Application
.	O
</s>
<s>
Strand	B-Algorithm
sort	I-Algorithm
Yes	O
Selection	B-Algorithm
Tournament	B-Algorithm
sort	I-Algorithm
No	O
Selection	B-Algorithm
Variation	O
of	O
Heapsort	B-Application
.	O
</s>
<s>
Cocktail	B-Algorithm
shaker	I-Algorithm
sort	I-Algorithm
Yes	O
ExchangingA	O
variant	O
of	O
Bubblesort	B-Algorithm
which	O
deals	O
well	O
with	O
small	O
values	O
at	O
end	O
of	O
list	O
Comb	B-Algorithm
sort	I-Algorithm
No	O
Exchanging	O
Faster	O
than	O
bubble	B-Algorithm
sort	I-Algorithm
on	O
average	O
.	O
</s>
<s>
Gnome	B-Algorithm
sort	I-Algorithm
Yes	O
Exchanging	O
Tiny	O
code	O
size	O
.	O
</s>
<s>
Odd	B-Algorithm
–	I-Algorithm
even	I-Algorithm
sort	I-Algorithm
Yes	O
Exchanging	O
Can	O
be	O
run	O
on	O
parallel	O
processors	O
easily	O
.	O
</s>
<s>
The	O
following	O
table	O
describes	O
integer	B-Algorithm
sorting	B-Algorithm
algorithms	I-Algorithm
and	O
other	O
sorting	B-Algorithm
algorithms	I-Algorithm
that	O
are	O
not	O
comparison	B-Algorithm
sorts	I-Algorithm
.	O
</s>
<s>
In	O
the	O
unit-cost	O
random-access	B-Application
machine	I-Application
model	O
,	O
algorithms	O
with	O
running	O
time	O
of	O
,	O
such	O
as	O
radix	B-Algorithm
sort	I-Algorithm
,	O
still	O
take	O
time	O
proportional	O
to	O
,	O
because	O
is	O
limited	O
to	O
be	O
not	O
more	O
than	O
,	O
and	O
a	O
larger	O
number	O
of	O
elements	O
to	O
sort	O
would	O
require	O
a	O
bigger	O
in	O
order	O
to	O
store	O
them	O
in	O
the	O
memory	B-General_Concept
.	O
</s>
<s>
+	O
Non-comparison	O
sorts	O
Name	O
Best	O
Average	O
Worst	O
Memory	B-General_Concept
Stable	O
Notes	O
Pigeonhole	B-Algorithm
sort	I-Algorithm
—	O
Cannot	O
sort	O
non-integers	O
.	O
</s>
<s>
Bucket	B-Algorithm
sort	I-Algorithm
(	O
uniform	O
keys	O
)	O
—	O
Assumes	O
uniform	O
distribution	O
of	O
elements	O
from	O
the	O
domain	O
in	O
the	O
array	O
.	O
</s>
<s>
Also	O
cannot	O
sort	O
non-integers	O
Bucket	B-Algorithm
sort	I-Algorithm
(	O
integer	O
keys	O
)	O
—	O
If	O
r	O
is	O
,	O
then	O
average	O
time	O
complexity	O
is	O
.	O
</s>
<s>
Counting	B-Algorithm
sort	I-Algorithm
—	O
If	O
r	O
is	O
,	O
then	O
average	O
time	O
complexity	O
is	O
.	O
</s>
<s>
LSD	O
Radix	B-Algorithm
Sort	I-Algorithm
recursion	O
levels	O
,	O
2d	O
for	O
count	O
array	O
.	O
</s>
<s>
Unlike	O
most	O
distribution	O
sorts	O
,	O
this	O
can	O
sort	O
Floating	B-Algorithm
point	I-Algorithm
numbers	I-Algorithm
,	O
negative	O
numbers	O
and	O
more	O
.	O
</s>
<s>
MSD	O
Radix	B-Algorithm
Sort	I-Algorithm
—	O
Stable	O
version	O
uses	O
an	O
external	O
array	O
of	O
size	O
to	O
hold	O
all	O
of	O
the	O
bins	O
.	O
</s>
<s>
MSD	O
Radix	B-Algorithm
Sort	I-Algorithm
(	O
in-place	B-Algorithm
)	O
—	O
d	O
=	O
1	O
for	O
in-place	B-Algorithm
,	O
recursion	O
levels	O
,	O
no	O
count	O
array	O
.	O
</s>
<s>
Spreadsort	B-Algorithm
Asymptotic	O
are	O
based	O
on	O
the	O
assumption	O
that	O
,	O
but	O
the	O
algorithm	O
does	O
not	O
require	O
this	O
.	O
</s>
<s>
Burstsort	B-Algorithm
—	O
Has	O
better	O
constant	O
factor	O
than	O
radix	B-Algorithm
sort	I-Algorithm
for	O
sorting	B-Algorithm
strings	O
.	O
</s>
<s>
Flashsort	B-Algorithm
Requires	O
uniform	O
distribution	O
of	O
elements	O
from	O
the	O
domain	O
in	O
the	O
array	O
to	O
run	O
in	O
linear	O
time	O
.	O
</s>
<s>
If	O
distribution	O
is	O
extremely	O
skewed	O
then	O
it	O
can	O
go	O
quadratic	O
if	O
underlying	O
sort	O
is	O
quadratic	O
(	O
it	O
is	O
usually	O
an	O
insertion	B-Algorithm
sort	I-Algorithm
)	O
.	O
</s>
<s>
In-place	B-Algorithm
version	O
is	O
not	O
stable	O
.	O
</s>
<s>
Postman	O
sort	O
—	O
—	O
A	O
variation	O
of	O
bucket	B-Algorithm
sort	I-Algorithm
,	O
which	O
works	O
very	O
similarly	O
to	O
MSD	O
Radix	B-Algorithm
Sort	I-Algorithm
.	O
</s>
<s>
Samplesort	B-Operating_System
can	O
be	O
used	O
to	O
parallelize	O
any	O
of	O
the	O
non-comparison	O
sorts	O
,	O
by	O
efficiently	O
distributing	O
data	O
into	O
several	O
buckets	O
and	O
then	O
passing	O
down	O
sorting	B-Algorithm
to	O
several	O
processors	O
,	O
with	O
no	O
need	O
to	O
merge	B-Algorithm
as	O
buckets	O
are	O
already	O
sorted	O
between	O
each	O
other	O
.	O
</s>
<s>
Some	O
algorithms	O
are	O
slow	O
compared	O
to	O
those	O
discussed	O
above	O
,	O
such	O
as	O
the	O
bogosort	O
with	O
unbounded	O
run	O
time	O
and	O
the	O
stooge	B-Algorithm
sort	I-Algorithm
which	O
has	O
O( 	O
n2.7	O
)	O
run	O
time	O
.	O
</s>
<s>
The	O
following	O
table	O
describes	O
some	O
sorting	B-Algorithm
algorithms	I-Algorithm
that	O
are	O
impractical	O
for	O
real-life	O
use	O
in	O
traditional	O
software	O
contexts	O
due	O
to	O
extremely	O
poor	O
performance	O
or	O
specialized	O
hardware	O
requirements	O
.	O
</s>
<s>
Name	O
Best	O
Average	O
Worst	O
Memory	B-General_Concept
Stable	O
Comparison	O
Other	O
notes	O
Bead	B-Algorithm
sort	I-Algorithm
Works	O
only	O
with	O
positive	O
integers	O
.	O
</s>
<s>
Simple	B-Algorithm
pancake	I-Algorithm
sort	I-Algorithm
—	O
Count	O
is	O
number	O
of	O
flips	O
.	O
</s>
<s>
"	O
I	O
Ca	O
n't	O
Believe	O
It	O
Can	O
Sort	O
"	O
Notable	O
primarily	O
for	O
appearing	O
to	O
be	O
an	O
erroneous	O
implementation	O
of	O
either	O
Insertion	B-Algorithm
Sort	I-Algorithm
or	O
Exchange	B-Algorithm
Sort	I-Algorithm
.	O
</s>
<s>
Spaghetti	O
(	O
Poll	O
)	O
sort	O
Polling	O
This	O
is	O
a	O
linear-time	O
,	O
analog	O
algorithm	O
for	O
sorting	B-Algorithm
a	O
sequence	O
of	O
items	O
,	O
requiring	O
O(n )	O
stack	O
space	O
,	O
and	O
the	O
sort	O
is	O
stable	O
.	O
</s>
<s>
See	O
spaghetti	B-Algorithm
sort	I-Algorithm
#Analysis	O
.	O
</s>
<s>
Sorting	B-Algorithm
network	I-Algorithm
(	O
stable	O
sorting	B-Algorithm
networks	I-Algorithm
require	O
more	O
comparisons	O
)	O
Order	O
of	O
comparisons	O
are	O
set	O
in	O
advance	O
based	O
on	O
a	O
fixed	O
network	O
size	O
.	O
</s>
<s>
Bitonic	B-Algorithm
sorter	I-Algorithm
An	O
effective	O
variation	O
of	O
Sorting	B-Algorithm
networks	I-Algorithm
.	O
</s>
<s>
Used	O
for	O
example	O
purposes	O
only	O
,	O
as	O
even	O
the	O
expected	O
best-case	O
runtime	O
is	O
awful	O
..	O
Stooge	B-Algorithm
sort	I-Algorithm
Slower	O
than	O
most	O
of	O
the	O
sorting	B-Algorithm
algorithms	I-Algorithm
(	O
even	O
naive	O
ones	O
)	O
with	O
a	O
time	O
complexity	O
of	O
Can	O
be	O
made	O
stable	O
,	O
and	O
is	O
also	O
a	O
sorting	B-Algorithm
network	I-Algorithm
.	O
</s>
<s>
Slowsort	B-Algorithm
A	O
multiply	O
and	O
surrender	O
algorithm	O
,	O
antonymous	O
with	O
divide-and-conquer	B-Algorithm
algorithm	I-Algorithm
.	O
</s>
<s>
Theoretical	O
computer	B-General_Concept
scientists	I-General_Concept
have	O
detailed	O
other	O
sorting	B-Algorithm
algorithms	I-Algorithm
that	O
provide	O
better	O
than	O
O(n log n )	O
time	O
complexity	O
assuming	O
additional	O
constraints	O
,	O
including	O
:	O
</s>
<s>
Thorup	O
's	O
algorithm	O
,	O
a	O
randomized	B-General_Concept
algorithm	I-General_Concept
for	O
sorting	B-Algorithm
keys	O
from	O
a	O
domain	O
of	O
finite	O
size	O
,	O
taking	O
time	O
and	O
O(n )	O
space	O
.	O
</s>
<s>
A	O
randomized	O
integer	B-Algorithm
sorting	I-Algorithm
algorithm	O
taking	O
expected	O
time	O
and	O
O(n )	O
space	O
.	O
</s>
<s>
One	O
of	O
the	O
authors	O
of	O
the	O
previously	O
mentioned	O
algorithm	O
also	O
claims	O
to	O
have	O
discovered	O
an	O
algorithm	O
taking	O
time	O
and	O
O(n )	O
space	O
,	O
sorting	B-Algorithm
real	O
numbers	O
.	O
</s>
<s>
While	O
there	O
are	O
a	O
large	O
number	O
of	O
sorting	B-Algorithm
algorithms	I-Algorithm
,	O
in	O
practical	O
implementations	O
a	O
few	O
algorithms	O
predominate	O
.	O
</s>
<s>
Insertion	B-Algorithm
sort	I-Algorithm
is	O
widely	O
used	O
for	O
small	O
data	B-General_Concept
sets	I-General_Concept
,	O
while	O
for	O
large	O
data	B-General_Concept
sets	I-General_Concept
an	O
asymptotically	O
efficient	O
sort	O
is	O
used	O
,	O
primarily	O
heapsort	B-Application
,	O
merge	B-Algorithm
sort	I-Algorithm
,	O
or	O
quicksort	B-Algorithm
.	O
</s>
<s>
Efficient	O
implementations	O
generally	O
use	O
a	O
hybrid	B-Algorithm
algorithm	I-Algorithm
,	O
combining	O
an	O
asymptotically	O
efficient	B-General_Concept
algorithm	I-General_Concept
for	O
the	O
overall	O
sort	O
with	O
insertion	B-Algorithm
sort	I-Algorithm
for	O
small	O
lists	O
at	O
the	O
bottom	O
of	O
a	O
recursion	O
.	O
</s>
<s>
Highly	O
tuned	O
implementations	O
use	O
more	O
sophisticated	O
variants	O
,	O
such	O
as	O
Timsort	B-Algorithm
(	O
merge	B-Algorithm
sort	I-Algorithm
,	O
insertion	B-Algorithm
sort	I-Algorithm
,	O
and	O
additional	O
logic	O
)	O
,	O
used	O
in	O
Android	B-Application
,	O
Java	B-Language
,	O
and	O
Python	B-Language
,	O
and	O
introsort	O
(	O
quicksort	B-Algorithm
and	O
heapsort	B-Application
)	O
,	O
used	O
(	O
in	O
variant	O
forms	O
)	O
in	O
some	O
C++	B-Language
sort	I-Language
implementations	O
and	O
in	O
.NET	B-Application
.	O
</s>
<s>
For	O
more	O
restricted	O
data	O
,	O
such	O
as	O
numbers	O
in	O
a	O
fixed	O
interval	O
,	O
distribution	O
sorts	O
such	O
as	O
counting	B-Algorithm
sort	I-Algorithm
or	O
radix	B-Algorithm
sort	I-Algorithm
are	O
widely	O
used	O
.	O
</s>
<s>
Bubble	B-Algorithm
sort	I-Algorithm
and	O
variants	O
are	O
rarely	O
used	O
in	O
practice	O
,	O
but	O
are	O
commonly	O
found	O
in	O
teaching	O
and	O
theoretical	O
discussions	O
.	O
</s>
<s>
When	O
physically	O
sorting	B-Algorithm
objects	O
(	O
such	O
as	O
alphabetizing	O
papers	O
,	O
tests	O
or	O
books	O
)	O
people	O
intuitively	O
generally	O
use	O
insertion	B-Algorithm
sorts	I-Algorithm
for	O
small	O
sets	O
.	O
</s>
<s>
For	O
larger	O
sets	O
,	O
people	O
often	O
first	O
bucket	O
,	O
such	O
as	O
by	O
initial	O
letter	O
,	O
and	O
multiple	O
bucketing	O
allows	O
practical	O
sorting	B-Algorithm
of	O
very	O
large	O
sets	O
.	O
</s>
<s>
Often	O
space	O
is	O
relatively	O
cheap	O
,	O
such	O
as	O
by	O
spreading	O
objects	O
out	O
on	O
the	O
floor	O
or	O
over	O
a	O
large	O
area	O
,	O
but	O
operations	O
are	O
expensive	O
,	O
particularly	O
moving	O
an	O
object	O
a	O
large	O
distance	O
–	O
locality	B-General_Concept
of	I-General_Concept
reference	I-General_Concept
is	O
important	O
.	O
</s>
<s>
Merge	B-Algorithm
sorts	I-Algorithm
are	O
also	O
practical	O
for	O
physical	O
objects	O
,	O
particularly	O
as	O
two	O
hands	O
can	O
be	O
used	O
,	O
one	O
for	O
each	O
list	O
to	O
merge	B-Algorithm
,	O
while	O
other	O
algorithms	O
,	O
such	O
as	O
heapsort	B-Application
or	O
quicksort	B-Algorithm
,	O
are	O
poorly	O
suited	O
for	O
human	O
use	O
.	O
</s>
<s>
Other	O
algorithms	O
,	O
such	O
as	O
library	B-Algorithm
sort	I-Algorithm
,	O
a	O
variant	O
of	O
insertion	B-Algorithm
sort	I-Algorithm
that	O
leaves	O
spaces	O
,	O
are	O
also	O
practical	O
for	O
physical	O
use	O
.	O
</s>
<s>
Two	O
of	O
the	O
simplest	O
sorts	O
are	O
insertion	B-Algorithm
sort	I-Algorithm
and	O
selection	B-Algorithm
sort	I-Algorithm
,	O
both	O
of	O
which	O
are	O
efficient	O
on	O
small	O
data	O
,	O
due	O
to	O
low	O
overhead	O
,	O
but	O
not	O
efficient	O
on	O
large	O
data	O
.	O
</s>
<s>
Insertion	B-Algorithm
sort	I-Algorithm
is	O
generally	O
faster	O
than	O
selection	B-Algorithm
sort	I-Algorithm
in	O
practice	O
,	O
due	O
to	O
fewer	O
comparisons	O
and	O
good	O
performance	O
on	O
almost-sorted	O
data	O
,	O
and	O
thus	O
is	O
preferred	O
in	O
practice	O
,	O
but	O
selection	B-Algorithm
sort	I-Algorithm
uses	O
fewer	O
writes	O
,	O
and	O
thus	O
is	O
used	O
when	O
write	O
performance	O
is	O
a	O
limiting	O
factor	O
.	O
</s>
<s>
Insertion	B-Algorithm
sort	I-Algorithm
is	O
a	O
simple	O
sorting	B-Algorithm
algorithm	I-Algorithm
that	O
is	O
relatively	O
efficient	O
for	O
small	O
lists	O
and	O
mostly	O
sorted	B-Algorithm
lists	I-Algorithm
,	O
and	O
is	O
often	O
used	O
as	O
part	O
of	O
more	O
sophisticated	O
algorithms	O
.	O
</s>
<s>
It	O
works	O
by	O
taking	O
elements	O
from	O
the	O
list	O
one	O
by	O
one	O
and	O
inserting	O
them	O
in	O
their	O
correct	O
position	O
into	O
a	O
new	O
sorted	B-Algorithm
list	I-Algorithm
similar	O
to	O
how	O
we	O
put	O
money	O
in	O
our	O
wallet	O
.	O
</s>
<s>
Shellsort	B-Algorithm
is	O
a	O
variant	O
of	O
insertion	B-Algorithm
sort	I-Algorithm
that	O
is	O
more	O
efficient	O
for	O
larger	O
lists	O
.	O
</s>
<s>
Selection	B-Algorithm
sort	I-Algorithm
is	O
an	O
in-place	B-Algorithm
comparison	B-Algorithm
sort	I-Algorithm
.	O
</s>
<s>
It	O
has	O
O(n2 )	O
complexity	O
,	O
making	O
it	O
inefficient	O
on	O
large	O
lists	O
,	O
and	O
generally	O
performs	O
worse	O
than	O
the	O
similar	O
insertion	B-Algorithm
sort	I-Algorithm
.	O
</s>
<s>
Selection	B-Algorithm
sort	I-Algorithm
is	O
noted	O
for	O
its	O
simplicity	O
,	O
and	O
also	O
has	O
performance	O
advantages	O
over	O
more	O
complicated	O
algorithms	O
in	O
certain	O
situations	O
.	O
</s>
<s>
Practical	O
general	O
sorting	B-Algorithm
algorithms	I-Algorithm
are	O
almost	O
always	O
based	O
on	O
an	O
algorithm	O
with	O
average	O
time	O
complexity	O
(	O
and	O
generally	O
worst-case	B-General_Concept
complexity	O
)	O
O(n log n )	O
,	O
of	O
which	O
the	O
most	O
common	O
are	O
heapsort	B-Application
,	O
merge	B-Algorithm
sort	I-Algorithm
,	O
and	O
quicksort	B-Algorithm
.	O
</s>
<s>
Each	O
has	O
advantages	O
and	O
drawbacks	O
,	O
with	O
the	O
most	O
significant	O
being	O
that	O
simple	O
implementation	O
of	O
merge	B-Algorithm
sort	I-Algorithm
uses	O
O(n )	O
additional	O
space	O
,	O
and	O
simple	O
implementation	O
of	O
quicksort	B-Algorithm
has	O
O(n2 )	O
worst-case	B-General_Concept
complexity	O
.	O
</s>
<s>
While	O
these	O
algorithms	O
are	O
asymptotically	O
efficient	O
on	O
random	O
data	O
,	O
for	O
practical	O
efficiency	B-General_Concept
on	O
real-world	O
data	O
various	O
modifications	O
are	O
used	O
.	O
</s>
<s>
First	O
,	O
the	O
overhead	O
of	O
these	O
algorithms	O
becomes	O
significant	O
on	O
smaller	O
data	O
,	O
so	O
often	O
a	O
hybrid	B-Algorithm
algorithm	I-Algorithm
is	O
used	O
,	O
commonly	O
switching	O
to	O
insertion	B-Algorithm
sort	I-Algorithm
once	O
the	O
data	O
is	O
small	O
enough	O
.	O
</s>
<s>
Thus	O
more	O
sophisticated	O
algorithms	O
are	O
often	O
employed	O
,	O
such	O
as	O
Timsort	B-Algorithm
(	O
based	O
on	O
merge	B-Algorithm
sort	I-Algorithm
)	O
or	O
introsort	O
(	O
based	O
on	O
quicksort	B-Algorithm
,	O
falling	O
back	O
to	O
heapsort	B-Application
)	O
.	O
</s>
<s>
Merge	B-Algorithm
sort	I-Algorithm
takes	O
advantage	O
of	O
the	O
ease	O
of	O
merging	O
already	O
sorted	B-Algorithm
lists	I-Algorithm
into	O
a	O
new	O
sorted	B-Algorithm
list	I-Algorithm
.	O
</s>
<s>
It	O
then	O
merges	O
each	O
of	O
the	O
resulting	O
lists	O
of	O
two	O
into	O
lists	O
of	O
four	O
,	O
then	O
merges	O
those	O
lists	O
of	O
four	O
,	O
and	O
so	O
on	O
;	O
until	O
at	O
last	O
two	O
lists	O
are	O
merged	O
into	O
the	O
final	O
sorted	B-Algorithm
list	I-Algorithm
.	O
</s>
<s>
Of	O
the	O
algorithms	O
described	O
here	O
,	O
this	O
is	O
the	O
first	O
that	O
scales	O
well	O
to	O
very	O
large	O
lists	O
,	O
because	O
its	O
worst-case	B-General_Concept
running	O
time	O
is	O
O(n log n )	O
.	O
</s>
<s>
It	O
is	O
also	O
easily	O
applied	O
to	O
lists	O
,	O
not	O
only	O
arrays	O
,	O
as	O
it	O
only	O
requires	O
sequential	B-General_Concept
access	I-General_Concept
,	O
not	O
random	B-General_Concept
access	I-General_Concept
.	O
</s>
<s>
Merge	B-Algorithm
sort	I-Algorithm
has	O
seen	O
a	O
relatively	O
recent	O
surge	O
in	O
popularity	O
for	O
practical	O
implementations	O
,	O
due	O
to	O
its	O
use	O
in	O
the	O
sophisticated	O
algorithm	O
Timsort	B-Algorithm
,	O
which	O
is	O
used	O
for	O
the	O
standard	O
sort	O
routine	O
in	O
the	O
programming	O
languages	O
Python	B-Language
and	O
Java	B-Language
(	O
as	O
of	O
JDK7	O
)	O
.	O
</s>
<s>
Merge	B-Algorithm
sort	I-Algorithm
itself	O
is	O
the	O
standard	O
routine	O
in	O
Perl	B-Language
,	O
among	O
others	O
,	O
and	O
has	O
been	O
used	O
in	O
Java	B-Language
at	O
least	O
since	O
2000	O
in	O
JDK1.3	O
.	O
</s>
<s>
Heapsort	B-Application
is	O
a	O
much	O
more	O
efficient	O
version	O
of	O
selection	B-Algorithm
sort	I-Algorithm
.	O
</s>
<s>
It	O
also	O
works	O
by	O
determining	O
the	O
largest	O
(	O
or	O
smallest	O
)	O
element	O
of	O
the	O
list	O
,	O
placing	O
that	O
at	O
the	O
end	O
(	O
or	O
beginning	O
)	O
of	O
the	O
list	O
,	O
then	O
continuing	O
with	O
the	O
rest	O
of	O
the	O
list	O
,	O
but	O
accomplishes	O
this	O
task	O
efficiently	O
by	O
using	O
a	O
data	B-General_Concept
structure	I-General_Concept
called	O
a	O
heap	B-Application
,	O
a	O
special	O
type	O
of	O
binary	O
tree	O
.	O
</s>
<s>
Once	O
the	O
data	O
list	O
has	O
been	O
made	O
into	O
a	O
heap	B-Application
,	O
the	O
root	O
node	O
is	O
guaranteed	O
to	O
be	O
the	O
largest	O
(	O
or	O
smallest	O
)	O
element	O
.	O
</s>
<s>
When	O
it	O
is	O
removed	O
and	O
placed	O
at	O
the	O
end	O
of	O
the	O
list	O
,	O
the	O
heap	B-Application
is	O
rearranged	O
so	O
the	O
largest	O
element	O
remaining	O
moves	O
to	O
the	O
root	O
.	O
</s>
<s>
Using	O
the	O
heap	B-Application
,	O
finding	O
the	O
next	O
largest	O
element	O
takes	O
O(log n )	O
time	O
,	O
instead	O
of	O
O(n )	O
for	O
a	O
linear	O
scan	O
as	O
in	O
simple	O
selection	B-Algorithm
sort	I-Algorithm
.	O
</s>
<s>
This	O
allows	O
Heapsort	B-Application
to	O
run	O
in	O
O(n log n )	O
time	O
,	O
and	O
this	O
is	O
also	O
the	O
worst	B-General_Concept
case	I-General_Concept
complexity	O
.	O
</s>
<s>
Quicksort	B-Algorithm
is	O
a	O
divide-and-conquer	B-Algorithm
algorithm	I-Algorithm
which	O
relies	O
on	O
a	O
partition	O
operation	O
:	O
to	O
partition	O
an	O
array	O
,	O
an	O
element	O
called	O
a	O
pivot	O
is	O
selected	O
.	O
</s>
<s>
This	O
can	O
be	O
done	O
efficiently	O
in	O
linear	O
time	O
and	O
in-place	B-Algorithm
.	O
</s>
<s>
Efficient	O
implementations	O
of	O
quicksort	B-Algorithm
(	O
with	O
in-place	B-Algorithm
partitioning	O
)	O
are	O
typically	O
unstable	O
sorts	O
and	O
somewhat	O
complex	O
,	O
but	O
are	O
among	O
the	O
fastest	O
sorting	B-Algorithm
algorithms	I-Algorithm
in	O
practice	O
.	O
</s>
<s>
Together	O
with	O
its	O
modest	O
O(log n )	O
space	O
usage	O
,	O
quicksort	B-Algorithm
is	O
one	O
of	O
the	O
most	O
popular	O
sorting	B-Algorithm
algorithms	I-Algorithm
and	O
is	O
available	O
in	O
many	O
standard	O
programming	O
libraries	O
.	O
</s>
<s>
The	O
important	O
caveat	O
about	O
quicksort	B-Algorithm
is	O
that	O
its	O
worst-case	B-General_Concept
performance	I-General_Concept
is	O
O(n2 )	O
;	O
while	O
this	O
is	O
rare	O
,	O
in	O
naive	O
implementations	O
(	O
choosing	O
the	O
first	O
or	O
last	O
element	O
as	O
pivot	O
)	O
this	O
occurs	O
for	O
sorted	O
data	O
,	O
which	O
is	O
a	O
common	O
case	O
.	O
</s>
<s>
The	O
most	O
complex	O
issue	O
in	O
quicksort	B-Algorithm
is	O
thus	O
choosing	O
a	O
good	O
pivot	O
element	O
,	O
as	O
consistently	O
poor	O
choices	O
of	O
pivots	O
can	O
result	O
in	O
drastically	O
slower	O
O(n2 )	O
performance	O
,	O
but	O
good	O
choice	O
of	O
pivots	O
yields	O
O(n log n )	O
performance	O
,	O
which	O
is	O
asymptotically	O
optimal	O
.	O
</s>
<s>
Finding	O
the	O
median	O
,	O
such	O
as	O
by	O
the	O
median	B-Algorithm
of	I-Algorithm
medians	I-Algorithm
selection	B-Algorithm
algorithm	I-Algorithm
is	O
however	O
an	O
O(n )	O
operation	O
on	O
unsorted	B-Algorithm
lists	I-Algorithm
and	O
therefore	O
exacts	O
significant	O
overhead	O
with	O
sorting	B-Algorithm
.	O
</s>
<s>
If	O
that	O
limit	O
is	O
exceeded	O
,	O
then	O
sorting	B-Algorithm
is	O
continued	O
using	O
the	O
heapsort	B-Application
algorithm	O
.	O
</s>
<s>
Shellsort	B-Algorithm
was	O
invented	O
by	O
Donald	O
Shell	O
in	O
1959	O
.	O
</s>
<s>
It	O
improves	O
upon	O
insertion	B-Algorithm
sort	I-Algorithm
by	O
moving	O
out	O
of	O
order	O
elements	O
more	O
than	O
one	O
position	O
at	O
a	O
time	O
.	O
</s>
<s>
The	O
concept	O
behind	O
Shellsort	B-Algorithm
is	O
that	O
insertion	B-Algorithm
sort	I-Algorithm
performs	O
in	O
time	O
,	O
where	O
k	O
is	O
the	O
greatest	O
distance	O
between	O
two	O
out-of-place	O
elements	O
.	O
</s>
<s>
So	O
,	O
by	O
first	O
sorting	B-Algorithm
elements	O
far	O
away	O
,	O
and	O
progressively	O
shrinking	O
the	O
gap	O
between	O
the	O
elements	O
to	O
sort	O
,	O
the	O
final	O
sort	O
computes	O
much	O
faster	O
.	O
</s>
<s>
One	O
implementation	O
can	O
be	O
described	O
as	O
arranging	O
the	O
data	O
sequence	O
in	O
a	O
two-dimensional	O
array	O
and	O
then	O
sorting	B-Algorithm
the	O
columns	O
of	O
the	O
array	O
using	O
insertion	B-Algorithm
sort	I-Algorithm
.	O
</s>
<s>
The	O
worst-case	B-General_Concept
time	O
complexity	O
of	O
Shellsort	B-Algorithm
is	O
an	O
open	O
problem	O
and	O
depends	O
on	O
the	O
gap	O
sequence	O
used	O
,	O
with	O
known	O
complexities	O
ranging	O
from	O
O(n2 )	O
to	O
O( 	O
n4/3	O
)	O
and	O
Θ(n log2 n )	O
.	O
</s>
<s>
This	O
,	O
combined	O
with	O
the	O
fact	O
that	O
Shellsort	B-Algorithm
is	O
in-place	B-Algorithm
,	O
only	O
needs	O
a	O
relatively	O
small	O
amount	O
of	O
code	O
,	O
and	O
does	O
not	O
require	O
use	O
of	O
the	O
call	B-General_Concept
stack	I-General_Concept
,	O
makes	O
it	O
is	O
useful	O
in	O
situations	O
where	O
memory	B-General_Concept
is	O
at	O
a	O
premium	O
,	O
such	O
as	O
in	O
embedded	B-Architecture
systems	I-Architecture
and	O
operating	B-Operating_System
system	I-Operating_System
kernels	I-Operating_System
.	O
</s>
<s>
Bubble	B-Algorithm
sort	I-Algorithm
,	O
and	O
variants	O
such	O
as	O
the	O
Comb	B-Algorithm
sort	I-Algorithm
and	O
cocktail	B-Algorithm
sort	I-Algorithm
,	O
are	O
simple	O
,	O
highly	O
inefficient	O
sorting	B-Algorithm
algorithms	I-Algorithm
.	O
</s>
<s>
Bubble	B-Algorithm
sort	I-Algorithm
is	O
a	O
simple	O
sorting	B-Algorithm
algorithm	I-Algorithm
.	O
</s>
<s>
The	O
algorithm	O
starts	O
at	O
the	O
beginning	O
of	O
the	O
data	B-General_Concept
set	I-General_Concept
.	O
</s>
<s>
It	O
continues	O
doing	O
this	O
for	O
each	O
pair	O
of	O
adjacent	O
elements	O
to	O
the	O
end	O
of	O
the	O
data	B-General_Concept
set	I-General_Concept
.	O
</s>
<s>
This	O
algorithm	O
's	O
average	O
time	O
and	O
worst-case	B-General_Concept
performance	I-General_Concept
is	O
O(n2 )	O
,	O
so	O
it	O
is	O
rarely	O
used	O
to	O
sort	O
large	O
,	O
unordered	O
data	B-General_Concept
sets	I-General_Concept
.	O
</s>
<s>
Bubble	B-Algorithm
sort	I-Algorithm
can	O
be	O
used	O
to	O
sort	O
a	O
small	O
number	O
of	O
items	O
(	O
where	O
its	O
asymptotic	O
inefficiency	O
is	O
not	O
a	O
high	O
penalty	O
)	O
.	O
</s>
<s>
Bubble	B-Algorithm
sort	I-Algorithm
can	O
also	O
be	O
used	O
efficiently	O
on	O
a	O
list	O
of	O
any	O
length	O
that	O
is	O
nearly	O
sorted	O
(	O
that	O
is	O
,	O
the	O
elements	O
are	O
not	O
significantly	O
out	O
of	O
place	O
)	O
.	O
</s>
<s>
0123546789	O
and	O
1032547698	O
)	O
,	O
bubble	B-Algorithm
sort	I-Algorithm
's	O
exchange	O
will	O
get	O
them	O
in	O
order	O
on	O
the	O
first	O
pass	O
,	O
the	O
second	O
pass	O
will	O
find	O
all	O
elements	O
in	O
order	O
,	O
so	O
the	O
sort	O
will	O
take	O
only	O
2n	O
time	O
.	O
</s>
<s>
Comb	B-Algorithm
sort	I-Algorithm
is	O
a	O
relatively	O
simple	O
sorting	B-Algorithm
algorithm	I-Algorithm
based	O
on	O
bubble	B-Algorithm
sort	I-Algorithm
and	O
originally	O
designed	O
by	O
Włodzimierz	O
Dobosiewicz	O
in	O
1980	O
.	O
</s>
<s>
The	O
basic	O
idea	O
is	O
to	O
eliminate	O
turtles	O
,	O
or	O
small	O
values	O
near	O
the	O
end	O
of	O
the	O
list	O
,	O
since	O
in	O
a	O
bubble	B-Algorithm
sort	I-Algorithm
these	O
slow	O
the	O
sorting	B-Algorithm
down	O
tremendously	O
.	O
</s>
<s>
(	O
Rabbits	O
,	O
large	O
values	O
around	O
the	O
beginning	O
of	O
the	O
list	O
,	O
do	O
not	O
pose	O
a	O
problem	O
in	O
bubble	B-Algorithm
sort	I-Algorithm
)	O
It	O
accomplishes	O
this	O
by	O
initially	O
swapping	O
elements	O
that	O
are	O
a	O
certain	O
distance	O
from	O
one	O
another	O
in	O
the	O
array	O
,	O
rather	O
than	O
only	O
swapping	O
elements	O
if	O
they	O
are	O
adjacent	O
to	O
one	O
another	O
,	O
and	O
then	O
shrinking	O
the	O
chosen	O
distance	O
until	O
it	O
is	O
operating	O
as	O
a	O
normal	O
bubble	B-Algorithm
sort	I-Algorithm
.	O
</s>
<s>
Thus	O
,	O
if	O
Shellsort	B-Algorithm
can	O
be	O
thought	O
of	O
as	O
a	O
generalized	O
version	O
of	O
insertion	B-Algorithm
sort	I-Algorithm
that	O
swaps	O
elements	O
spaced	O
a	O
certain	O
distance	O
away	O
from	O
one	O
another	O
,	O
comb	B-Algorithm
sort	I-Algorithm
can	O
be	O
thought	O
of	O
as	O
the	O
same	O
generalization	O
applied	O
to	O
bubble	B-Algorithm
sort	I-Algorithm
.	O
</s>
<s>
Exchange	B-Algorithm
sort	I-Algorithm
is	O
sometimes	O
confused	O
with	O
bubble	B-Algorithm
sort	I-Algorithm
,	O
although	O
the	O
algorithms	O
are	O
in	O
fact	O
distinct	O
.	O
</s>
<s>
Exchange	B-Algorithm
sort	I-Algorithm
works	O
by	O
comparing	O
the	O
first	O
element	O
with	O
all	O
elements	O
above	O
it	O
,	O
swapping	O
where	O
needed	O
,	O
thereby	O
guaranteeing	O
that	O
the	O
first	O
element	O
is	O
correct	O
for	O
the	O
final	O
sort	O
order	O
;	O
it	O
then	O
proceeds	O
to	O
do	O
the	O
same	O
for	O
the	O
second	O
element	O
,	O
and	O
so	O
on	O
.	O
</s>
<s>
It	O
lacks	O
the	O
advantage	O
which	O
bubble	B-Algorithm
sort	I-Algorithm
has	O
of	O
detecting	O
in	O
one	O
pass	O
if	O
the	O
list	O
is	O
already	O
sorted	O
,	O
but	O
it	O
can	O
be	O
faster	O
than	O
bubble	B-Algorithm
sort	I-Algorithm
by	O
a	O
constant	O
factor	O
(	O
one	O
less	O
pass	O
over	O
the	O
data	O
to	O
be	O
sorted	O
;	O
half	O
as	O
many	O
total	O
comparisons	O
)	O
in	O
worst	B-General_Concept
case	I-General_Concept
situations	O
.	O
</s>
<s>
Like	O
any	O
simple	O
O(n2 )	O
sort	O
it	O
can	O
be	O
reasonably	O
fast	O
over	O
very	O
small	O
data	B-General_Concept
sets	I-General_Concept
,	O
though	O
in	O
general	O
insertion	B-Algorithm
sort	I-Algorithm
will	O
be	O
faster	O
.	O
</s>
<s>
Distribution	O
sort	O
refers	O
to	O
any	O
sorting	B-Algorithm
algorithm	I-Algorithm
where	O
data	O
is	O
distributed	O
from	O
their	O
input	O
to	O
multiple	O
intermediate	O
structures	O
which	O
are	O
then	O
gathered	O
and	O
placed	O
on	O
the	O
output	O
.	O
</s>
<s>
For	O
example	O
,	O
both	O
bucket	B-Algorithm
sort	I-Algorithm
and	O
flashsort	B-Algorithm
are	O
distribution	O
based	O
sorting	B-Algorithm
algorithms	I-Algorithm
.	O
</s>
<s>
Distribution	O
sorting	B-Algorithm
algorithms	I-Algorithm
can	O
be	O
used	O
on	O
a	O
single	O
processor	O
,	O
or	O
they	O
can	O
be	O
a	O
distributed	B-Operating_System
algorithm	I-Operating_System
,	O
where	O
individual	O
subsets	O
are	O
separately	O
sorted	O
on	O
different	O
processors	O
,	O
then	O
combined	O
.	O
</s>
<s>
This	O
allows	O
external	B-Algorithm
sorting	I-Algorithm
of	O
data	O
too	O
large	O
to	O
fit	O
into	O
a	O
single	O
computer	O
's	O
memory	B-General_Concept
.	O
</s>
<s>
Counting	B-Algorithm
sort	I-Algorithm
is	O
applicable	O
when	O
each	O
input	O
is	O
known	O
to	O
belong	O
to	O
a	O
particular	O
set	O
,	O
S	O
,	O
of	O
possibilities	O
.	O
</s>
<s>
The	O
algorithm	O
runs	O
in	O
O( |S|	O
+	O
n	O
)	O
time	O
and	O
O( |S|	O
)	O
memory	B-General_Concept
where	O
n	O
is	O
the	O
length	O
of	O
the	O
input	O
.	O
</s>
<s>
This	O
sorting	B-Algorithm
algorithm	I-Algorithm
often	O
cannot	O
be	O
used	O
because	O
S	O
needs	O
to	O
be	O
reasonably	O
small	O
for	O
the	O
algorithm	O
to	O
be	O
efficient	O
,	O
but	O
it	O
is	O
extremely	O
fast	O
and	O
demonstrates	O
great	O
asymptotic	O
behavior	O
as	O
n	O
increases	O
.	O
</s>
<s>
Bucket	B-Algorithm
sort	I-Algorithm
is	O
a	O
divide-and-conquer	B-Algorithm
sorting	B-Algorithm
algorithm	I-Algorithm
that	O
generalizes	O
counting	B-Algorithm
sort	I-Algorithm
by	O
partitioning	O
an	O
array	O
into	O
a	O
finite	O
number	O
of	O
buckets	O
.	O
</s>
<s>
Each	O
bucket	O
is	O
then	O
sorted	O
individually	O
,	O
either	O
using	O
a	O
different	O
sorting	B-Algorithm
algorithm	I-Algorithm
,	O
or	O
by	O
recursively	O
applying	O
the	O
bucket	B-Algorithm
sorting	B-Algorithm
algorithm	I-Algorithm
.	O
</s>
<s>
A	O
bucket	B-Algorithm
sort	I-Algorithm
works	O
best	O
when	O
the	O
elements	O
of	O
the	O
data	B-General_Concept
set	I-General_Concept
are	O
evenly	O
distributed	O
across	O
all	O
buckets	O
.	O
</s>
<s>
Radix	B-Algorithm
sort	I-Algorithm
is	O
an	O
algorithm	O
that	O
sorts	O
numbers	O
by	O
processing	O
individual	O
digits	O
.	O
</s>
<s>
Radix	B-Algorithm
sort	I-Algorithm
can	O
process	O
digits	O
of	O
each	O
number	O
either	O
starting	O
from	O
the	O
least	O
significant	O
digit	O
(	O
LSD	O
)	O
or	O
starting	O
from	O
the	O
most	O
significant	O
digit	O
(	O
MSD	O
)	O
.	O
</s>
<s>
Then	O
it	O
sorts	O
them	O
by	O
the	O
next	O
digit	O
,	O
and	O
so	O
on	O
from	O
the	O
least	O
significant	O
to	O
the	O
most	O
significant	O
,	O
ending	O
up	O
with	O
a	O
sorted	B-Algorithm
list	I-Algorithm
.	O
</s>
<s>
While	O
the	O
LSD	O
radix	B-Algorithm
sort	I-Algorithm
requires	O
the	O
use	O
of	O
a	O
stable	O
sort	O
,	O
the	O
MSD	O
radix	B-Algorithm
sort	I-Algorithm
algorithm	O
does	O
not	O
(	O
unless	O
stable	O
sorting	B-Algorithm
is	O
desired	O
)	O
.	O
</s>
<s>
In-place	B-Algorithm
MSD	O
radix	B-Algorithm
sort	I-Algorithm
is	O
not	O
stable	O
.	O
</s>
<s>
It	O
is	O
common	O
for	O
the	O
counting	B-Algorithm
sort	I-Algorithm
algorithm	O
to	O
be	O
used	O
internally	O
by	O
the	O
radix	B-Algorithm
sort	I-Algorithm
.	O
</s>
<s>
A	O
hybrid	B-Algorithm
sorting	B-Algorithm
approach	O
,	O
such	O
as	O
using	O
insertion	B-Algorithm
sort	I-Algorithm
for	O
small	O
bins	O
,	O
improves	O
performance	O
of	O
radix	B-Algorithm
sort	I-Algorithm
significantly	O
.	O
</s>
<s>
When	O
the	O
size	O
of	O
the	O
array	O
to	O
be	O
sorted	O
approaches	O
or	O
exceeds	O
the	O
available	O
primary	O
memory	B-General_Concept
,	O
so	O
that	O
(	O
much	O
slower	O
)	O
disk	O
or	O
swap	O
space	O
must	O
be	O
employed	O
,	O
the	O
memory	B-General_Concept
usage	O
pattern	O
of	O
a	O
sorting	B-Algorithm
algorithm	I-Algorithm
becomes	O
important	O
,	O
and	O
an	O
algorithm	O
that	O
might	O
have	O
been	O
fairly	O
efficient	O
when	O
the	O
array	O
fit	O
easily	O
in	O
RAM	O
may	O
become	O
impractical	O
.	O
</s>
<s>
In	O
this	O
scenario	O
,	O
the	O
total	O
number	O
of	O
comparisons	O
becomes	O
(	O
relatively	O
)	O
less	O
important	O
,	O
and	O
the	O
number	O
of	O
times	O
sections	O
of	O
memory	B-General_Concept
must	O
be	O
copied	O
or	O
swapped	O
to	O
and	O
from	O
the	O
disk	O
can	O
dominate	O
the	O
performance	O
characteristics	O
of	O
an	O
algorithm	O
.	O
</s>
<s>
Thus	O
,	O
the	O
number	O
of	O
passes	O
and	O
the	O
localization	O
of	O
comparisons	O
can	O
be	O
more	O
important	O
than	O
the	O
raw	O
number	O
of	O
comparisons	O
,	O
since	O
comparisons	O
of	O
nearby	O
elements	O
to	O
one	O
another	O
happen	O
at	O
system	B-General_Concept
bus	I-General_Concept
speed	O
(	O
or	O
,	O
with	O
caching	O
,	O
even	O
at	O
CPU	B-General_Concept
speed	O
)	O
,	O
which	O
,	O
compared	O
to	O
disk	O
speed	O
,	O
is	O
virtually	O
instantaneous	O
.	O
</s>
<s>
For	O
example	O
,	O
the	O
popular	O
recursive	O
quicksort	B-Algorithm
algorithm	O
provides	O
quite	O
reasonable	O
performance	O
with	O
adequate	O
RAM	O
,	O
but	O
due	O
to	O
the	O
recursive	O
way	O
that	O
it	O
copies	O
portions	O
of	O
the	O
array	O
it	O
becomes	O
much	O
less	O
practical	O
when	O
the	O
array	O
does	O
not	O
fit	O
in	O
RAM	O
,	O
because	O
it	O
may	O
cause	O
a	O
number	O
of	O
slow	O
copy	O
or	O
move	O
operations	O
to	O
and	O
from	O
disk	O
.	O
</s>
<s>
One	O
way	O
to	O
work	O
around	O
this	O
problem	O
,	O
which	O
works	O
well	O
when	O
complex	O
records	O
(	O
such	O
as	O
in	O
a	O
relational	B-Application
database	I-Application
)	O
are	O
being	O
sorted	O
by	O
a	O
relatively	O
small	O
key	O
field	O
,	O
is	O
to	O
create	O
an	O
index	O
into	O
the	O
array	O
and	O
then	O
sort	O
the	O
index	O
,	O
rather	O
than	O
the	O
entire	O
array	O
.	O
</s>
<s>
Because	O
the	O
index	O
is	O
much	O
smaller	O
than	O
the	O
entire	O
array	O
,	O
it	O
may	O
fit	O
easily	O
in	O
memory	B-General_Concept
where	O
the	O
entire	O
array	O
would	O
not	O
,	O
effectively	O
eliminating	O
the	O
disk-swapping	O
problem	O
.	O
</s>
<s>
This	O
procedure	O
is	O
sometimes	O
called	O
"	O
tag	B-Algorithm
sort	I-Algorithm
"	O
.	O
</s>
<s>
Another	O
technique	O
for	O
overcoming	O
the	O
memory-size	O
problem	O
is	O
using	O
external	B-Algorithm
sorting	I-Algorithm
,	O
for	O
example	O
one	O
of	O
the	O
ways	O
is	O
to	O
combine	O
two	O
algorithms	O
in	O
a	O
way	O
that	O
takes	O
advantage	O
of	O
the	O
strength	O
of	O
each	O
to	O
improve	O
overall	O
performance	O
.	O
</s>
<s>
For	O
instance	O
,	O
the	O
array	O
might	O
be	O
subdivided	O
into	O
chunks	O
of	O
a	O
size	O
that	O
will	O
fit	O
in	O
RAM	O
,	O
the	O
contents	O
of	O
each	O
chunk	O
sorted	O
using	O
an	O
efficient	B-General_Concept
algorithm	I-General_Concept
(	O
such	O
as	O
quicksort	B-Algorithm
)	O
,	O
and	O
the	O
results	O
merged	O
using	O
a	O
k-way	O
merge	B-Algorithm
similar	O
to	O
that	O
used	O
in	O
merge	B-Algorithm
sort	I-Algorithm
.	O
</s>
<s>
This	O
is	O
faster	O
than	O
performing	O
either	O
merge	B-Algorithm
sort	I-Algorithm
or	O
quicksort	B-Algorithm
over	O
the	O
entire	O
list	O
.	O
</s>
<s>
For	O
sorting	B-Algorithm
very	O
large	O
sets	O
of	O
data	O
that	O
vastly	O
exceed	O
system	O
memory	B-General_Concept
,	O
even	O
the	O
index	O
may	O
need	O
to	O
be	O
sorted	O
using	O
an	O
algorithm	O
or	O
combination	O
of	O
algorithms	O
designed	O
to	O
perform	O
reasonably	O
with	O
virtual	B-Architecture
memory	I-Architecture
,	O
i.e.	O
,	O
to	O
reduce	O
the	O
amount	O
of	O
swapping	O
required	O
.	O
</s>
<s>
Related	O
problems	O
include	O
approximate	O
sorting	B-Algorithm
(	O
sorting	B-Algorithm
a	O
sequence	O
to	O
within	O
a	O
certain	O
amount	O
of	O
the	O
correct	O
order	O
)	O
,	O
partial	B-Algorithm
sorting	I-Algorithm
(	O
sorting	B-Algorithm
only	O
the	O
k	O
smallest	O
elements	O
of	O
a	O
list	O
,	O
or	O
finding	O
the	O
k	O
smallest	O
elements	O
,	O
but	O
unordered	O
)	O
and	O
selection	B-Algorithm
(	O
computing	O
the	O
kth	O
smallest	O
element	O
)	O
.	O
</s>
<s>
These	O
can	O
be	O
solved	O
inefficiently	O
by	O
a	O
total	O
sort	O
,	O
but	O
more	O
efficient	B-General_Concept
algorithms	I-General_Concept
exist	O
,	O
often	O
derived	O
by	O
generalizing	O
a	O
sorting	B-Algorithm
algorithm	I-Algorithm
.	O
</s>
<s>
The	O
most	O
notable	O
example	O
is	O
quickselect	B-Algorithm
,	O
which	O
is	O
related	O
to	O
quicksort	B-Algorithm
.	O
</s>
<s>
Conversely	O
,	O
some	O
sorting	B-Algorithm
algorithms	I-Algorithm
can	O
be	O
derived	O
by	O
repeated	O
application	O
of	O
a	O
selection	B-Algorithm
algorithm	I-Algorithm
;	O
quicksort	B-Algorithm
and	O
quickselect	B-Algorithm
can	O
be	O
seen	O
as	O
the	O
same	O
pivoting	O
move	O
,	O
differing	O
only	O
in	O
whether	O
one	O
recurses	O
on	O
both	O
sides	O
(	O
quicksort	B-Algorithm
,	O
divide-and-conquer	B-Algorithm
)	O
or	O
one	O
side	O
(	O
quickselect	B-Algorithm
,	O
decrease-and-conquer	B-Algorithm
)	O
.	O
</s>
<s>
A	O
kind	O
of	O
opposite	O
of	O
a	O
sorting	B-Algorithm
algorithm	I-Algorithm
is	O
a	O
shuffling	B-Algorithm
algorithm	I-Algorithm
.	O
</s>
<s>
Shuffling	O
can	O
also	O
be	O
implemented	O
by	O
a	O
sorting	B-Algorithm
algorithm	I-Algorithm
,	O
namely	O
by	O
a	O
random	O
sort	O
:	O
assigning	O
a	O
random	O
number	O
to	O
each	O
element	O
of	O
the	O
list	O
and	O
then	O
sorting	B-Algorithm
based	O
on	O
the	O
random	O
numbers	O
.	O
</s>
<s>
This	O
is	O
generally	O
not	O
done	O
in	O
practice	O
,	O
however	O
,	O
and	O
there	O
is	O
a	O
well-known	O
simple	O
and	O
efficient	B-General_Concept
algorithm	I-General_Concept
for	O
shuffling	O
:	O
the	O
Fisher	B-Algorithm
–	I-Algorithm
Yates	I-Algorithm
shuffle	I-Algorithm
.	O
</s>
<s>
Sorting	B-Algorithm
algorithms	I-Algorithm
are	O
ineffective	O
for	O
finding	O
an	O
order	O
in	O
many	O
situations	O
.	O
</s>
<s>
Usually	O
when	O
elements	O
have	O
no	O
reliable	O
comparison	O
function	O
(	O
crowdsourced	O
preferences	O
like	O
voting	O
systems	O
)	O
,	O
comparisons	O
are	O
very	O
costly	O
(	O
sports	O
)	O
,	O
or	O
when	O
it	O
would	O
be	O
impossible	O
to	O
pairwise	O
compare	O
all	O
elements	O
for	O
all	O
criteria	O
(	O
search	B-Application
engines	O
)	O
.	O
</s>
<s>
A	O
common	O
example	O
is	O
in	O
chess	O
,	O
where	O
players	O
are	O
ranked	O
with	O
the	O
Elo	O
rating	O
system	O
,	O
and	O
rankings	O
are	O
determined	O
by	O
a	O
tournament	O
system	O
instead	O
of	O
a	O
sorting	B-Algorithm
algorithm	I-Algorithm
.	O
</s>
