<s>
Cycle	B-Language
sort	I-Language
is	O
an	O
in-place	O
,	O
unstable	O
sorting	B-Algorithm
algorithm	I-Algorithm
,	O
a	O
comparison	B-Algorithm
sort	I-Algorithm
that	O
is	O
theoretically	O
optimal	O
in	O
terms	O
of	O
the	O
total	O
number	O
of	O
writes	O
to	O
the	O
original	O
array	B-Data_Structure
,	O
unlike	O
any	O
other	O
in-place	O
sorting	B-Algorithm
algorithm	I-Algorithm
.	O
</s>
<s>
It	O
is	O
based	O
on	O
the	O
idea	O
that	O
the	O
permutation	B-Algorithm
to	O
be	O
sorted	O
can	O
be	O
factored	O
into	O
cycles	O
,	O
which	O
can	O
individually	O
be	O
rotated	O
to	O
give	O
a	O
sorted	O
result	O
.	O
</s>
<s>
Unlike	O
nearly	O
every	O
other	O
sort	O
,	O
items	O
are	O
never	O
written	O
elsewhere	O
in	O
the	O
array	B-Data_Structure
simply	O
to	O
push	O
them	O
out	O
of	O
the	O
way	O
of	O
the	O
action	O
.	O
</s>
<s>
Minimizing	O
the	O
number	O
of	O
writes	O
is	O
useful	O
when	O
making	O
writes	O
to	O
some	O
huge	O
data	O
set	O
is	O
very	O
expensive	O
,	O
such	O
as	O
with	O
EEPROMs	B-General_Concept
like	O
Flash	B-Device
memory	I-Device
where	O
each	O
write	O
reduces	O
the	O
lifespan	O
of	O
the	O
memory	O
.	O
</s>
<s>
To	O
illustrate	O
the	O
idea	O
of	O
cycle	B-Language
sort	I-Language
,	O
consider	O
a	O
list	O
with	O
distinct	O
elements	O
.	O
</s>
<s>
Given	O
an	O
element	O
,	O
we	O
can	O
find	O
the	O
index	O
at	O
which	O
it	O
will	O
occur	O
in	O
the	O
sorted	B-Algorithm
list	I-Algorithm
by	O
simply	O
counting	O
the	O
number	O
of	O
elements	O
in	O
the	O
entire	O
list	O
that	O
are	O
smaller	O
than	O
.	O
</s>
<s>
This	O
completes	O
a	O
cycle	B-Algorithm
.	O
</s>
<s>
When	O
computing	O
the	O
correct	O
positions	O
,	O
we	O
have	O
to	O
make	O
sure	O
not	O
to	O
double-count	O
the	O
first	O
element	O
of	O
the	O
cycle	B-Algorithm
.	O
</s>
<s>
Simply	O
swapping	O
these	O
would	O
cause	O
the	O
algorithm	O
to	O
cycle	B-Algorithm
indefinitely	O
.	O
</s>
<s>
The	O
following	O
Python	B-Language
implementation	O
performs	O
cycle	B-Language
sort	I-Language
on	O
an	O
array	B-Data_Structure
,	O
counting	O
the	O
number	O
of	O
writes	O
to	O
that	O
array	B-Data_Structure
that	O
were	O
needed	O
to	O
sort	O
it	O
.	O
</s>
<s>
The	O
next	O
implementation	O
written	O
in	O
C++	B-Language
simply	O
performs	O
cyclic	O
array	B-Data_Structure
sorting	B-Algorithm
.	O
</s>
<s>
When	O
the	O
array	B-Data_Structure
contains	O
only	O
duplicates	O
of	O
a	O
relatively	O
small	O
number	O
of	O
items	O
,	O
a	O
constant-time	O
perfect	B-Algorithm
hash	I-Algorithm
function	I-Algorithm
can	O
greatly	O
speed	O
up	O
finding	O
where	O
to	O
put	O
an	O
item	O
,	O
turning	O
the	O
sort	O
from	O
Θ(n2 )	O
time	O
to	O
Θ( n	O
+	O
k	O
)	O
time	O
,	O
where	O
k	O
is	O
the	O
total	O
number	O
of	O
hashes	O
.	O
</s>
<s>
The	O
array	B-Data_Structure
ends	O
up	O
sorted	O
in	O
the	O
order	O
of	O
the	O
hashes	O
,	O
so	O
choosing	O
a	O
hash	O
function	O
that	O
gives	O
you	O
the	O
right	O
ordering	O
is	O
important	O
.	O
</s>
<s>
Before	O
the	O
sort	O
,	O
create	O
a	O
histogram	B-Algorithm
,	O
sorted	O
by	O
hash	O
,	O
counting	O
the	O
number	O
of	O
occurrences	O
of	O
each	O
hash	O
in	O
the	O
array	B-Data_Structure
.	O
</s>
<s>
Then	O
create	O
a	O
table	O
with	O
the	O
cumulative	O
sum	O
of	O
each	O
entry	O
in	O
the	O
histogram	B-Algorithm
.	O
</s>
<s>
The	O
cumulative	O
sum	O
table	O
will	O
then	O
contain	O
the	O
position	O
in	O
the	O
array	B-Data_Structure
of	O
each	O
element	O
.	O
</s>
<s>
The	O
proper	O
place	O
of	O
elements	O
can	O
then	O
be	O
found	O
by	O
a	O
constant-time	O
hashing	O
and	O
cumulative	O
sum	O
table	O
lookup	O
rather	O
than	O
a	O
linear	B-Algorithm
search	I-Algorithm
.	O
</s>
