Permutations by swapping: Difference between revisions

(adding Java implementation)
Line 1,263:
 
import java.util.Arrays;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
 
Line 1,294 ⟶ 1,293:
 
void output(Object[] array, boolean plus) {
System.out.println('[' + Arrays.streamtoString(array) + (plus ? " +1" : " -1"));
.map(String::valueOf)
.collect(Collectors.joining(", ")) + ']' + (plus ? " +1" : " -1"));
}
 
Line 1,311 ⟶ 1,308:
void loop(Object[] array, int n) {
int[] c = new int[n];
for (int i = 0; i < n; i++) {
c[i] = 0;
}
output(array, true);
boolean plus = false;
for (int i = 0; i < n; ) {
while (i < n) {
if (c[i] < i) {
if (i % 2 == 0) {
Line 1,325 ⟶ 1,318:
}
output(array, plus);
plus ^= true!plus;
c[i]++;
i = 0;
Line 1,334 ⟶ 1,327:
}
}
}</lang>
}
{{out}}
</lang>
<pre>
 
<lang Java>[0, 1, 2, 3] +1
[1, 0, 2, 3] -1
[2, 0, 1, 3] +1
Line 1,386 ⟶ 1,379:
[1, 0, 2, 3] +1
[0, 1, 2, 3] -1
</langpre>
 
 
=={{header|jq}}==
Anonymous user