Maths & numbers

Permutation inversion count distribution calculator

An inversion is a pair of positions whose values appear in the opposite order to their labels.

Your numbers

Integer 0–40.
Integer from zero through n(n−1)/2.

Calculated on your device. Your inputs aren’t sent to us.

Your result

Ready when you are

Enter your inputs and select Calculate.

Report an issue

Behind the numbers

How this calculation works

An inversion is a pair of positions whose values appear in the opposite order to their labels. This calculator returns the complete inversion-count distribution for permutations of distinct labels, the count at a selected k and its exact probability under a uniform permutation. The recurrence inserts the largest label and counts how many new inversions its position creates, retaining exact integers throughout.

THE FORMULAM_n(k)=Σ_(j=0)^(min(k,n−1))M_(n−1)(k−j), with M₀(0)=1; probability=M_n(k)/n!.

A worked example

The six permutations of 1,2,3 have inversion counts 0,1,1,2,2 and 3. The distribution is therefore 1,2,2,1. Exactly two permutations have one inversion, giving probability 2/6=1/3. For permutation 3,1,2 the inversion pairs are (3,1) and (3,2), even though it has only one adjacent descent.

Good to know

Use n=0–40 and k from zero through n(n−1)/2. Labels are distinct and all n! permutations are equally likely. Repeated-value multiset permutations, biased shuffles and adjacent-descent distributions are different problems. The empty permutation n=0 has count one at k=0. The full table may contain 781 rows at n=40 and keeps exact count-to-factorial probability ratios.

Inversions can span more than neighboring positions

For each i<j, compare the two values. If the earlier value is larger, that pair contributes one inversion. Several inversions may involve the same position, and a pair need not be adjacent. A descent checks only neighbors, so the two statistics cannot be interchanged even though both vanish in the increasing permutation.

The largest possible inversion count is n(n−1)/2, achieved by the decreasing permutation. The increasing permutation has zero. Both endpoints have count one. Reversing a permutation complements its inversion count to the maximum, explaining the symmetry of the distribution. These facts supply independent checks on the returned table.

The inversion distribution for three labels is symmetric with counts one two two one1k=02k=12k=21k=3
Among the six permutations of three distinct labels, the counts at k=0,1,2,3 are 1,2,2,1. At k=1 the count is two and probability is 2/6=1/3. This counts all inverted pairs, not adjacent descents.

The Eulerian number for permutation ascents calculator counts ascents between neighbors, which differs from inversions over every earlier-later pair.

Insert the largest label to derive the recurrence

Start with a permutation of n−1 labels and insert n into one of n positions. At the end it creates zero new inversions; one position earlier it creates one; at the beginning it creates n−1. Each insertion position has a unique new-inversion contribution, so summing the previous counts over that range gives M_n(k).

A sliding window evaluates these sums efficiently without changing their combinatorial meaning. It adds the new right endpoint and removes the term that falls beyond the n-position window. Counts outside the previous support are zero. At every size the row sum must equal the factorial of that size, because every permutation has exactly one inversion count.

Exact probabilities refer to a uniform permutation

The probability denominator is n!, not the number of possible inversion counts. The values of k are not equally likely: near the middle, many permutations share a count, while the endpoints have only one each. Dividing by the support size would consequently overstate the extreme cases and understate the central ones.

The selected probability is reduced to an exact fraction; table rows retain their count over n! to make the shared sample space visible. Small n can be checked by enumerating every permutation and counting inversion pairs directly. For n=0 there is one empty arrangement, so its zero-inversion probability is one rather than an undefined zero-over-zero expression.

A few questions, answered

Is k the number of adjacent swaps needed to sort?

For distinct labels and swaps restricted to adjacent positions, the minimum number is the inversion count. Arbitrary swaps have a different minimum.

Can I enter repeated labels?

No. This distribution is for distinct labels. Repeated symbols change both the sample space and the inversion multiplicities.

Find your calculator

Start typing to explore the library.