<s>
Bucket	B-Algorithm
sort	I-Algorithm
,	O
or	O
bin	B-Algorithm
sort	I-Algorithm
,	O
is	O
a	O
sorting	B-Algorithm
algorithm	I-Algorithm
that	O
works	O
by	O
distributing	O
the	O
elements	O
of	O
an	O
array	B-Data_Structure
into	O
a	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>
It	O
is	O
a	O
distribution	O
sort	O
,	O
a	O
generalization	O
of	O
pigeonhole	B-Algorithm
sort	I-Algorithm
that	O
allows	O
multiple	O
keys	O
per	O
bucket	O
,	O
and	O
is	O
a	O
cousin	O
of	O
radix	B-Algorithm
sort	I-Algorithm
in	O
the	O
most-to-least	O
significant	O
digit	O
flavor	O
.	O
</s>
<s>
Bucket	B-Algorithm
sort	I-Algorithm
can	O
be	O
implemented	O
with	O
comparisons	O
and	O
therefore	O
can	O
also	O
be	O
considered	O
a	O
comparison	B-Algorithm
sort	I-Algorithm
algorithm	O
.	O
</s>
<s>
The	O
computational	B-General_Concept
complexity	I-General_Concept
depends	O
on	O
the	O
algorithm	O
used	O
to	O
sort	O
each	O
bucket	O
,	O
the	O
number	O
of	O
buckets	O
to	O
use	O
,	O
and	O
whether	O
the	O
input	O
is	O
uniformly	O
distributed	O
.	O
</s>
<s>
Bucket	B-Algorithm
sort	I-Algorithm
works	O
as	O
follows	O
:	O
</s>
<s>
Set	O
up	O
an	O
array	B-Data_Structure
of	O
initially	O
empty	O
"	O
buckets	O
"	O
.	O
</s>
<s>
Scatter	O
:	O
Go	O
over	O
the	O
original	O
array	B-Data_Structure
,	O
putting	O
each	O
object	O
in	O
its	O
bucket	O
.	O
</s>
<s>
Gather	O
:	O
Visit	O
the	O
buckets	O
in	O
order	O
and	O
put	O
all	O
elements	O
back	O
into	O
the	O
original	O
array	B-Data_Structure
.	O
</s>
<s>
Let	O
array	B-Data_Structure
denote	O
the	O
array	B-Data_Structure
to	O
be	O
sorted	O
and	O
k	O
denote	O
the	O
number	O
of	O
buckets	O
to	O
use	O
.	O
</s>
<s>
The	O
function	O
nextSort	O
is	O
a	O
sorting	B-Algorithm
function	O
used	O
to	O
sort	O
each	O
bucket	O
.	O
</s>
<s>
Conventionally	O
,	O
insertion	B-Algorithm
sort	I-Algorithm
is	O
used	O
,	O
but	O
other	O
algorithms	O
could	O
be	O
used	O
as	O
well	O
,	O
such	O
as	O
selection	B-Algorithm
sort	I-Algorithm
or	O
merge	B-Algorithm
sort	I-Algorithm
.	O
</s>
<s>
Using	O
bucketSort	B-Algorithm
itself	O
as	O
nextSort	O
produces	O
a	O
relative	O
of	O
radix	B-Algorithm
sort	I-Algorithm
;	O
in	O
particular	O
,	O
the	O
case	O
n	O
=	O
2	O
corresponds	O
to	O
quicksort	B-Algorithm
(	O
although	O
potentially	O
with	O
poor	O
pivot	O
choices	O
)	O
.	O
</s>
<s>
The	O
overall	O
performance	O
would	O
then	O
be	O
dominated	O
by	O
the	O
algorithm	O
used	O
to	O
sort	O
each	O
bucket	O
,	O
for	O
example	O
insertion	B-Algorithm
sort	I-Algorithm
or	O
comparison	B-Algorithm
sort	I-Algorithm
algorithms	O
,	O
such	O
as	O
merge	B-Algorithm
sort	I-Algorithm
.	O
</s>
<s>
The	O
first	O
step	O
,	O
which	O
is	O
initialize	O
the	O
buckets	O
and	O
find	O
the	O
maximum	O
key	O
value	O
in	O
the	O
array	B-Data_Structure
,	O
can	O
be	O
done	O
in	O
time	O
.	O
</s>
<s>
Assume	O
insertion	B-Algorithm
sort	I-Algorithm
is	O
used	O
to	O
sort	O
each	O
bucket	O
,	O
then	O
the	O
third	O
step	O
costs	O
,	O
where	O
is	O
the	O
length	O
of	O
the	O
bucket	O
indexed	O
.	O
</s>
<s>
The	O
last	O
step	O
of	O
bucket	B-Algorithm
sort	I-Algorithm
,	O
which	O
is	O
concatenating	O
all	O
the	O
sorted	O
objects	O
in	O
each	O
buckets	O
,	O
requires	O
time	O
.	O
</s>
<s>
Note	O
that	O
if	O
k	O
is	O
chosen	O
to	O
be	O
,	O
then	O
bucket	B-Algorithm
sort	I-Algorithm
runs	O
in	O
average	O
time	O
,	O
given	O
a	O
uniformly	O
distributed	O
input	O
.	O
</s>
<s>
A	O
common	O
optimization	O
is	O
to	O
put	O
the	O
unsorted	O
elements	O
of	O
the	O
buckets	O
back	O
in	O
the	O
original	O
array	B-Data_Structure
first	O
,	O
then	O
run	O
insertion	B-Algorithm
sort	I-Algorithm
over	O
the	O
complete	O
array	B-Data_Structure
;	O
because	O
insertion	B-Algorithm
sort	I-Algorithm
's	O
runtime	O
is	O
based	O
on	O
how	O
far	O
each	O
element	O
is	O
from	O
its	O
final	O
position	O
,	O
the	O
number	O
of	O
comparisons	O
remains	O
relatively	O
small	O
,	O
and	O
the	O
memory	O
hierarchy	O
is	O
better	O
exploited	O
by	O
storing	O
the	O
list	O
contiguously	O
in	O
memory	O
.	O
</s>
<s>
The	O
most	O
common	O
variant	O
of	O
bucket	B-Algorithm
sort	I-Algorithm
operates	O
on	O
a	O
list	O
of	O
n	O
numeric	O
inputs	O
between	O
zero	O
and	O
some	O
maximum	O
value	O
M	O
and	O
divides	O
the	O
value	O
range	O
into	O
n	O
buckets	O
each	O
of	O
size	O
M/n	O
.	O
</s>
<s>
If	O
each	O
bucket	O
is	O
sorted	O
using	O
insertion	B-Algorithm
sort	I-Algorithm
,	O
the	O
sort	O
can	O
be	O
shown	O
to	O
run	O
in	O
expected	O
linear	O
time	O
(	O
where	O
the	O
average	O
is	O
taken	O
over	O
all	O
possible	O
inputs	O
)	O
.	O
</s>
<s>
This	O
performance	O
degradation	O
is	O
avoided	O
in	O
the	O
original	O
bucket	B-Algorithm
sort	I-Algorithm
algorithm	O
by	O
assuming	O
that	O
the	O
input	O
is	O
generated	O
by	O
a	O
random	O
process	O
that	O
distributes	O
elements	O
uniformly	O
over	O
the	O
interval	O
[	O
0	O
,	O
1	O
)	O
.	O
</s>
<s>
Similar	O
to	O
generic	O
bucket	B-Algorithm
sort	I-Algorithm
as	O
described	O
above	O
,	O
ProxmapSort	O
works	O
by	O
dividing	O
an	O
array	B-Data_Structure
of	O
keys	O
into	O
subarrays	O
via	O
the	O
use	O
of	O
a	O
"	O
map	O
key	O
"	O
function	O
that	O
preserves	O
a	O
partial	O
ordering	O
on	O
the	O
keys	O
;	O
as	O
each	O
key	O
is	O
added	O
to	O
its	O
subarray	O
,	O
insertion	B-Algorithm
sort	I-Algorithm
is	O
used	O
to	O
keep	O
that	O
subarray	O
sorted	O
,	O
resulting	O
in	O
the	O
entire	O
array	B-Data_Structure
being	O
in	O
sorted	O
order	O
when	O
ProxmapSort	O
completes	O
.	O
</s>
<s>
ProxmapSort	O
differs	O
from	O
bucket	B-Algorithm
sorts	I-Algorithm
in	O
its	O
use	O
of	O
the	O
map	O
key	O
to	O
place	O
the	O
data	O
approximately	O
where	O
it	O
belongs	O
in	O
sorted	O
order	O
,	O
producing	O
a	O
"	O
proxmap	O
"	O
—	O
a	O
proximity	O
mapping	O
—	O
of	O
the	O
keys	O
.	O
</s>
<s>
Another	O
variant	O
of	O
bucket	B-Algorithm
sort	I-Algorithm
known	O
as	O
histogram	B-Algorithm
sort	I-Algorithm
or	O
counting	B-Algorithm
sort	I-Algorithm
adds	O
an	O
initial	O
pass	O
that	O
counts	O
the	O
number	O
of	O
elements	O
that	O
will	O
fall	O
into	O
each	O
bucket	O
using	O
a	O
count	O
array	B-Data_Structure
.	O
</s>
<s>
Using	O
this	O
information	O
,	O
the	O
array	B-Data_Structure
values	O
can	O
be	O
arranged	O
into	O
a	O
sequence	O
of	O
buckets	O
in-place	O
by	O
a	O
sequence	O
of	O
exchanges	O
,	O
leaving	O
no	O
space	O
overhead	O
for	O
bucket	O
storage	O
.	O
</s>
<s>
The	O
Postman	B-Algorithm
's	I-Algorithm
sort	I-Algorithm
is	O
a	O
variant	O
of	O
bucket	B-Algorithm
sort	I-Algorithm
that	O
takes	O
advantage	O
of	O
a	O
hierarchical	O
structure	O
of	O
elements	O
,	O
typically	O
described	O
by	O
a	O
set	O
of	O
attributes	O
.	O
</s>
<s>
This	O
is	O
the	O
algorithm	O
used	O
by	O
letter-sorting	O
machines	O
in	O
post	O
offices	O
:	O
mail	O
is	O
sorted	O
first	O
between	O
domestic	O
and	O
international	O
;	O
then	O
by	O
state	O
,	O
province	O
or	O
territory	O
;	O
then	O
by	O
destination	O
post	O
office	O
;	O
then	O
by	O
routes	O
,	O
etc	O
.	O
</s>
<s>
Since	O
keys	O
are	O
not	O
compared	O
against	O
each	O
other	O
,	O
sorting	B-Algorithm
time	O
is	O
O(cn )	O
,	O
where	O
c	O
depends	O
on	O
the	O
size	O
of	O
the	O
key	O
and	O
number	O
of	O
buckets	O
.	O
</s>
<s>
This	O
is	O
similar	O
to	O
a	O
radix	B-Algorithm
sort	I-Algorithm
that	O
works	O
"	O
top	O
down	O
,	O
"	O
or	O
"	O
most	O
significant	O
digit	O
first.	O
"	O
</s>
<s>
The	O
shuffle	O
sort	O
is	O
a	O
variant	O
of	O
bucket	B-Algorithm
sort	I-Algorithm
that	O
begins	O
by	O
removing	O
the	O
first	O
1/8	O
of	O
the	O
n	O
items	O
to	O
be	O
sorted	O
,	O
sorts	O
them	O
recursively	O
,	O
and	O
puts	O
them	O
in	O
an	O
array	B-Data_Structure
.	O
</s>
<s>
Each	O
"	O
bucket	O
"	O
is	O
then	O
sorted	O
,	O
and	O
the	O
"	O
buckets	O
"	O
are	O
concatenated	O
into	O
a	O
sorted	O
array	B-Data_Structure
.	O
</s>
<s>
Bucket	B-Algorithm
sort	I-Algorithm
can	O
be	O
seen	O
as	O
a	O
generalization	O
of	O
counting	B-Algorithm
sort	I-Algorithm
;	O
in	O
fact	O
,	O
if	O
each	O
bucket	O
has	O
size	O
1	O
then	O
bucket	B-Algorithm
sort	I-Algorithm
degenerates	O
to	O
counting	B-Algorithm
sort	I-Algorithm
.	O
</s>
<s>
The	O
variable	O
bucket	O
size	O
of	O
bucket	B-Algorithm
sort	I-Algorithm
allows	O
it	O
to	O
use	O
O(n )	O
memory	O
instead	O
of	O
O(M )	O
memory	O
,	O
where	O
M	O
is	O
the	O
number	O
of	O
distinct	O
values	O
;	O
in	O
exchange	O
,	O
it	O
gives	O
up	O
counting	B-Algorithm
sort	I-Algorithm
's	O
O( n	O
+	O
M	O
)	O
worst-case	O
behavior	O
.	O
</s>
<s>
Bucket	B-Algorithm
sort	I-Algorithm
with	O
two	O
buckets	O
is	O
effectively	O
a	O
version	O
of	O
quicksort	B-Algorithm
where	O
the	O
pivot	O
value	O
is	O
always	O
selected	O
to	O
be	O
the	O
middle	O
value	O
of	O
the	O
value	O
range	O
.	O
</s>
<s>
While	O
this	O
choice	O
is	O
effective	O
for	O
uniformly	O
distributed	O
inputs	O
,	O
other	O
means	O
of	O
choosing	O
the	O
pivot	O
in	O
quicksort	B-Algorithm
such	O
as	O
randomly	O
selected	O
pivots	O
make	O
it	O
more	O
resistant	O
to	O
clustering	O
in	O
the	O
input	O
distribution	O
.	O
</s>
<s>
The	O
n-way	O
mergesort	B-Algorithm
algorithm	O
also	O
begins	O
by	O
distributing	O
the	O
list	O
into	O
n	O
sublists	O
and	O
sorting	B-Algorithm
each	O
one	O
;	O
however	O
,	O
the	O
sublists	O
created	O
by	O
mergesort	B-Algorithm
have	O
overlapping	O
value	O
ranges	O
and	O
so	O
cannot	O
be	O
recombined	O
by	O
simple	O
concatenation	O
as	O
in	O
bucket	B-Algorithm
sort	I-Algorithm
.	O
</s>
<s>
Top-down	O
radix	B-Algorithm
sort	I-Algorithm
can	O
be	O
seen	O
as	O
a	O
special	O
case	O
of	O
bucket	B-Algorithm
sort	I-Algorithm
where	O
both	O
the	O
range	O
of	O
values	O
and	O
the	O
number	O
of	O
buckets	O
is	O
constrained	O
to	O
be	O
a	O
power	O
of	O
two	O
.	O
</s>
