<s>
In	O
computer	B-General_Concept
science	I-General_Concept
,	O
counting	B-Algorithm
sort	I-Algorithm
is	O
an	O
algorithm	O
for	O
sorting	B-Algorithm
a	O
collection	B-Application
of	O
objects	O
according	O
to	O
keys	O
that	O
are	O
small	O
positive	O
integers	O
;	O
that	O
is	O
,	O
it	O
is	O
an	O
integer	B-Algorithm
sorting	I-Algorithm
algorithm	O
.	O
</s>
<s>
It	O
operates	O
by	O
counting	O
the	O
number	O
of	O
objects	O
that	O
possess	O
distinct	O
key	O
values	O
,	O
and	O
applying	O
prefix	B-Application
sum	I-Application
on	O
those	O
counts	O
to	O
determine	O
the	O
positions	O
of	O
each	O
key	O
value	O
in	O
the	O
output	O
sequence	O
.	O
</s>
<s>
It	O
is	O
often	O
used	O
as	O
a	O
subroutine	O
in	O
radix	B-Algorithm
sort	I-Algorithm
,	O
another	O
sorting	B-Algorithm
algorithm	I-Algorithm
,	O
which	O
can	O
handle	O
larger	O
keys	O
more	O
efficiently	O
.	O
</s>
<s>
Counting	B-Algorithm
sort	I-Algorithm
is	O
not	O
a	O
comparison	B-Algorithm
sort	I-Algorithm
;	O
it	O
uses	O
key	O
values	O
as	O
indexes	O
into	O
an	O
array	B-Data_Structure
and	O
the	O
lower	O
bound	O
for	O
comparison	B-Algorithm
sorting	I-Algorithm
will	O
not	O
apply	O
.	O
</s>
<s>
Bucket	B-Algorithm
sort	I-Algorithm
may	O
be	O
used	O
in	O
lieu	O
of	O
counting	B-Algorithm
sort	I-Algorithm
,	O
and	O
entails	O
a	O
similar	O
time	O
analysis	O
.	O
</s>
<s>
However	O
,	O
compared	O
to	O
counting	B-Algorithm
sort	I-Algorithm
,	O
bucket	B-Algorithm
sort	I-Algorithm
requires	O
linked	B-Data_Structure
lists	I-Data_Structure
,	O
dynamic	B-Data_Structure
arrays	I-Data_Structure
,	O
or	O
a	O
large	O
amount	O
of	O
pre-allocated	O
memory	O
to	O
hold	O
the	O
sets	O
of	O
items	O
within	O
each	O
bucket	O
,	O
whereas	O
counting	B-Algorithm
sort	I-Algorithm
stores	O
a	O
single	O
number	O
(	O
the	O
count	O
of	O
items	O
)	O
per	O
bucket	O
.	O
</s>
<s>
In	O
the	O
most	O
general	O
case	O
,	O
the	O
input	O
to	O
counting	B-Algorithm
sort	I-Algorithm
consists	O
of	O
a	O
collection	B-Application
of	O
items	O
,	O
each	O
of	O
which	O
has	O
a	O
non-negative	O
integer	O
key	O
whose	O
maximum	O
value	O
is	O
at	O
most	O
.	O
</s>
<s>
In	O
some	O
descriptions	O
of	O
counting	B-Algorithm
sort	I-Algorithm
,	O
the	O
input	O
to	O
be	O
sorted	O
is	O
assumed	O
to	O
be	O
more	O
simply	O
a	O
sequence	O
of	O
integers	O
itself	O
,	O
but	O
this	O
simplification	O
does	O
not	O
accommodate	O
many	O
applications	O
of	O
counting	B-Algorithm
sort	I-Algorithm
.	O
</s>
<s>
For	O
instance	O
,	O
when	O
used	O
as	O
a	O
subroutine	O
in	O
radix	B-Algorithm
sort	I-Algorithm
,	O
the	O
keys	O
for	O
each	O
call	O
to	O
counting	B-Algorithm
sort	I-Algorithm
are	O
individual	O
digits	O
of	O
larger	O
item	O
keys	O
;	O
it	O
would	O
not	O
suffice	O
to	O
return	O
only	O
a	O
sorted	B-Algorithm
list	I-Algorithm
of	O
the	O
key	O
digits	O
,	O
separated	O
from	O
the	O
items	O
.	O
</s>
<s>
In	O
applications	O
such	O
as	O
in	O
radix	B-Algorithm
sort	I-Algorithm
,	O
a	O
bound	O
on	O
the	O
maximum	O
key	O
value	O
will	O
be	O
known	O
in	O
advance	O
,	O
and	O
can	O
be	O
assumed	O
to	O
be	O
part	O
of	O
the	O
input	O
to	O
the	O
algorithm	O
.	O
</s>
<s>
The	O
output	O
is	O
an	O
array	B-Data_Structure
of	O
the	O
elements	O
ordered	O
by	O
their	O
keys	O
.	O
</s>
<s>
Because	O
of	O
its	O
application	O
to	O
radix	B-Algorithm
sorting	I-Algorithm
,	O
counting	B-Algorithm
sort	I-Algorithm
must	O
be	O
a	O
stable	O
sort	O
;	O
that	O
is	O
,	O
if	O
two	O
elements	O
share	O
the	O
same	O
key	O
,	O
their	O
relative	O
order	O
in	O
the	O
output	O
array	B-Data_Structure
and	O
their	O
relative	O
order	O
in	O
the	O
input	O
array	B-Data_Structure
should	O
match	O
.	O
</s>
<s>
Here	O
input	O
is	O
the	O
input	O
array	B-Data_Structure
to	O
be	O
sorted	O
,	O
key	O
returns	O
the	O
numeric	O
key	O
of	O
each	O
item	O
in	O
the	O
input	O
array	B-Data_Structure
,	O
count	O
is	O
an	O
auxiliary	O
array	B-Data_Structure
used	O
first	O
to	O
store	O
the	O
numbers	O
of	O
items	O
with	O
each	O
key	O
,	O
and	O
then	O
(	O
after	O
the	O
second	O
loop	O
)	O
to	O
store	O
the	O
positions	O
where	O
items	O
with	O
each	O
key	O
should	O
be	O
placed	O
,	O
</s>
<s>
k	O
is	O
the	O
maximum	O
value	O
of	O
the	O
non-negative	O
key	O
values	O
and	O
output	O
is	O
the	O
sorted	O
output	O
array	B-Data_Structure
.	O
</s>
<s>
In	O
summary	O
,	O
the	O
algorithm	O
loops	O
over	O
the	O
items	O
in	O
the	O
first	O
loop	O
,	O
computing	O
a	O
histogram	B-Algorithm
of	O
the	O
number	O
of	O
times	O
each	O
key	O
occurs	O
within	O
the	O
input	O
collection	B-Application
.	O
</s>
<s>
After	O
that	O
in	O
the	O
second	O
loop	O
,	O
it	O
performs	O
a	O
prefix	B-Application
sum	I-Application
computation	O
on	O
count	O
in	O
order	O
to	O
determine	O
,	O
for	O
each	O
key	O
,	O
the	O
position	O
range	O
where	O
the	O
items	O
having	O
that	O
key	O
should	O
be	O
placed	O
;	O
i.e.	O
</s>
<s>
Finally	O
,	O
in	O
the	O
third	O
loop	O
,	O
it	O
loops	O
over	O
the	O
items	O
of	O
input	O
again	O
,	O
but	O
in	O
reverse	O
order	O
,	O
moving	O
each	O
item	O
into	O
its	O
sorted	O
position	O
in	O
the	O
output	O
array	B-Data_Structure
.	O
</s>
<s>
The	O
initialization	O
of	O
the	O
count	O
array	B-Data_Structure
,	O
and	O
the	O
second	O
for	O
loop	O
which	O
performs	O
a	O
prefix	B-Application
sum	I-Application
on	O
the	O
count	O
array	B-Data_Structure
,	O
each	O
iterate	O
at	O
most	O
times	O
and	O
therefore	O
take	O
time	O
.	O
</s>
<s>
The	O
other	O
two	O
for	O
loops	O
,	O
and	O
the	O
initialization	O
of	O
the	O
output	O
array	B-Data_Structure
,	O
each	O
take	O
time	O
.	O
</s>
<s>
For	O
problem	O
instances	O
in	O
which	O
the	O
maximum	O
key	O
value	O
is	O
significantly	O
smaller	O
than	O
the	O
number	O
of	O
items	O
,	O
counting	B-Algorithm
sort	I-Algorithm
can	O
be	O
highly	O
space-efficient	O
,	O
as	O
the	O
only	O
storage	O
it	O
uses	O
other	O
than	O
its	O
input	O
and	O
output	O
arrays	O
is	O
the	O
Count	O
array	B-Data_Structure
which	O
uses	O
space	O
.	O
</s>
<s>
If	O
each	O
item	O
to	O
be	O
sorted	O
is	O
itself	O
an	O
integer	O
,	O
and	O
used	O
as	O
key	O
as	O
well	O
,	O
then	O
the	O
second	O
and	O
third	O
loops	O
of	O
counting	B-Algorithm
sort	I-Algorithm
can	O
be	O
combined	O
;	O
in	O
the	O
second	O
loop	O
,	O
instead	O
of	O
computing	O
the	O
position	O
where	O
items	O
with	O
key	O
i	O
should	O
be	O
placed	O
in	O
the	O
output	O
,	O
simply	O
append	O
Count[i]	O
copies	O
of	O
the	O
number	O
i	O
to	O
the	O
output	O
.	O
</s>
<s>
This	O
algorithm	O
may	O
also	O
be	O
used	O
to	O
eliminate	O
duplicate	O
keys	O
,	O
by	O
replacing	O
the	O
Count	O
array	B-Data_Structure
with	O
a	O
bit	B-Data_Structure
vector	I-Data_Structure
that	O
stores	O
a	O
one	O
for	O
a	O
key	O
that	O
is	O
present	O
in	O
the	O
input	O
and	O
a	O
zero	O
for	O
a	O
key	O
that	O
is	O
not	O
present	O
.	O
</s>
<s>
If	O
additionally	O
the	O
items	O
are	O
the	O
integer	O
keys	O
themselves	O
,	O
both	O
second	O
and	O
third	O
loops	O
can	O
be	O
omitted	O
entirely	O
and	O
the	O
bit	B-Data_Structure
vector	I-Data_Structure
will	O
itself	O
serve	O
as	O
output	O
,	O
representing	O
the	O
values	O
as	O
offsets	O
of	O
the	O
non-zero	O
entries	O
,	O
added	O
to	O
the	O
range	O
's	O
lowest	O
value	O
.	O
</s>
<s>
Thus	O
the	O
keys	O
are	O
sorted	O
and	O
the	O
duplicates	O
are	O
eliminated	O
in	O
this	O
variant	O
just	O
by	O
being	O
placed	O
into	O
the	O
bit	B-Data_Structure
array	I-Data_Structure
.	O
</s>
<s>
For	O
data	O
in	O
which	O
the	O
maximum	O
key	O
size	O
is	O
significantly	O
smaller	O
than	O
the	O
number	O
of	O
data	O
items	O
,	O
counting	B-Algorithm
sort	I-Algorithm
may	O
be	O
parallelized	B-Operating_System
by	O
splitting	O
the	O
input	O
into	O
subarrays	O
of	O
approximately	O
equal	O
size	O
,	O
processing	O
each	O
subarray	O
in	O
parallel	O
to	O
generate	O
a	O
separate	O
count	O
array	B-Data_Structure
for	O
each	O
subarray	O
,	O
and	O
then	O
merging	O
the	O
count	O
arrays	O
.	O
</s>
<s>
When	O
used	O
as	O
part	O
of	O
a	O
parallel	O
radix	B-Algorithm
sort	I-Algorithm
algorithm	O
,	O
the	O
key	O
size	O
(	O
base	O
of	O
the	O
radix	O
representation	O
)	O
should	O
be	O
chosen	O
to	O
match	O
the	O
size	O
of	O
the	O
split	O
subarrays	O
.	O
</s>
<s>
The	O
simplicity	O
of	O
the	O
counting	B-Algorithm
sort	I-Algorithm
algorithm	O
and	O
its	O
use	O
of	O
the	O
easily	O
parallelizable	O
prefix	B-Application
sum	I-Application
primitive	O
also	O
make	O
it	O
usable	O
in	O
more	O
fine-grained	O
parallel	B-Operating_System
algorithms	I-Operating_System
.	O
</s>
<s>
As	O
described	O
,	O
counting	B-Algorithm
sort	I-Algorithm
is	O
not	O
an	O
in-place	B-Algorithm
algorithm	I-Algorithm
;	O
even	O
disregarding	O
the	O
count	O
array	B-Data_Structure
,	O
it	O
needs	O
separate	O
input	O
and	O
output	O
arrays	O
.	O
</s>
<s>
It	O
is	O
possible	O
to	O
modify	O
the	O
algorithm	O
so	O
that	O
it	O
places	O
the	O
items	O
into	O
sorted	O
order	O
within	O
the	O
same	O
array	B-Data_Structure
that	O
was	O
given	O
to	O
it	O
as	O
the	O
input	O
,	O
using	O
only	O
the	O
count	O
array	B-Data_Structure
as	O
auxiliary	O
storage	O
;	O
however	O
,	O
the	O
modified	O
in-place	B-Algorithm
version	O
of	O
counting	B-Algorithm
sort	I-Algorithm
is	O
not	O
stable	O
.	O
</s>
<s>
Although	O
radix	B-Algorithm
sorting	I-Algorithm
itself	O
dates	O
back	O
far	O
longer	O
,	O
</s>
<s>
counting	B-Algorithm
sort	I-Algorithm
,	O
and	O
its	O
application	O
to	O
radix	B-Algorithm
sorting	I-Algorithm
,	O
were	O
both	O
invented	O
by	O
Harold	O
H	O
.	O
Seward	O
in	O
1954	O
.	O
</s>
