-
Notifications
You must be signed in to change notification settings - Fork 4
/
sjt_perm.go
87 lines (77 loc) · 1.46 KB
/
sjt_perm.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
// https://www.cut-the-knot.org/Curriculum/Combinatorics/JohnsonTrotter.shtml
// https://en.wikipedia.org/wiki/Steinhaus%E2%80%93Johnson%E2%80%93Trotter_algorithm
// generate a permutation of given a initial configuration
package main
import (
"bufio"
"flag"
"fmt"
"io"
"math"
"math/rand"
"os"
"time"
)
type dint struct {
index int
value int
dir bool
}
func main() {
rand.Seed(time.Now().UnixNano())
N := 10
flag.IntVar(&N, "n", N, "permutation size")
flag.Parse()
w := bufio.NewWriter(os.Stdout)
defer w.Flush()
p := dints(N)
for i := 1; ; i++ {
dprint(w, i, p)
x, y, v := largest(p)
if x == -1 {
break
}
p[x], p[y] = p[y], p[x]
reverse(p, v)
}
}
func dints(n int) []dint {
p := make([]dint, n)
for i := range p {
p[i] = dint{i, rand.Intn(math.MaxInt32), false}
}
return p
}
func largest(p []dint) (int, int, int) {
x, y, v := -1, -1, -1
for i := range p {
switch {
case i > 0 && !p[i].dir && p[i].index > p[i-1].index && p[i].index > v:
x = i
y = i - 1
v = p[i].index
case i < len(p)-1 && p[i].dir && p[i].index > p[i+1].index && p[i].index > v:
x = i
y = i + 1
v = p[i].index
}
}
return x, y, v
}
func reverse(p []dint, v int) {
for i := range p {
if p[i].index > v {
p[i].dir = !p[i].dir
}
}
}
func dprint(w io.Writer, n int, p []dint) {
fmt.Fprintf(w, "%d [", n)
for i := range p {
fmt.Fprintf(w, "%v", p[i].index)
if i+1 < len(p) {
fmt.Fprintf(w, " ")
}
}
fmt.Fprintf(w, "]\n")
}