This is a little messy, but I think it works. I will show how to transform a permutation to an index and then describe how to go the other way.
Let's number the positions in the permutation as follows and start the current index at 1:
_ _ _ _ _ _
5 4 3 2 1 0
Current Index = 1
(I am going to start with current index = 1, so the smallest index will be 1. We will have to remember to subtract 1 when we want to convert an index back to a permutation).
(Let's hope Yahoo! does not mess up my formatting).
Now pick a position for the 6 and add (position number)*(6-1)! =
(position number)*120 to the index. For example, let's put 6 in position 3. Now we let's number the remaining places as follows:
_ _ 6 _ _ _
4 3 _ 2 1 0
Current Index = 1 + 360 = 361
Now pick a position for the 5 and add (position number)*(5-1)! =
(position number)*24 to the index. For example, let's put 5 in position 0. Now we let's number the remaining places as follows:
_ _ 6 _ _ 5
3 2 _ 1 0 _
Current Index = 360 + 0*24 = 361
Now pick a position for the 4 and add (position number)*(4-1)! =
(position number)*6 to the index. For example, let's put 4 in position 3. Now we let's number the remaining places as follows:
4 _ 6 _ _ 5
_ 2 _ 1 0 _
Current Index = 361 + 3*6 = 379
Now pick a position for the 3 and add (position number)*(3-1)! =
(position number)*2 to the index. For example, let's put 3 in position 1. Now we let's number the remaining places as follows:
4 _ 6 3 _ 5
_ 1 _ _ 0 _
Current Index = 379 + 1*2 = 381
Now pick a position for the 2 and add (position number)*(2-1)! =
(position number)*1 to the index. For example, let's put 2 in position 1. Now we let's number the remaining places as follows:
4 2 6 3 _ 5
_ _ _ _ 0 _
Current Index = 381 + 1 = 382
Now there is only one position left so 1 goes there and we have the permutation 4 2 6 3 1 5 and index 382.
We can now reverse these steps, by taking the index 382 and subtracting 1 to get 381. Then we divide by 5! to get 3 remainder 21. That tells us to put 6 in position 3 so we have:
_ _ 6 _ _ _
Now we use the remainder, 21, to place the rest of the numbers. Dividing 21 by 4!, we get 0 remainder 21, so 5 goes in spot 0 and we have:
_ _ 6 _ _ 5
Now we use the remainder, 21, to place the rest of the numbers. Dividing 21 by 3!, we get 3 remainder 3, so 4 goes in spot 3 and we have:
4 _ 6 _ _ 5
Now we use the remainder, 3, to place the rest of the numbers. Dividing 4 by 2!, we get 1 remainder 1, so we place 3 in spot 1
4 _ 6 3 _ 5
Finally, we divide 1 by 1 and get 0 remainder, so 2 goes in spot 1 and we place 1 in the last remaining spot and we are have:
4 2 6 3 1 5
I hope that makes sense. Note that it has the neat property that the permuation 1 2 3 4 5 6 corresponds to index 1 and in general the indices of the permutations are in the same order as the permutations themselves (if the permutations are viewes as six digit numbers).