How can I return all possible permutations of a given array in Java?

226 views Asked by At

Say I have the nested array

gridInterFT=[[1,2],[2,3],[3,4],[1,3],[1,4],[2,4]]

How would I write a function that takes this array as input and returns a 3d nested array containing all possible permutations of the array? I.e. the returned array should look something like:

[[[1,2],[2,3],[3,4],[1,3],[1,4],[2,4]], [[1,3],[2,3],[3,4],[1,2],[1,4],[2,4]], 
[[2,3],[1,2],[3,4],[1,3],[1,4],[2,4]]...]

Are there any libraries which contain functions that do this directly?

1

There are 1 answers

0
AudioBubble On BEST ANSWER
static <T> void swap(T[] a, int i, int j) {
    T t = a[i];
    a[i] = a[j];
    a[j] = t;
}

static void permutation(int[][] perm, int n, List<int[][]> result) {
    int size = perm.length;
    if (n >= size)
        result.add(Arrays.copyOf(perm, size));
    else
        for (int i = n; i < size; i++ ) {
            swap(perm, n, i);
            permutation(perm, n + 1, result);
            swap(perm, n, i);
        }
}
public static int[][][] permutation(int[][] perm) {
    List<int[][]> result = new ArrayList<>();
    permutation(perm, 0, result);
    return result.toArray(new int[0][][]);
}

public static void main(String[] args) {
    int[][] gridInterFT={{1,2},{2,3},{3,4},{1,3},{1,4},{2,4}};
    int[][][] r = permutation(gridInterFT);
    for (int[][] a : r) {
        for (int i = 0; i < a.length; ++i)
            System.out.print(Arrays.toString(a[i]));
        System.out.println();
    }
}