Inversion number (source code)

= Inversion number
{title2=$\operatorname{inv}(a)$}

= Inversion statistic
{synonym}

= Inversion count
{synonym}

The <inversion number> of a <permutation> $a_1\cdots a_n$ is
$$
\operatorname{inv}(a)=\#\{(i,j):1\leq i<j\leq n,\ a_i>a_j\}.
$$
For `3142`, the inverted pairs of values are $(3,1),(3,2),(4,2)$, giving three. The <inversion number> ranges from zero for increasing order to $\binom n2$ for decreasing order; it is the minimum number of <adjacent transpositions> needed to sort the <permutation>. Its parity gives the <sign of a permutation>, $(-1)^{\operatorname{inv}(a)}$. The same pair-counting formula defines the <word inversion number> when repeated symbols are allowed.