1 package io.github.skenvy;
2
3 import java.util.ArrayList;
4 import java.util.Arrays;
5
6 public class PermutationComputation {
7
8 ArrayList<int[]> permutations;
9
10 public PermutationComputation() {
11 permutations = new ArrayList<int[]>();
12 }
13
14 public static void main(String[] args) {
15 PermutationComputation pc = new PermutationComputation();
16 pc.permutationCompute(1, 5);
17 pc.clear();
18 pc.permutationCompute(2, 5);
19 for (int k = 0; k < pc.permutations.size(); k++) {
20 System.out.println(Arrays.toString(pc.permutations.get(k)));
21 }
22 }
23
24 public void permutationCompute(int subsetSize, int overHowMany) {
25 int[] carry = new int[subsetSize];
26 permutationComputationRoll(subsetSize, overHowMany, 0, carry);
27 }
28
29 public void permutationComputationRoll(int subsetSize, int overHowMany, int level, int[] carry) {
30 if (level == subsetSize) {
31 permutations.add(carry.clone());
32 } else {
33 for (int k = level; k < overHowMany; k++) {
34 if (level == 0) {
35 carry[0] = k;
36 permutationComputationRoll(subsetSize, overHowMany, 1, carry);
37 } else {
38 if (k > carry[level - 1]) {
39 carry[level] = k;
40 permutationComputationRoll(subsetSize, overHowMany, level + 1, carry);
41 }
42 }
43 }
44 }
45 }
46
47 public void clear() {
48 permutations.clear();
49 }
50
51 public int size() {
52 return permutations.size();
53 }
54
55 public int[] get(int index) {
56 return permutations.get(index);
57 }
58
59 public int getget(int index1, int index2) {
60 return permutations.get(index1)[index2];
61 }
62 }