<s>
In	O
computer	B-General_Concept
science	I-General_Concept
and	O
discrete	O
mathematics	O
,	O
an	O
inversion	B-Algorithm
in	O
a	O
sequence	O
is	O
a	O
pair	O
of	O
elements	O
that	O
are	O
out	O
of	O
their	O
natural	O
order	O
.	O
</s>
<s>
Let	O
be	O
a	O
permutation	B-Algorithm
.	O
</s>
<s>
There	O
is	O
an	O
inversion	B-Algorithm
of	O
between	O
and	O
if	O
and	O
.	O
</s>
<s>
The	O
inversion	B-Algorithm
is	O
indicated	O
by	O
an	O
ordered	O
pair	O
containing	O
either	O
the	O
places	O
or	O
the	O
elements	O
.	O
</s>
<s>
The	O
inversion	B-Algorithm
set	O
is	O
the	O
set	O
of	O
all	O
inversions	B-Algorithm
.	O
</s>
<s>
A	O
permutation	B-Algorithm
's	O
inversion	B-Algorithm
set	O
using	O
place-based	O
notation	O
is	O
the	O
same	O
as	O
the	O
inverse	O
permutation	B-Algorithm
's	O
inversion	B-Algorithm
set	O
using	O
element-based	O
notation	O
with	O
the	O
two	O
components	O
of	O
each	O
ordered	O
pair	O
exchanged	O
.	O
</s>
<s>
Likewise	O
,	O
a	O
permutation	B-Algorithm
's	O
inversion	B-Algorithm
set	O
using	O
element-based	O
notation	O
is	O
the	O
same	O
as	O
the	O
inverse	O
permutation	B-Algorithm
's	O
inversion	B-Algorithm
set	O
using	O
place-based	O
notation	O
with	O
the	O
two	O
components	O
of	O
each	O
ordered	O
pair	O
exchanged	O
.	O
</s>
<s>
Inversions	B-Algorithm
are	O
usually	O
defined	O
for	O
permutations	B-Algorithm
,	O
but	O
may	O
also	O
be	O
defined	O
for	O
sequences:Let	O
be	O
a	O
sequence	O
(	O
or	O
multiset	B-Language
permutation	B-Algorithm
)	O
.	O
</s>
<s>
If	O
and	O
,	O
either	O
the	O
pair	O
of	O
places	O
or	O
the	O
pair	O
of	O
elements	O
is	O
called	O
an	O
inversion	B-Algorithm
of	O
.	O
</s>
<s>
For	O
sequences	O
,	O
inversions	B-Algorithm
according	O
to	O
the	O
element-based	O
definition	O
are	O
not	O
unique	O
,	O
because	O
different	O
pairs	O
of	O
places	O
may	O
have	O
the	O
same	O
pair	O
of	O
values	O
.	O
</s>
<s>
The	O
inversion	B-Algorithm
number	O
of	O
a	O
sequence	O
,	O
is	O
the	O
cardinality	B-Application
of	O
the	O
inversion	B-Algorithm
set	O
.	O
</s>
<s>
It	O
is	O
a	O
common	O
measure	O
of	O
sortedness	O
(	O
sometimes	O
called	O
presortedness	O
)	O
of	O
a	O
permutation	B-Algorithm
or	O
sequence	O
.	O
</s>
<s>
The	O
inversion	B-Algorithm
number	O
is	O
between	O
0	O
and	O
inclusive	O
.	O
</s>
<s>
A	O
permutation	B-Algorithm
and	O
its	O
inverse	O
have	O
the	O
same	O
inversion	B-Algorithm
number	O
.	O
</s>
<s>
Also	O
,	O
when	O
is	O
even	O
,	O
(	O
because	O
each	O
pair	O
is	O
an	O
inversion	B-Algorithm
)	O
.	O
</s>
<s>
This	O
last	O
example	O
shows	O
that	O
a	O
set	O
that	O
is	O
intuitively	O
"	O
nearly	O
sorted	O
"	O
can	O
still	O
have	O
a	O
quadratic	O
number	O
of	O
inversions	B-Algorithm
.	O
</s>
<s>
The	O
inversion	B-Algorithm
number	O
is	O
the	O
number	O
of	O
crossings	O
in	O
the	O
arrow	O
diagram	O
of	O
the	O
permutation	B-Algorithm
,	O
the	O
permutation	B-Algorithm
's	O
Kendall	O
tau	O
distance	O
from	O
the	O
identity	O
permutation	B-Algorithm
,	O
and	O
the	O
sum	O
of	O
each	O
of	O
the	O
inversion	B-Algorithm
related	O
vectors	O
defined	O
below	O
.	O
</s>
<s>
Standard	O
comparison	B-Algorithm
sorting	I-Algorithm
algorithms	O
can	O
be	O
adapted	O
to	O
compute	O
the	O
inversion	B-Algorithm
number	O
in	O
time	O
.	O
</s>
<s>
Three	O
similar	O
vectors	O
are	O
in	O
use	O
that	O
condense	O
the	O
inversions	B-Algorithm
of	O
a	O
permutation	B-Algorithm
into	O
a	O
vector	O
that	O
uniquely	O
determines	O
it	O
.	O
</s>
<s>
They	O
are	O
often	O
called	O
inversion	B-Algorithm
vector	I-Algorithm
or	O
Lehmer	O
code	O
.	O
</s>
<s>
This	O
article	O
uses	O
the	O
term	O
inversion	B-Algorithm
vector	I-Algorithm
(	O
)	O
like	O
Wolfram	B-Application
.	O
</s>
<s>
The	O
remaining	O
two	O
vectors	O
are	O
sometimes	O
called	O
left	O
and	O
right	O
inversion	B-Algorithm
vector	I-Algorithm
,	O
but	O
to	O
avoid	O
confusion	O
with	O
the	O
inversion	B-Algorithm
vector	I-Algorithm
this	O
article	O
calls	O
them	O
left	O
inversion	B-Algorithm
count	O
(	O
)	O
and	O
right	O
inversion	B-Algorithm
count	O
(	O
)	O
.	O
</s>
<s>
Interpreted	O
as	O
a	O
factorial	O
number	O
the	O
left	O
inversion	B-Algorithm
count	O
gives	O
the	O
permutations	B-Algorithm
reverse	O
colexicographic	O
,	O
and	O
the	O
right	O
inversion	B-Algorithm
count	O
gives	O
the	O
lexicographic	O
index	O
.	O
</s>
<s>
Inversion	B-Algorithm
vector	I-Algorithm
:With	O
the	O
element-based	O
definition	O
is	O
the	O
number	O
of	O
inversions	B-Algorithm
whose	O
smaller	O
(	O
right	O
)	O
component	O
is	O
.	O
</s>
<s>
Left	O
inversion	B-Algorithm
count	O
:With	O
the	O
place-based	O
definition	O
is	O
the	O
number	O
of	O
inversions	B-Algorithm
whose	O
bigger	O
(	O
right	O
)	O
component	O
is	O
.	O
</s>
<s>
Right	O
inversion	B-Algorithm
count	O
,	O
often	O
called	O
Lehmer	O
code:With	O
the	O
place-based	O
definition	O
is	O
the	O
number	O
of	O
inversions	B-Algorithm
whose	O
smaller	O
(	O
left	O
)	O
component	O
is	O
.	O
</s>
<s>
Both	O
and	O
can	O
be	O
found	O
with	O
the	O
help	O
of	O
a	O
Rothe	O
diagram	O
,	O
which	O
is	O
a	O
permutation	B-Algorithm
matrix	I-Algorithm
with	O
the	O
1s	O
represented	O
by	O
dots	O
,	O
and	O
an	O
inversion	B-Algorithm
(	O
often	O
represented	O
by	O
a	O
cross	O
)	O
in	O
every	O
position	O
that	O
has	O
a	O
dot	O
to	O
the	O
right	O
and	O
below	O
it	O
.	O
</s>
<s>
is	O
the	O
sum	O
of	O
inversions	B-Algorithm
in	O
row	O
of	O
the	O
Rothe	O
diagram	O
,	O
while	O
is	O
the	O
sum	O
of	O
inversions	B-Algorithm
in	O
column	O
.	O
</s>
<s>
The	O
permutation	B-Algorithm
matrix	I-Algorithm
of	O
the	O
inverse	O
is	O
the	O
transpose	O
,	O
therefore	O
of	O
a	O
permutation	B-Algorithm
is	O
of	O
its	O
inverse	O
,	O
and	O
vice	O
versa	O
.	O
</s>
<s>
The	O
following	O
sortable	O
table	O
shows	O
the	O
24	O
permutations	B-Algorithm
of	O
four	O
elements	O
(	O
in	O
the	O
column	O
)	O
with	O
their	O
place-based	O
inversion	B-Algorithm
sets	O
(	O
in	O
the	O
p-b	O
column	O
)	O
,	O
inversion	B-Algorithm
related	O
vectors	O
(	O
in	O
the	O
,	O
,	O
and	O
columns	O
)	O
,	O
and	O
inversion	B-Algorithm
numbers	O
(	O
in	O
the	O
#	O
column	O
)	O
.	O
</s>
<s>
It	O
can	O
be	O
seen	O
that	O
and	O
always	O
have	O
the	O
same	O
digits	O
,	O
and	O
that	O
and	O
are	O
both	O
related	O
to	O
the	O
place-based	O
inversion	B-Algorithm
set	O
.	O
</s>
<s>
The	O
set	O
of	O
permutations	B-Algorithm
on	O
n	O
items	O
can	O
be	O
given	O
the	O
structure	O
of	O
a	O
partial	O
order	O
,	O
called	O
the	O
weak	O
order	O
of	O
permutations	B-Algorithm
,	O
which	O
forms	O
a	O
lattice	O
.	O
</s>
<s>
The	O
Hasse	O
diagram	O
of	O
the	O
inversion	B-Algorithm
sets	O
ordered	O
by	O
the	O
subset	O
relation	O
forms	O
the	O
skeleton	O
of	O
a	O
permutohedron	B-Algorithm
.	O
</s>
<s>
If	O
a	O
permutation	B-Algorithm
is	O
assigned	O
to	O
each	O
inversion	B-Algorithm
set	O
using	O
the	O
place-based	O
definition	O
,	O
the	O
resulting	O
order	O
of	O
permutations	B-Algorithm
is	O
that	O
of	O
the	O
permutohedron	B-Algorithm
,	O
where	O
an	O
edge	O
corresponds	O
to	O
the	O
swapping	O
of	O
two	O
elements	O
with	O
consecutive	O
values	O
.	O
</s>
<s>
This	O
is	O
the	O
weak	O
order	O
of	O
permutations	B-Algorithm
.	O
</s>
<s>
The	O
identity	O
is	O
its	O
minimum	O
,	O
and	O
the	O
permutation	B-Algorithm
formed	O
by	O
reversing	O
the	O
identity	O
is	O
its	O
maximum	O
.	O
</s>
<s>
If	O
a	O
permutation	B-Algorithm
were	O
assigned	O
to	O
each	O
inversion	B-Algorithm
set	O
using	O
the	O
element-based	O
definition	O
,	O
the	O
resulting	O
order	O
of	O
permutations	B-Algorithm
would	O
be	O
that	O
of	O
a	O
Cayley	O
graph	O
,	O
where	O
an	O
edge	O
corresponds	O
to	O
the	O
swapping	O
of	O
two	O
elements	O
on	O
consecutive	O
places	O
.	O
</s>
<s>
This	O
Cayley	O
graph	O
of	O
the	O
symmetric	B-Algorithm
group	I-Algorithm
is	O
similar	O
to	O
its	O
permutohedron	B-Algorithm
,	O
but	O
with	O
each	O
permutation	B-Algorithm
replaced	O
by	O
its	O
inverse	O
.	O
</s>
