<s>
Heap	B-Algorithm
's	I-Algorithm
algorithm	I-Algorithm
generates	O
all	O
possible	O
permutations	B-Algorithm
of	O
objects	O
.	O
</s>
<s>
The	O
algorithm	O
minimizes	O
movement	O
:	O
it	O
generates	O
each	O
permutation	B-Algorithm
from	O
the	O
previous	O
one	O
by	O
interchanging	O
a	O
single	O
pair	O
of	O
elements	O
;	O
the	O
other	O
elements	O
are	O
not	O
disturbed	O
.	O
</s>
<s>
In	O
a	O
1977	O
review	O
of	O
permutation-generating	O
algorithms	O
,	O
Robert	O
Sedgewick	O
concluded	O
that	O
it	O
was	O
at	O
that	O
time	O
the	O
most	O
effective	O
algorithm	O
for	O
generating	O
permutations	B-Algorithm
by	O
computer	O
.	O
</s>
<s>
The	O
sequence	O
of	O
permutations	B-Algorithm
of	O
objects	O
generated	O
by	O
Heap	B-Algorithm
's	I-Algorithm
algorithm	I-Algorithm
is	O
the	O
beginning	O
of	O
the	O
sequence	O
of	O
permutations	B-Algorithm
of	O
objects	O
.	O
</s>
<s>
So	O
there	O
is	O
one	O
infinite	O
sequence	O
of	O
permutations	B-Algorithm
generated	O
by	O
Heap	B-Algorithm
's	I-Algorithm
algorithm	I-Algorithm
.	O
</s>
<s>
For	O
a	O
collection	O
containing	O
different	O
elements	O
,	O
Heap	O
found	O
a	O
systematic	O
method	O
for	O
choosing	O
at	O
each	O
step	O
a	O
pair	O
of	O
elements	O
to	O
switch	O
in	O
order	O
to	O
produce	O
every	O
possible	O
permutation	B-Algorithm
of	O
these	O
elements	O
exactly	O
once	O
.	O
</s>
<s>
Described	O
recursively	O
as	O
a	O
decrease	B-Algorithm
and	I-Algorithm
conquer	I-Algorithm
method	O
,	O
Heap	B-Algorithm
's	I-Algorithm
algorithm	I-Algorithm
operates	O
at	O
each	O
step	O
on	O
the	O
initial	O
elements	O
of	O
the	O
collection	O
.	O
</s>
<s>
Each	O
step	O
generates	O
the	O
permutations	B-Algorithm
that	O
end	O
with	O
the	O
same	O
final	O
elements	O
.	O
</s>
<s>
In	O
this	O
proof	O
,	O
we	O
'll	O
use	O
the	O
implementation	O
below	O
as	O
Heap	B-Algorithm
's	I-Algorithm
Algorithm	I-Algorithm
.	O
</s>
<s>
While	O
it	O
is	O
not	O
optimal	O
(	O
see	O
section	O
below	O
)	O
,	O
the	O
implementation	O
is	O
nevertheless	O
still	O
correct	O
and	O
will	O
produce	O
all	O
permutations	B-Algorithm
.	O
</s>
<s>
Claim	O
:	O
If	O
array	O
has	O
length	O
,	O
then	O
performing	O
Heap	B-Algorithm
's	I-Algorithm
algorithm	I-Algorithm
will	O
either	O
result	O
in	O
being	O
"	O
rotated	O
"	O
to	O
the	O
right	O
by	O
1	O
(	O
i.e.	O
</s>
<s>
Basis	O
:	O
The	O
claim	O
above	O
trivially	O
holds	O
true	O
for	O
as	O
Heap	B-Algorithm
's	I-Algorithm
algorithm	I-Algorithm
will	O
simply	O
return	O
unaltered	O
in	O
order	O
.	O
</s>
<s>
If	O
,	O
for	O
,	O
is	O
even	O
,	O
then	O
the	O
subset	O
of	O
the	O
first	O
elements	O
will	O
remain	O
unaltered	O
after	O
performing	O
Heap	B-Algorithm
's	I-Algorithm
Algorithm	I-Algorithm
on	O
the	O
subarray	O
,	O
as	O
assumed	O
by	O
the	O
induction	O
hypothesis	O
.	O
</s>
<s>
By	O
performing	O
Heap	B-Algorithm
's	I-Algorithm
Algorithm	I-Algorithm
on	O
the	O
subarray	O
and	O
then	O
performing	O
the	O
swapping	O
operation	O
,	O
in	O
the	O
th	O
iteration	O
of	O
the	O
for-loop	O
,	O
where	O
,	O
the	O
th	O
element	O
in	O
will	O
be	O
swapped	O
into	O
the	O
last	O
position	O
of	O
which	O
can	O
be	O
thought	O
as	O
a	O
kind	O
of	O
"	O
buffer	O
"	O
.	O
</s>
<s>
If	O
,	O
for	O
,	O
is	O
odd	O
,	O
then	O
the	O
subset	O
of	O
the	O
first	O
elements	O
will	O
be	O
rotated	O
after	O
performing	O
Heap	B-Algorithm
's	I-Algorithm
Algorithm	I-Algorithm
on	O
the	O
first	O
elements	O
.	O
</s>
<s>
Notice	O
that	O
,	O
after	O
1	O
iteration	O
of	O
the	O
for-loop	O
,	O
when	O
performing	O
Heap	B-Algorithm
's	I-Algorithm
Algorithm	I-Algorithm
on	O
,	O
is	O
rotated	O
to	O
the	O
right	O
by	O
1	O
.	O
</s>
<s>
The	O
induction	O
proof	O
for	O
the	O
claim	O
is	O
now	O
complete	O
,	O
which	O
will	O
now	O
lead	O
to	O
why	O
Heap	B-Algorithm
's	I-Algorithm
Algorithm	I-Algorithm
creates	O
all	O
permutations	B-Algorithm
of	O
array	O
.	O
</s>
<s>
Once	O
again	O
we	O
will	O
prove	O
by	O
induction	O
the	O
correctness	O
of	O
Heap	B-Algorithm
's	I-Algorithm
Algorithm	I-Algorithm
.	O
</s>
<s>
Basis	O
:	O
Heap	B-Algorithm
's	I-Algorithm
Algorithm	I-Algorithm
trivially	O
permutes	O
an	O
array	O
of	O
size	O
as	O
outputting	O
is	O
the	O
one	O
and	O
only	O
permutation	B-Algorithm
of	O
.	O
</s>
<s>
Induction	O
:	O
Assume	O
Heap	B-Algorithm
's	I-Algorithm
Algorithm	I-Algorithm
permutes	O
an	O
array	O
of	O
size	O
.	O
</s>
<s>
Because	O
permutations	B-Algorithm
of	O
an	O
array	O
can	O
be	O
made	O
by	O
altering	O
some	O
array	O
through	O
the	O
removal	O
of	O
an	O
element	O
from	O
then	O
tacking	O
on	O
to	O
each	O
permutation	B-Algorithm
of	O
the	O
altered	O
array	O
,	O
it	O
follows	O
that	O
Heap	B-Algorithm
's	I-Algorithm
Algorithm	I-Algorithm
permutes	O
an	O
array	O
of	O
size	O
,	O
for	O
the	O
"	O
buffer	O
"	O
in	O
essence	O
holds	O
the	O
removed	O
element	O
,	O
being	O
tacked	O
onto	O
the	O
permutations	B-Algorithm
of	O
the	O
subarray	O
of	O
size	O
.	O
</s>
<s>
Because	O
each	O
iteration	O
of	O
Heap	B-Algorithm
's	I-Algorithm
Algorithm	I-Algorithm
has	O
a	O
different	O
element	O
of	O
occupying	O
the	O
buffer	O
when	O
the	O
subarray	O
is	O
permuted	O
,	O
every	O
permutation	B-Algorithm
is	O
generated	O
as	O
each	O
element	O
of	O
has	O
a	O
chance	O
to	O
be	O
tacked	O
onto	O
the	O
permutations	B-Algorithm
of	O
the	O
array	O
without	O
the	O
buffer	O
element	O
.	O
</s>
<s>
This	O
implementation	O
will	O
succeed	O
in	O
producing	O
all	O
permutations	B-Algorithm
but	O
does	O
not	O
minimize	O
movement	O
.	O
</s>
<s>
As	O
the	O
recursive	O
call-stacks	B-General_Concept
unwind	O
,	O
it	O
results	O
in	O
additional	O
swaps	O
at	O
each	O
level	O
.	O
</s>
