I'm trying to write a recursive method that takes an integer array and copies alternating elements into two separate arrays until there are no elements left to split.
For example:
[0 1 2 3 4 5 6 7 8 9]
...
[0 2 4 6 8]
[1 3 5 7 9] /
[0 4 8]
[2 6] /
[0 8]
[4] /
[0]
[8] /
[2]
[6] /
[1 5 9]
[3 7] /
[1 9]
[5] /
[1]
[9] /
[3]
[7]
So far I have been able to split the initial array, but my recursion won't terminate. Any suggestions?
Here is the method's code:
public static void halve(int[] a, int x)
{
for (int i = 0; i < x; i = i + 2)
{
xList[i/2] = i;
yList[i/2] = i + 1;
}
printList(xList);
printList(yList);
if (x-1 > 2)
halve(xList, xList.length-1);
else if (x-1 > 2)
halve(yList, yList.length-1);
}
xListandyList?