Using Lehmer code, any permutation of a sequence of N elements can be encoded and mapped to a decimal number using the factorial number system.
Example :
Lehmer codes for ABCD permutations :
ABDC => 0010
CBAD => 2100
DCBA => 3210
These inversions vectors can be converted to a decimal using factorials :
2100 => 2 x 3! + 1 x 2! + 0 x 1! + 0 x 0!
=> 2 x 6 + 1 x 2 + 0 x 1 + 0 x 1
=> 14
So CBAD permutation can be map directly to number 14.
My question is :
From a number that map to a permutation, is there a computationally efficient method to generate numbers of other permutations that differ from the previous permutation by swapping two elements in the sequence ?
Example :
We have 4 (which map to ADBC) and we want to swap the first two elements. Result is 18 (or DABC).
4 => 18
0200 => 3000
ADBC => DABC
Method would be declared like this :
swap(4, 0, 1); //return 18
I want to avoid doing the whole process again but reverse :
Number => Factorial => Rebuild original permutation and swap elements (costly) =>
Factorial => Number
Note : On wikipedia there is article about Steinhaus–Johnson–Trotter algorithm but i'm not sure it would help here.