Permutations by swapping: Difference between revisions

Content added Content deleted
(J draft)
Line 19: Line 19:
Meanwhile, here's an inductive approach, using negative integers to look left and positive integers to look right:
Meanwhile, here's an inductive approach, using negative integers to look left and positive integers to look right:


<lang J>bfjt0=: _1 - i.
<lang J>bfsjt0=: _1 - i.
lookingat=: 0 >. <:@# <. i.@# + *
lookingat=: 0 >. <:@# <. i.@# + *
next=: | >./@:* | > | {~ lookingat
next=: | >./@:* | > | {~ lookingat
bfjtn=: (((] <@, ] + *@{~) | i. next) C. ] * _1 ^ next < |)^:(*@next)</lang>
bfsjtn=: (((] <@, ] + *@{~) | i. next) C. ] * _1 ^ next < |)^:(*@next)</lang>


Here, bfjt0 N gives the initial permutation of order N, and bfjtn^:M bfjt0M gives the Mth Steinhaus–Johnson–Trotter permutation of order N.
Here, bfsjt0 N gives the initial permutation of order N, and bfsjtn^:M bfsjt0M gives the Mth Steinhaus–Johnson–Trotter permutation of order N. (bf stands for "brute force".)


To convert from the Steinhaus–Johnson–Trotter representation of a permutation to J's representation, use <:@|, or to find J's permutation index of a Steinhaus–Johnson–Trotter representation of a permutation, use A.<:@|
To convert from the Steinhaus–Johnson–Trotter representation of a permutation to J's representation, use <:@|, or to find J's permutation index of a Steinhaus–Johnson–Trotter representation of a permutation, use A.<:@|
Line 30: Line 30:
Example use:
Example use:


<lang J> bfjtn^:(i.!3) bfjt0 3
<lang J> bfsjtn^:(i.!3) bfjt0 3
_1 _2 _3
_1 _2 _3
_1 _3 _2
_1 _3 _2
Line 37: Line 37:
_2 3 _1
_2 3 _1
_2 _1 3
_2 _1 3
<:@| bfjtn^:(i.!3) bfjt0 3
<:@| bfsjtn^:(i.!3) bfjt0 3
0 1 2
0 1 2
0 2 1
0 2 1
Line 44: Line 44:
1 2 0
1 2 0
1 0 2
1 0 2
A. <:@| bfjtn^:(i.!3) bfjt0 3
A. <:@| bfsjtn^:(i.!3) bfjt0 3
0 1 4 5 3 2</lang>
0 1 4 5 3 2</lang>