Here's the problem; I need to write a recursive function that outputs permutations for a given input n; such that the output looks like:
Given n=3, you'd get. . .
empty set
1
1 2
1 3
2
2 3
3
1 2 3
The order in which the subsets are generated does not matter.
class genPerms {
public static void main(String[] args){
System.out.print("empty set");
genPerm(3); }
public static void genPerm(int n) {
int[] appendings = new int[0];
recursiveGenPerm(n, appendings);}
public static void recursiveGenPerm(int n, int[] appendings2) {
if(n == 0) {
for(int v = 0; v < appendings2.length; v++){
System.out.print(appendings2[v]);}
System.out.println();}
else{
recursiveGenPerm(n-1, appendings2);
int[] B = new int[appendings2.length+1];
B[0] = n;
for(int c=1; c < B.length; c++){
B[c] = c+1;}
recursiveGenPerm(n-1, B);
}
}}
This is my code thus far, which outputs:
empty set
1
2
12
3
12
22
123
Obviously, I have a problem with 2 of the lines (the second 12 and the 22), any ideas on how to fix this? I think it's a problem with how I copy appendings to B but I'm not positive. Any help would be appreciated.