Jump to content

Permutations by swapping: Difference between revisions

m
(J draft)
Line 19:
Meanwhile, here's an inductive approach, using negative integers to look left and positive integers to look right:
 
<lang J>bfjt0bfsjt0=: _1 - i.
lookingat=: 0 >. <:@# <. i.@# + *
next=: | >./@:* | > | {~ lookingat
bfjtnbfsjtn=: (((] <@, ] + *@{~) | i. next) C. ] * _1 ^ next < |)^:(*@next)</lang>
 
Here, bfjt0bfsjt0 N gives the initial permutation of order N, and bfjtnbfsjtn^:M bfjt0Mbfsjt0M 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.<:@|
Line 30:
Example use:
 
<lang J> bfjtnbfsjtn^:(i.!3) bfjt0 3
_1 _2 _3
_1 _3 _2
Line 37:
_2 3 _1
_2 _1 3
<:@| bfjtnbfsjtn^:(i.!3) bfjt0 3
0 1 2
0 2 1
Line 44:
1 2 0
1 0 2
A. <:@| bfjtnbfsjtn^:(i.!3) bfjt0 3
0 1 4 5 3 2</lang>
 
6,962

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.