Könnt ihr mir sagen, wie die Methode filling_shift_in_place
auch für zum Beispiel
filling_shift_in_place(new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}, 3);
funktionieren könnte?:
public static void filling_shift_in_place(int[] a, int x) {
if (a.length / x * x == a.length) {
System.out.println("not solvable");
}
int to = a.length - ((x % 2 == 1) ? 1 : 0);
int t2 = a[0];
int first = t2;
int j = 0;
for (int i = 0; i < to; i++) {
int k = getIndex(j, a.length, x);
System.out.println(j + " <-> " + k);
int t1 = a[k];
a[j] = a[k];
a[k] = t2;
t2 = t1;
j = k;
}
a[j] = first;
System.out.println(Arrays.toString(a));
}
public static int getIndex(int i, int n, int x) {
return (i + x) % n;
}
public static void main(String[] args) {
filling_shift_in_place(new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}, 3);
filling_shift_in_place(new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11}, 3);
filling_shift_in_place(new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}, 5);
filling_shift_in_place(new int[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12}, 3);
}
Oder ist das schon theoretisch ausgeschlossen? Danke vorab.