Permutation: Get number of inversions

Get the number of inversions in a Permutation object. An inversion is defined as: if i < j then p(i) > p(j).

Examples

The query for the number of inversions in the permutation (3,2,4,5,1) gives 3 because 3>2, 3>1 and 2>1.

The permutation (1,2,3,4) has 0 inversions.

The permutation (4,3,2,1) has 6 inversions.

The number of inversions of a permutation with n elements is always in the closed interval [0, n(n-1)/2].

Links to this page


© djmw 20250120