<s>
The	O
Steinhaus	B-Algorithm
–	I-Algorithm
Johnson	I-Algorithm
–	I-Algorithm
Trotter	I-Algorithm
algorithm	I-Algorithm
or	O
Johnson	B-Algorithm
–	I-Algorithm
Trotter	I-Algorithm
algorithm	I-Algorithm
,	O
also	O
called	O
plain	B-Algorithm
changes	I-Algorithm
,	O
is	O
an	O
algorithm	O
named	O
after	O
Hugo	O
Steinhaus	O
,	O
Selmer	O
M	O
.	O
Johnson	O
and	O
Hale	O
F	O
.	O
Trotter	O
that	O
generates	O
all	O
of	O
the	O
permutations	B-Algorithm
of	O
elements	O
.	O
</s>
<s>
Each	O
permutation	B-Algorithm
in	O
the	O
sequence	O
that	O
it	O
generates	O
differs	O
from	O
the	O
previous	O
permutation	B-Algorithm
by	O
swapping	O
two	O
adjacent	O
elements	O
of	O
the	O
sequence	O
.	O
</s>
<s>
Equivalently	O
,	O
this	O
algorithm	O
finds	O
a	O
Hamiltonian	O
cycle	O
in	O
the	O
permutohedron	B-Algorithm
.	O
</s>
<s>
This	O
method	O
was	O
known	O
already	O
to	O
17th-century	O
English	O
change	O
ringers	O
,	O
and	O
calls	O
it	O
"	O
perhaps	O
the	O
most	O
prominent	O
permutation	B-Algorithm
enumeration	O
algorithm	O
"	O
.	O
</s>
<s>
A	O
version	O
of	O
the	O
algorithm	O
can	O
be	O
implemented	O
in	O
such	O
a	O
way	O
that	O
the	O
average	O
time	O
per	O
permutation	B-Algorithm
is	O
constant	O
.	O
</s>
<s>
As	O
well	O
as	O
being	O
simple	O
and	O
computationally	O
efficient	O
,	O
this	O
algorithm	O
has	O
the	O
advantage	O
that	O
subsequent	O
computations	O
on	O
the	O
permutations	B-Algorithm
that	O
it	O
generates	O
may	O
be	O
sped	O
up	O
because	O
of	O
the	O
similarity	O
between	O
consecutive	O
permutations	B-Algorithm
that	O
it	O
generates	O
.	O
</s>
<s>
The	O
sequence	O
of	O
permutations	B-Algorithm
generated	O
by	O
the	O
Steinhaus	B-Algorithm
–	I-Algorithm
Johnson	I-Algorithm
–	I-Algorithm
Trotter	I-Algorithm
algorithm	I-Algorithm
has	O
a	O
natural	O
recursive	O
structure	O
,	O
that	O
can	O
be	O
generated	O
by	O
a	O
recursive	O
algorithm	O
.	O
</s>
<s>
However	O
the	O
actual	O
Steinhaus	B-Algorithm
–	I-Algorithm
Johnson	I-Algorithm
–	I-Algorithm
Trotter	I-Algorithm
algorithm	I-Algorithm
does	O
not	O
use	O
recursion	O
,	O
instead	O
computing	O
the	O
same	O
sequence	O
of	O
permutations	B-Algorithm
by	O
a	O
simple	O
iterative	B-Algorithm
method	I-Algorithm
.	O
</s>
<s>
A	O
later	O
improvement	O
allows	O
it	O
to	O
run	O
in	O
constant	O
average	O
time	O
per	O
permutation	B-Algorithm
.	O
</s>
<s>
The	O
sequence	O
of	O
permutations	B-Algorithm
for	O
a	O
given	O
number	O
can	O
be	O
formed	O
from	O
the	O
sequence	O
of	O
permutations	B-Algorithm
for	O
by	O
placing	O
the	O
number	O
into	O
each	O
possible	O
position	O
in	O
each	O
of	O
the	O
shorter	O
permutations	B-Algorithm
.	O
</s>
<s>
The	O
Steinhaus	B-Algorithm
–	I-Algorithm
Johnson	I-Algorithm
–	I-Algorithm
Trotter	I-Algorithm
algorithm	I-Algorithm
follows	O
this	O
structure	O
:	O
the	O
sequence	O
of	O
permutations	B-Algorithm
it	O
generates	O
consists	O
of	O
blocks	O
of	O
permutations	B-Algorithm
,	O
so	O
that	O
within	O
each	O
block	O
the	O
permutations	B-Algorithm
agree	O
on	O
the	O
ordering	O
of	O
the	O
numbers	O
from	O
1	O
to	O
and	O
differ	O
only	O
in	O
the	O
position	O
of	O
.	O
</s>
<s>
The	O
blocks	O
themselves	O
are	O
ordered	O
recursively	O
,	O
according	O
to	O
the	O
Steinhaus	B-Algorithm
–	I-Algorithm
Johnson	I-Algorithm
–	I-Algorithm
Trotter	I-Algorithm
algorithm	I-Algorithm
for	O
one	O
less	O
element	O
.	O
</s>
<s>
Thus	O
,	O
from	O
the	O
single	O
permutation	B-Algorithm
on	O
one	O
element	O
,	O
</s>
<s>
one	O
may	O
place	O
the	O
number	O
2	O
in	O
each	O
possible	O
position	O
in	O
descending	O
order	O
to	O
form	O
a	O
list	O
of	O
two	O
permutations	B-Algorithm
on	O
two	O
elements	O
,	O
</s>
<s>
Then	O
,	O
one	O
may	O
place	O
the	O
number	O
3	O
in	O
each	O
of	O
three	O
different	O
positions	O
for	O
these	O
two	O
permutations	B-Algorithm
,	O
in	O
descending	O
order	O
for	O
the	O
first	O
permutation	B-Algorithm
1	O
2	O
,	O
and	O
then	O
in	O
ascending	O
order	O
for	O
the	O
permutation	B-Algorithm
2	O
1	O
:	O
</s>
<s>
In	O
sequences	O
of	O
permutations	B-Algorithm
with	O
this	O
recursive	O
structure	O
,	O
each	O
permutation	B-Algorithm
differs	O
from	O
the	O
previous	O
one	O
either	O
by	O
the	O
single-position-at-a-time	O
motion	O
of	O
,	O
or	O
by	O
a	O
change	O
of	O
two	O
smaller	O
numbers	O
inherited	O
from	O
the	O
previous	O
sequence	O
of	O
shorter	O
permutations	B-Algorithm
.	O
</s>
<s>
This	O
sequence	O
may	O
be	O
generated	O
by	O
a	O
recursive	O
algorithm	O
that	O
constructs	O
the	O
sequence	O
of	O
smaller	O
permutations	B-Algorithm
and	O
then	O
performs	O
all	O
possible	O
insertions	O
of	O
the	O
largest	O
number	O
into	O
the	O
recursively-generated	O
sequence	O
.	O
</s>
<s>
The	O
same	O
ordering	O
of	O
permutations	B-Algorithm
can	O
also	O
be	O
described	O
equivalently	O
as	O
the	O
ordering	O
generated	O
by	O
the	O
following	O
greedy	O
algorithm	O
.	O
</s>
<s>
Start	O
with	O
the	O
identity	O
permutation	B-Algorithm
.	O
</s>
<s>
Now	O
repeatedly	O
transpose	O
the	O
largest	O
possible	O
entry	O
with	O
the	O
entry	O
to	O
its	O
left	O
or	O
right	O
,	O
such	O
that	O
in	O
each	O
step	O
,	O
a	O
new	O
permutation	B-Algorithm
is	O
created	O
that	O
has	O
not	O
been	O
encountered	O
in	O
the	O
list	O
of	O
permutations	B-Algorithm
before	O
.	O
</s>
<s>
From	O
this	O
point	O
,	O
flipping	O
with	O
its	O
right	O
neighbor	O
would	O
yield	O
the	O
initial	O
permutation	B-Algorithm
,	O
so	O
the	O
sequence	O
instead	O
flips	O
with	O
its	O
left	O
neighbor	O
and	O
arrives	O
at	O
,	O
etc	O
.	O
</s>
<s>
However	O
,	O
the	O
actual	O
Steinhaus	B-Algorithm
–	I-Algorithm
Johnson	I-Algorithm
–	I-Algorithm
Trotter	I-Algorithm
algorithm	I-Algorithm
does	O
not	O
use	O
recursion	O
,	O
and	O
does	O
not	O
need	O
to	O
keep	O
track	O
of	O
the	O
permutations	B-Algorithm
that	O
it	O
has	O
already	O
encountered	O
.	O
</s>
<s>
Instead	O
,	O
it	O
computes	O
the	O
same	O
sequence	O
of	O
permutations	B-Algorithm
by	O
a	O
simple	O
iterative	B-Algorithm
method	I-Algorithm
.	O
</s>
<s>
As	O
described	O
by	O
,	O
the	O
algorithm	O
for	O
generating	O
the	O
next	O
permutation	B-Algorithm
from	O
a	O
given	O
permutation	B-Algorithm
performs	O
the	O
following	O
steps	O
.	O
</s>
<s>
For	O
each	O
from	O
1	O
to	O
,	O
let	O
be	O
the	O
position	O
where	O
the	O
value	O
is	O
placed	O
in	O
permutation	B-Algorithm
.	O
</s>
<s>
If	O
the	O
order	O
of	O
the	O
numbers	O
from	O
1	O
to	O
in	O
permutation	B-Algorithm
defines	O
an	O
even	O
permutation	B-Algorithm
,	O
let	O
otherwise	O
,	O
let	O
.	O
</s>
<s>
Find	O
the	O
largest	O
number	O
for	O
which	O
defines	O
a	O
valid	O
position	O
in	O
permutation	B-Algorithm
that	O
contains	O
a	O
number	O
smaller	O
than	O
.	O
</s>
<s>
When	O
no	O
number	O
can	O
be	O
found	O
meeting	O
the	O
conditions	O
of	O
the	O
second	O
step	O
of	O
the	O
algorithm	O
,	O
the	O
algorithm	O
has	O
reached	O
the	O
final	O
permutation	B-Algorithm
of	O
the	O
sequence	O
and	O
terminates	O
.	O
</s>
<s>
This	O
procedure	O
may	O
be	O
implemented	O
in	O
time	O
per	O
permutation	B-Algorithm
.	O
</s>
<s>
gives	O
an	O
alternative	O
implementation	O
of	O
an	O
iterative	B-Algorithm
algorithm	I-Algorithm
for	O
the	O
same	O
sequence	O
,	O
in	O
lightly	O
commented	O
ALGOL	B-Language
60	I-Language
notation	O
.	O
</s>
<s>
Because	O
this	O
method	O
generates	O
permutations	B-Algorithm
that	O
alternate	O
between	O
being	O
even	O
and	O
odd	O
,	O
it	O
may	O
easily	O
be	O
modified	O
to	O
generate	O
only	O
the	O
even	O
permutations	B-Algorithm
or	O
only	O
the	O
odd	O
permutations	B-Algorithm
:	O
to	O
generate	O
the	O
next	O
permutation	B-Algorithm
of	O
the	O
same	O
parity	O
from	O
a	O
given	O
permutation	B-Algorithm
,	O
simply	O
apply	O
the	O
same	O
procedure	O
twice	O
.	O
</s>
<s>
A	O
subsequent	O
improvement	O
by	O
Shimon	O
Even	O
provides	O
an	O
improvement	O
to	O
the	O
running	O
time	O
of	O
the	O
algorithm	O
by	O
storing	O
additional	O
information	O
for	O
each	O
element	O
in	O
the	O
permutation	B-Algorithm
:	O
its	O
position	O
,	O
and	O
a	O
direction	O
(	O
positive	O
,	O
negative	O
,	O
or	O
zero	O
)	O
in	O
which	O
it	O
is	O
currently	O
moving	O
(	O
essentially	O
,	O
this	O
is	O
the	O
same	O
information	O
computed	O
using	O
the	O
parity	O
of	O
the	O
permutation	B-Algorithm
in	O
Johnson	O
's	O
version	O
of	O
the	O
algorithm	O
)	O
.	O
</s>
<s>
If	O
this	O
causes	O
the	O
chosen	O
element	O
to	O
reach	O
the	O
first	O
or	O
last	O
position	O
within	O
the	O
permutation	B-Algorithm
,	O
or	O
if	O
the	O
next	O
element	O
in	O
the	O
same	O
direction	O
is	O
greater	O
than	O
the	O
chosen	O
element	O
,	O
the	O
direction	O
of	O
the	O
chosen	O
element	O
is	O
set	O
to	O
zero	O
:	O
</s>
<s>
That	O
is	O
,	O
positive	O
for	O
all	O
elements	O
between	O
the	O
start	O
of	O
the	O
permutation	B-Algorithm
and	O
the	O
chosen	O
element	O
,	O
and	O
negative	O
for	O
elements	O
toward	O
the	O
end	O
.	O
</s>
<s>
Thus	O
,	O
the	O
swaps	O
involving	O
the	O
number	O
take	O
only	O
constant	O
time	O
;	O
since	O
these	O
swaps	O
account	O
for	O
all	O
but	O
a	O
fraction	O
of	O
all	O
of	O
the	O
swaps	O
performed	O
by	O
the	O
algorithm	O
,	O
the	O
average	O
time	O
per	O
permutation	B-Algorithm
generated	O
is	O
also	O
constant	O
,	O
even	O
though	O
a	O
small	O
number	O
of	O
permutations	B-Algorithm
will	O
take	O
a	O
larger	O
amount	O
of	O
time	O
.	O
</s>
<s>
A	O
more	O
complex	O
loopless	B-Algorithm
version	O
of	O
the	O
same	O
procedure	O
allows	O
it	O
to	O
be	O
performed	O
in	O
constant	O
time	O
per	O
permutation	B-Algorithm
in	O
every	O
case	O
;	O
however	O
,	O
the	O
modifications	O
needed	O
to	O
eliminate	O
loops	O
from	O
the	O
procedure	O
make	O
it	O
slower	O
in	O
practice	O
.	O
</s>
<s>
The	O
set	O
of	O
all	O
permutations	B-Algorithm
of	O
items	O
may	O
be	O
represented	O
geometrically	O
by	O
a	O
permutohedron	B-Algorithm
,	O
the	O
polytope	O
formed	O
from	O
the	O
convex	O
hull	O
of	O
vectors	O
,	O
the	O
permutations	B-Algorithm
of	O
the	O
vector	O
.	O
</s>
<s>
Although	O
defined	O
in	O
this	O
way	O
in	O
-dimensional	O
space	O
,	O
it	O
is	O
actually	O
an	O
-dimensional	O
polytope	O
;	O
for	O
example	O
,	O
the	O
permutohedron	B-Algorithm
on	O
four	O
items	O
is	O
a	O
three-dimensional	O
polyhedron	O
,	O
the	O
truncated	O
octahedron	O
.	O
</s>
<s>
If	O
each	O
vertex	O
of	O
the	O
permutohedron	B-Algorithm
is	O
labeled	O
by	O
the	O
inverse	O
permutation	B-Algorithm
to	O
the	O
permutation	B-Algorithm
defined	O
by	O
its	O
vertex	O
coordinates	O
,	O
the	O
resulting	O
labeling	O
describes	O
a	O
Cayley	O
graph	O
of	O
the	O
symmetric	B-Algorithm
group	I-Algorithm
of	O
permutations	B-Algorithm
on	O
items	O
,	O
as	O
generated	O
by	O
the	O
permutations	B-Algorithm
that	O
swap	O
adjacent	O
pairs	O
of	O
items	O
.	O
</s>
<s>
Thus	O
,	O
each	O
two	O
consecutive	O
permutations	B-Algorithm
in	O
the	O
sequence	O
generated	O
by	O
the	O
Steinhaus	B-Algorithm
–	I-Algorithm
Johnson	I-Algorithm
–	I-Algorithm
Trotter	I-Algorithm
algorithm	I-Algorithm
correspond	O
in	O
this	O
way	O
to	O
two	O
vertices	O
that	O
form	O
the	O
endpoints	O
of	O
an	O
edge	O
in	O
the	O
permutohedron	B-Algorithm
,	O
and	O
the	O
whole	O
sequence	O
of	O
permutations	B-Algorithm
describes	O
a	O
Hamiltonian	O
path	O
in	O
the	O
permutohedron	B-Algorithm
,	O
a	O
path	O
that	O
passes	O
through	O
each	O
vertex	O
exactly	O
once	O
.	O
</s>
<s>
If	O
the	O
sequence	O
of	O
permutations	B-Algorithm
is	O
completed	O
by	O
adding	O
one	O
more	O
edge	O
from	O
the	O
last	O
permutation	B-Algorithm
to	O
the	O
first	O
one	O
in	O
the	O
sequence	O
,	O
the	O
result	O
is	O
instead	O
a	O
Hamiltonian	O
cycle	O
.	O
</s>
<s>
A	O
Gray	B-Device
code	I-Device
for	O
numbers	O
in	O
a	O
given	O
radix	O
is	O
a	O
sequence	O
that	O
contains	O
each	O
number	O
up	O
to	O
a	O
given	O
limit	O
exactly	O
once	O
,	O
in	O
such	O
a	O
way	O
that	O
each	O
pair	O
of	O
consecutive	O
numbers	O
differs	O
by	O
one	O
in	O
a	O
single	O
digit	O
.	O
</s>
<s>
The	O
permutations	B-Algorithm
of	O
the	O
numbers	O
from	O
1	O
to	O
may	O
be	O
placed	O
in	O
one-to-one	O
correspondence	O
with	O
the	O
numbers	O
from	O
0	O
to	O
by	O
pairing	O
each	O
permutation	B-Algorithm
with	O
the	O
sequence	O
of	O
numbers	O
that	O
count	O
the	O
number	O
of	O
positions	O
in	O
the	O
permutation	B-Algorithm
that	O
are	O
to	O
the	O
right	O
of	O
value	O
and	O
that	O
contain	O
a	O
value	O
less	O
than	O
(	O
that	O
is	O
,	O
the	O
number	O
of	O
inversions	B-Algorithm
for	O
which	O
is	O
the	O
larger	O
of	O
the	O
two	O
inverted	O
values	O
)	O
,	O
and	O
then	O
interpreting	O
these	O
sequences	O
as	O
numbers	O
in	O
the	O
factorial	O
number	O
system	O
,	O
that	O
is	O
,	O
the	O
mixed	O
radix	O
system	O
with	O
radix	O
sequence	O
For	O
instance	O
,	O
the	O
permutation	B-Algorithm
would	O
give	O
the	O
values	O
,	O
,	O
,	O
,	O
and	O
.	O
</s>
<s>
Consecutive	O
permutations	B-Algorithm
in	O
the	O
sequence	O
generated	O
by	O
the	O
Steinhaus	B-Algorithm
–	I-Algorithm
Johnson	I-Algorithm
–	I-Algorithm
Trotter	I-Algorithm
algorithm	I-Algorithm
have	O
numbers	O
of	O
inversions	B-Algorithm
that	O
differ	O
by	O
one	O
,	O
forming	O
a	O
Gray	B-Device
code	I-Device
for	O
the	O
factorial	O
number	O
system	O
.	O
</s>
<s>
More	O
generally	O
,	O
combinatorial	O
algorithms	O
researchers	O
have	O
defined	O
a	O
Gray	B-Device
code	I-Device
for	O
a	O
set	O
of	O
combinatorial	O
objects	O
to	O
be	O
an	O
ordering	O
for	O
the	O
objects	O
in	O
which	O
each	O
two	O
consecutive	O
objects	O
differ	O
in	O
the	O
minimal	O
possible	O
way	O
.	O
</s>
<s>
In	O
this	O
generalized	O
sense	O
,	O
the	O
Steinhaus	B-Algorithm
–	I-Algorithm
Johnson	I-Algorithm
–	I-Algorithm
Trotter	I-Algorithm
algorithm	I-Algorithm
generates	O
a	O
Gray	B-Device
code	I-Device
for	O
the	O
permutations	B-Algorithm
themselves	O
.	O
</s>
<s>
The	O
method	O
was	O
known	O
for	O
much	O
of	O
history	O
as	O
a	O
method	O
for	O
change	O
ringing	O
of	O
church	O
bells	O
:	O
it	O
gives	O
a	O
procedure	O
by	O
which	O
a	O
set	O
of	O
bells	O
can	O
be	O
rung	O
through	O
all	O
possible	O
permutations	B-Algorithm
,	O
changing	O
the	O
order	O
of	O
only	O
two	O
bells	O
per	O
change	O
.	O
</s>
<s>
These	O
so-called	O
"	O
plain	B-Algorithm
changes	I-Algorithm
"	O
were	O
recorded	O
as	O
early	O
as	O
1621	O
for	O
four	O
bells	O
,	O
and	O
a	O
1677	O
book	O
by	O
Fabian	O
Stedman	O
lists	O
the	O
solutions	O
for	O
up	O
to	O
six	O
bells	O
.	O
</s>
<s>
More	O
recently	O
,	O
change	O
ringers	O
have	O
abided	O
by	O
a	O
rule	O
that	O
no	O
bell	O
may	O
stay	O
in	O
the	O
same	O
position	O
for	O
three	O
consecutive	O
permutations	B-Algorithm
;	O
this	O
rule	O
is	O
violated	O
by	O
the	O
plain	B-Algorithm
changes	I-Algorithm
,	O
so	O
other	O
strategies	O
that	O
swap	O
multiple	O
bells	O
per	O
change	O
have	O
been	O
devised	O
.	O
</s>
<s>
A	O
book	O
by	O
Steinhaus	O
,	O
originally	O
published	O
in	O
1958	O
and	O
translated	O
into	O
English	O
in	O
1963	O
,	O
describes	O
a	O
related	O
puzzle	O
of	O
generating	O
all	O
permutations	B-Algorithm
by	O
a	O
system	O
of	O
particles	O
,	O
each	O
moving	O
at	O
constant	O
speed	O
along	O
a	O
line	O
and	O
swapping	O
positions	O
when	O
one	O
particle	O
overtakes	O
another	O
.	O
</s>
<s>
No	O
solution	O
is	O
possible	O
for	O
,	O
because	O
the	O
number	O
of	O
swaps	O
is	O
far	O
fewer	O
than	O
the	O
number	O
of	O
permutations	B-Algorithm
,	O
but	O
the	O
Steinhaus	B-Algorithm
–	I-Algorithm
Johnson	I-Algorithm
–	I-Algorithm
Trotter	I-Algorithm
algorithm	I-Algorithm
describes	O
the	O
motion	O
of	O
particles	O
with	O
non-constant	O
speeds	O
that	O
generate	O
all	O
permutations	B-Algorithm
.	O
</s>
