Permutations.java
I thought I might like to do this but does anyone know of a more efficient way to do this?
private Object[][] resulting2DArray;
public static void perm2(Object[] s) {
int N = s.length;
resulting2DArray = new Object[N][N];
Object[] a = new Object[N];
for (int i = 0; i < N; i++)
a[i] = s[i];
perm2(a, N);
return resulting2DArray;
}
private static void perm2(Object[] a, int n) {
if (n == 1) {
//some how dump the contents into the 2D array "resulting2DArray"
return;
}
for (int i = 0; i < n; i++) {
swap(a, i, n-1);
perm2(a, n-1);
swap(a, i, n-1);
}
}
// swap the characters at indices i and j
private static void swap(Object[] a, int i, int j) {
Object c;
c = a[i]; a[i] = a[j]; a[j] = c;
}
Does anyone have a brilliant idea?


Sign In
Create Account


Back to top









