Building N-element permutations
To generate permutations of N elements by repeatedly rotating 3 elements in the same direction, you can build a 4-element permutation from the 3-element rotation, then a 5-element permutation from the 4-element permutation, and so on, until you reach N elements.
E.g. the 3-element rotation is as follows:
012
<<< 120
<<< 201
The 4-element permutation repeatedly uses the 3-element rotation, alternated by a rotation in which the 4th element is rotated:
0123
<<< 1203
<<< 2013
<<=< 0312
<<< 3102
<<< 1032
<<=< 0231
<<< 2301
<<< 3021
=<<< 3210
<<< 2130
<<< 1320
(where <
is the postion of an element that is rotated, and =
is the position of an element that remains in place.)
The 5-element permutaton repeats the 4-element permutation, alternated by a rotation in which the 5th element is rotated:
01234 <=<=< 23401 <=<=< 40123 <=<=< 12340 <=<=< 34012
<<< 12034 <<< 34201 <<< 01423 <<< 23140 <<< 40312
<<< 20134 <<< 42301 <<< 14023 <<< 31240 <<< 03412
<<=< 03124 <<=< 20341 <<=< 42013 <<=< 14230 <<=< 31402
<<< 31024 <<< 03241 <<< 20413 <<< 42130 <<< 14302
<<< 10324 <<< 32041 <<< 04213 <<< 21430 <<< 43102
<<=< 02314 <<=< 24031 <<=< 41203 <<=< 13420 <<=< 30142
<<< 23014 <<< 40231 <<< 12403 <<< 34120 <<< 01342
<<< 30214 <<< 02431 <<< 24103 <<< 41320 <<< 13042
=<<< 32104 =<<< 04321 =<<< 21043 =<<< 43210 =<<< 10432
<<< 21304 <<< 43021 <<< 10243 <<< 32410 <<< 04132
<<< 13204 <<< 30421 <<< 02143 <<< 24310 <<< 41032
Defining an N-element permutation
For each step from N-1 to N elements, there are several options, each with their own end-state. Each step is thus defined by N-1 rotations, and together they define an N-element permutation; e.g. for the above example:
3 ELEMENTS
rotations: <<< <<<
complete permutation: 012 -> 201
4 ELEMENTS
rotations: <<=< <<=< =<<<
complete permutation: 0123 -> 1320
5 ELEMENTS
rotations: <=<=< <=<=< <=<=< <=<=<
complete permutation: 01234 -> 41032
Selecting rotations
You'll see in the above examples that the 4-element permutation doesn't use 3 identical rotations, but instead <<=<
, again <<=<
and finally =<<<
; that is because not every combination of possible rotations creates a correct permutation.
To find which rotations you can use for an N-element permutation, look at the end-state of the N-1-element permutation, and see which cycles it contains, e.g.:
Permutation 0123 -> 1320
has two cycles: [031]
and [2]
, because position 0 moves to position 3, position 3 moves to position 1, and position 1 moves to position 0, while position 2 stays in place.
Permutation 0123 -> 3210
has two cycles: [03]
and [12]
, because 0 and 3 switch places, and 1 and 2 switch places.
case: multiple cycles
To create an N-element permutation from an N-1-element permutation, you need to rotate two elements from the first N-1 elements with the N-th element. Check which cycles the N-1-element permutation has, and select rotation points at position 0, and at a position in a second cycle. Repeat this rotation as many times as there are elements in the second cycle, then move the second point to a position in the third cycle (if there is one), and repeat this as many times as there are elements in the third cycle, and so on. When all cycles have been used, repeat the last rotation as necessary.
An example will make this clearer:
N-1-permutation: 012345 -> 254301
cycles: [042], [15], [3]
0123456 ... 2543016
<<====< 5643012 ... 4103562 (positions 0 and 1, for cycle [15])
<<====< 1203564 ... 0653124 (repeat for second element in [15])
<==<==< 3654120 ... 5214360 (positions 0 and 3, for cycle [3])
<==<==< 4210365 ... 1630425 (done; repeat last rotation till end)
<==<==< 0635421 ... 3245061
<==<==< 5241063 ... 4601523
As you will notice, in the case of 2 cycles, the rotation stays the same throughout:
N-1-permutation: 012345 -> 354201
cycles: [0423], [15]
0123456 ... 3542016
<<====< 5642013 ... 2104563 (positions 0 and 1, for cycle [15])
<<====< 1304562 ... 4650132 (repeat for second element in [15])
<<====< 6250134 ... 0315624 (done; repeat last rotation till end)
<<====< 3415620 ... 5261340
<<====< 2061345 ... 1436205
<<====< 4536201 ... 6023451
case: one cycle
Find two positions X and Y, where X is to the left of Y, the N-1 permutation moves X to Y, and Y is not the right-most position in the N-1 permutation. Then rotate positions X and Y with the Nth element repeatedly, and for the final step rotate Y and the right-most position.
Again, an example will make this clearer:
N-1-permutation: 012345 -> 153024
cycles: [032451]
positions X and Y: e.g. 0 and 3, because 0 moves to 3 and 3 < 5 (other option: 2 and 4)
0123456 ... 1530246
<==<==< 0536241 ... 5460321 (positions X and Y)
<==<==< 0461325 ... 4210635 (repeat until penultimate rotation)
<==<==< 0215634 ... 2350164
<==<==< 0354162 ... 3640512
<==<==< 0642513 ... 6120453
===<=<< 6125430 ... 1356240 (last rotation: position Y and right-most)
There is an edge case when the N-1 permutation consists of 1-position shifts in the same direction as the rotation; in this case, alternate between positions 0 and 1 and positions 1 and 2:
N-1-permutation: 012345 -> 123450
cycles: [054321]
0123456 ... 1234506
<<====< 2634501 ... 6345021
=<<===< 6415023 ... 4150263
<<====< 1350264 ... 3502614
=<<===< 3042615 ... 0426135
<<====< 4526130 ... 5261340
=<<===< 5601342 ... 6013452
Example of rotation selection
Here is an example for permutations up to 10 elements; there are many options, but this one shows an interesting repeating pattern starting from 7 elements (compare the rotations used for 7 and 9 elements and for 8 and 10 elements, and also the end state for 7 and 9 elements):
THREE ELEMENTS (3 permutations)
012
<<< 120
<<< 201 = 1 cycle: [012] X=0, Y=1
FOUR ELEMENTS (12 permutations)
0123 ... 2013
<<=< 0312 ... 1032
<<=< 0231 ... 3021
=<<< 3210 ... 1320 = 2 cycles: [031][2] X=0, Y=2
FIVE ELEMENTS (60 permutations)
01234 ... 13204
<=<=< 23401 ... 30421
<=<=< 40123 ... 02143
<=<=< 12340 ... 24310
<=<=< 34012 ... 41032 = 3 cycles: [024][1][3] X=0, Y=1,3
SIX ELEMENTS (360 permutations)
012345 ... 410325
<<===< 150324 ... 251304
<==<=< 351402 ... 053412
<==<=< 453210 ... 154230
<==<=< 254031 ... 352041
<==<=< 052143 ... 450123 = 2 cycles: [024][135] X=0, Y=1
SEVEN ELEMENTS (2,520 permutations)
0123456 ... 4501236
<<====< 5601234 ... 2356014
<<====< 3456012 ... 0134562
<<====< 1234560 ... 5612340
<<====< 6012345 ... 3460125
<<====< 4560123 ... 1245603
<<====< 2345601 ... 6023451 = 5 cycles: [016][2][3][4][5]; X=0, Y=2,3,4,5
EIGHT ELEMENTS (20,160 permutations)
01234567 ... 60234517
<=<====< 20734516 ... 12734506
<==<===< 32764501 ... 03764521
<===<==< 43761520 ... 24761530
<====<=< 54761032 ... 35761042
<====<=< 05761243 ... 40761253
<====<=< 20761354 ... 52761304
<====<=< 32761405 ... 03761425 = 2 cycles: [0][1457263]; X=0, Y=1
NINE ELEMENTS (181,440 permutations)
012345678 ... 037614258
<<======< 387614250 ... 365281740
<<======< 605281743 ... 624708513
<<======< 234708516 ... 271530486
<<======< 761530482 ... 758463102
<<======< 528463107 ... 540126837
<<======< 470126835 ... 413872065
<<======< 153872064 ... 186057324
<<======< 846057321 ... 802345671 = 7 cycles: [018][2][3][4][5][6][7]; X=0, Y=2,3,4,5,6,7
TEN ELEMENTS (1,814,400 permutations)
0123456789 ... 8023456719
<=<======< 2093456718 ... 1293456708
<==<=====< 3298456701 ... 0398456721
<===<====< 4398156720 ... 2498156730
<====<===< 5498106732 ... 3598106742
<=====<==< 6598102743 ... 4698102753
<======<=< 7698102354 ... 5798102364
<======<=< 3798102465 ... 6398102475
<======<=< 4398102576 ... 7498102536
<======<=< 5498102637 ... 3598102647 = 2 cycles: [051483][2679]; X=0, Y=2
Generating the permutations
Generating permutations is done by first selecting which rotations to use, and then executing the rotations using the following sequence, in which every third 3 is replaced by a 4, every fourth 4 is replaced by a 5, every fifth 5 is replaced by a 6, and so on:
3,3,4,3,3,4,3,3,4,3,3,5,3,3,4,3,3,4,3,3,4,3,3,5,3,3,4,3,3,4,3,3,4,3,3,5,3,3,4,3,3,4,3,3,4,3,3,5,3,3,4,3,3,4,3,3,4,3,3,6...
which is similar to Ehrlich's sequence, in that you can use the first 2 rotations to generate the 3 permutations of 3 elements, or the first 11 for the 12 permutations for 4 elements, or the first 59 for the 60 permutations of 5 elements, or in general: N!/2-1 rotations to generate N!/2 permutations.
(update: for an implementation, see my other answer to this question.)
Unreachable permutations
As was mentioned in the question and comments, only half of the possible permutations can be generated using 3-element rotation. Every permutation generated with the method above has an unreachable companion permutation, which can be created by switching the first two elements, e.g.:
0123 (1023)
<<< 1203 (2103)
<<< 2013 (0213)
<<=< 0312 (3012)
<<< 3102 (1302)
<<< 1032 (0132)
<<=< 0231 (2031)
<<< 2301 (3201)
<<< 3021 (0321)
=<<< 3210 (2310)
<<< 2130 (1230)
<<< 1320 (3120)