### Ranking and Unranking Permutations

 problem size permutation rank
This script ranks and unranks a permutation of n distinct integers from the interval [0 .. n-1] (n<10). The script doesn't check the validity of your input, so it's up to you to take care of it. The algorithm bases on the concept of the Combinatorial Number System[see addendum]. Every permutation can be represented by an assignement matrix which can be represented by a combi-number[see addendum]. For instance the permutation 31420 is represented by the assignement matrix
``` o o o o x
o x o o o
o o o x o
x o o o o
o o x o o```
which is represented by the combi-number[see addendum] k31210=3·4!+1·3!+2·2!+1·1!+0·0!=83.
The ranking and unranking is computed using O(n) 'arithmetic operations', so it does the same as the algorithm of Myrvold and Ruskey 'Ranking and Unranking Permutations in Linear Time' at http://www.csr.uvic.ca/~fruskey/Publications/RankPerm.html. While the formulation O(n) 'arithmetic operations' is correct, the formulation 'in Linear Time' seems to be not correct, because the time for an 'arithmetic operation' (Myrvold and Ruskey use multiplication, modulo and integer division) depends on the magnitude of n! (calculation with integer values would be faster than with longinteger values). I think the algorithm used in this script is not as sophisticated as the above mentioned and also the ordering is a bit nicer then that of the above mentioned algorithm:

(c) Lutz Tautenhahn 1998, 2001