-
Notifications
You must be signed in to change notification settings - Fork 0
/
bench_fannkuchredux.java
168 lines (149 loc) · 3.07 KB
/
bench_fannkuchredux.java
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
class Math {
public int pow(int v, int exp){
if (exp < 0){
return 1 / pow(v, -exp);
} else if (exp == 0){
return 1;
} else {
int ret = 1;
while (exp > 0){
if (exp % 2 == 0){
v = v * v;
exp = exp / 2;
} else {
ret = ret * v;
exp = exp - 1;
}
}
return ret;
}
}
public int factorial(int val){
int ret = 1;
int sign = signum(val);
if (val < 0){
val = -val;
}
if (val == 0){
return 1;
}
while (val > 0){
ret = ret * val;
val = val - 1;
}
return ret * sign;
}
public int min(int s, int t){
if (s < t){
return s;
}
return t;
}
public int max(int s, int t){
if (s > t){
return s;
}
return t;
}
public int lengthInChars(int num){
int len = 1;
if (num < 0){
len = len + 1;
num = -num;
}
while (num > 10){
num = num / 10;
len = len + 1;
}
return len;
}
public int signum(int num){
if (num == 0){
return 0;
}
if (num < 0){
return -1;
}
return 1;
}
public int abs(int num){
if (num < 0){
return -num;
}
return num;
}
}
/*
Adapted from the following source:
The Computer Language Benchmarks Game
http://shootout.alioth.debian.org/
contributed by Isaac Gouy
converted to Java by Oleg Mazurov
*/
class fannkuchredux {
public int maxFlipsCount;
public void fannkuch(int n) {
int[] perm = new int[n];
int[] perm1 = new int[n];
int[] count = new int[n];
maxFlipsCount = 0;
int permCount = 0;
int checksum = 0;
int i = 0;
while (i < n){
perm1[i] = i;
i = i + 1;
}
int r = n;
while (true) {
while (r != 1){ count[r-1] = r; r = r - 1; }
i = 0;
while (i < n){
perm[i] = perm1[i];
i = i + 1;
}
int flipsCount = 0;
int k = 0;
while ( !((k=perm[0]) == 0) ) {
int k2 = (k+1) / 2;
int j = 0;
while (j < k2){
int temp = perm[j]; perm[j] = perm[k-j]; perm[k-j] = temp;
j = j + 1;
}
flipsCount = flipsCount + 1;
}
maxFlipsCount = new Math().max(maxFlipsCount, flipsCount);
if (permCount % 2 == 0){
checksum = checksum + flipsCount;
} else {
checksum = checksum - flipsCount;
}
/* Use incremental change to generate another permutation */
boolean doBreak = false;
while (!doBreak) {
if (r == n) {
System.out.println( checksum );
return;
}
int perm0 = perm1[0];
int l = 0;
while (l < r) {
int j = l + 1;
perm1[l] = perm1[j];
l = j;
}
perm1[r] = perm0;
count[r] = count[r] - 1;
if (count[r] > 0) doBreak = true;
else r = r + 1;
}
permCount = permCount + 1;
}
}
public static void main(String[] args){
fannkuchredux fk = new fannkuchredux();
fk.fannkuch(11);
System.out.println(fk.maxFlipsCount);
}
}