Help on Recursion
Hi everyone,
I'm doing a program to do the branch tree.
ex: the order of the numbers 1234
can be:
1234 , 1243, 1324, 1342, 1423, 1432, 2134, 2143, 2314, 2341, 2413, 2431,3124, 3142,3214,3241,3412,3421,4123,4132,4213,4231,4312,4321
I did a method that gives the possible orders of 123456 like this:
public static voidswapAll6(int[] array)
{
int n = array.length-1;
int[] array1 = newint[n+1];
int[] array4 =new int[n+1];
int[] array5 =new int[n+1];
int i = 1;
for(int b5 = n-5; b5 <= n; b5++)
{
int a = n-5;
putAinB(array, a, b5);
putAinB(array5, a, b5);
if(b5==n-5)
System.arraycopy(array,0,array5,0,6);
else if(b5 > n-5)
{
System.arraycopy(array5,0,array,0,6);
}
for(int b4 = n-4; b4 <=n ; b4++)
{
a = n-4;
putAinB(array, a, b4);
putAinB(array4, a, b4);
if(b4==n-4)
System.arraycopy(array,0,array4,0,6);
else if(b4 > n-4)
{
System.arraycopy(array4,0,array,0,6);
}
//putAinB(array, a, b4);
for(int b3 = n-3; b3 <= n; b3++)
{
a = n-3;
if(b3>n-3)
System.arraycopy(array1,0,array,0,6);
putAinB(array, a, b3);
for(int b2 = n-2; b2 <= n ; b2++)
{
a = n-2;
putAinB(array, a, b2);
if(b2 == n-2)
System.arraycopy(array,0,array1,0,6);
for(int b1 = n-1; b1<=n ; b1++)
{
a= n-1;
putAinB(array, a, b1);
if( b1 == n-1)
{
System.out.print(i);
System.out.print("\t");
printArray(array);
i++;
}
else if(b1 == n)
{
System.out.print(i);
System.out.print("\t");
printArray(array);
putAinB(array, a, b1);
i++;
}//end else
}//end for(n-1)
}//end for(n-2)
}//end for(n-3)
}//end for(n-4)
}//end for(n-5)
}//end swapAll6()
public static void putAinB(int[] array, int a, int b)
{
int temp = array[a];
array[a] = array;
array = temp;
}
but every time I want to change the numbers say 123456789 10 11 12...300 it would be a miss so I thought of recursion but it didn't work with me so anybody can help me in it. It was like this
public static void swapRecursion(int[] array, int a, int b,int end)
{
int n = array.length-1;
int[] array1 = new int[n+1];
//System.arraycopy(array,0,array1,0,n+1);
while(end >0)
{
int j = b;
for(int i = j; i <= n; i++)
{
putAinB(array,a,i);
System.arraycopy(array,0,array1,0,n+1);
if(a == n-1 && i== n-1)
{
printArray(array);
}//enf if(a == n-1)
if(a == n-1 && i == n)
{
printArray(array);
putAinB(array,a,i);
}//end if(a == n-1 && i == n)
//a++;
//end--;
if(a < n-1)
{
b=a;
swapRecursion(array1,++a,++b,--end);
}
}//end for
}//end while
}//end swapRecursion
thanks

