information:classification, linguistic:voice_change
Numbering permutations

One way to represent permutations of n things is by an integer N with 0 ≤ N < n !, provided convenient methods are given to convert between the number and the representation of a permutation as an ordered arrangement (sequence). This gives the most compact representation of arbitrary permutations, and in computing is particularly attractive when n is small enough that N can be held in a machine word; for 32-bit words this means n ≤ 12, and for 64-bit words this means n ≤ 20. The conversion can be done via the intermediate form of a sequence of numbers d, d,..., d, d, where d is a non-negative integer less than i (one may omit d, as it is always 0, but its presence makes the subsequent conversion to a permutation easier to describe). The first step then is to simply express N in the factorial number system, which is just a particular mixed radix representation, where, for numbers less than n !, the bases (place values or multiplication factors) for successive digits are (n − 1)!, (n − 2)!,..., 2!, 1!. The second step interprets this sequence as a Lehmer code or (almost equivalently) as an inversion table.

In the Lehmer code for a permutation σ, the number d represents the choice made for the first term σ, the number d represents the choice made for the second term σ among the remaining n − 1 elements of the set, and so forth. More precisely, each d gives the number of remaining elements strictly less than the term σ. Since those remaining elements are bound to turn up as some later term σ, the digit d counts the inversions (i, j) involving i as smaller index (the number of values j for which i < j and σ > σ). The inversion table for σ is quite similar, but here d counts the number of inversions (i, j) where k = σ occurs as the smaller of the two values appearing in inverted order.

Both encodings can be visualized by an n by n Rothe diagram (named after Heinrich August Rothe) in which dots at (i, σ) mark the entries of the permutation, and a cross at (i, σ) marks the inversion (i, j); by the definition of inversions a cross appears in any square that comes both before the dot (j, σ) in its column, and before the dot (i, σ) in its row. The Lehmer code lists the numbers of crosses in successive rows, while the inversion table lists the numbers of crosses in successive columns; it is just the Lehmer code for the inverse permutation, and vice versa.

To effectively convert a Lehmer code d, d,..., d, d into a permutation of an ordered set S, one can start with a list of the elements of S in increasing order, and for i increasing from 1 to n set σ to the element in the list that is preceded by d other ones, and remove that element from the list. To convert an inversion table d, d,..., d, d into the corresponding permutation, one can traverse the numbers from d to d while inserting the elements of S from largest to smallest into an initially empty sequence; at the step using the number d from the inversion table, the element from S inserted into the sequence at the point where it is preceded by d elements already present. Alternatively one could process the numbers from the inversion table and the elements of S both in the opposite order, starting with a row of n empty slots, and at each step place the element from S into the empty slot that is preceded by d other empty slots.

Converting successive natural numbers to the factorial number system produces those sequences in lexicographic order (as is the case with any mixed radix number system), and further converting them to permutations preserves the lexicographic ordering, provided the Lehmer code interpretation is used (using inversion tables, one gets a different ordering, where one starts by comparing permutations by the place of their entries 1 rather than by the value of their first entries). The sum of the numbers in the factorial number system representation gives the number of inversions of the permutation, and the parity of that sum gives the signature of the permutation. Moreover, the positions of the zeroes in the inversion table give the values of left-to-right maxima of the permutation (in the example 6, 8, 9) while the positions of the zeroes in the Lehmer code are the positions of the right-to-left minima (in the example positions the 4, 8, 9 of the values 1, 2, 5); this allows computing the distribution of such extrema among all permutations. A permutation with Lehmer code d, d,..., d, d has an ascent n − i if and only if d ≥ d.