See Knuth volume 4A on generating permutations.