so title says it all, I've been trying to figure out a recursive way to "shift" an array - rotate it.
I have found a way but I've been told its not good since its using a temp... so here I am asking you for your advice for a recursive procedure that doesnt use a temp(temporary parameter).
here's my code so far:
Code:
public static void moving(int[]a,int length,int temp)
{
if(length==0)
System.out.print("");
else
{
temp=a[a.length-length-1];
a[a.length-length-1]=a[a.length-1]
a[a.length-1]=temp;
moving(a,length-1,temp);
}
}
so I put a bit more thought into this and found out the way I was aiming to:
Code:
public static void moveLikeJagger(int[] a,int length,int k)
{
if(length==0)
a[length]=k;
else
{
a[length]=a[length-1];
moveLikeJagger(a,length-1,k);
}
}
the main should look like this:
Code:
public static void main(String[] args)
{
System.out.println("enter array's length");
int n=reader.nextInt();//reader is the Scanner name
int[] a=new int[n];
moveLikeJagger(a,a.length-1,a[a.length-1]);
}
Have fun using that code, all rights reserved to me (at least give me credits!)