# Rearrange the array by Recursion

Page 1 of 1

## 5 Replies - 542 Views - Last Post: 06 August 2017 - 01:38 PMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=405621&amp;s=330e97a798dea252dc8bff2236e769de&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 Tea_Coffe

Reputation: 0
• Posts: 3
• Joined: 05-August 17

# Rearrange the array by Recursion

Posted 05 August 2017 - 03:13 PM

I want to Rearrange an array A so all the even values appear before the odd ones by using a recursion method ...

I solved this problem by using an empty array B and three indices n, n1 & n2 where:

n goes from 0 to A.length-1
n1 points to the beginning of the array (n1 will be incremented only an even value is read from array A)
n1 points to the end of the array (n2 will be incremented only an odd value is read from array A)

However Can I solve this problem by two indices:

- I thought of an index i that moves from 0 to n/2 [n is the array A length]

```public static void main(String[] args) {

int [] a = {1,2,3,4,5,6,7,8};
int[]y=new int[a.length];
a = arr(a,0,0,y);
for(int x:a)
System.out.print(x +" ");
}

public static int[] arr(int []x,int n,int i,int []y){

if(n==x.length)
return y;
else{
// even
if(x[n]%2==0)
y[i]=x[n];
// odd
if(x[n]%2==1)
y[x.length-1-i]=x[n];

// (i+1)%(x.length/2) will make
// i moves from 0 to the middle of the array
arr(x,n+1,(i+1)%(x.length/2) ,y);
return y;
}

}

```

Output : 0 6 0 8 0 7 0 5

The problem: values overwritten. how Can I fix that ?

Is This A Good Question/Topic? 0

## Replies To: Rearrange the array by Recursion

### #2 NormR

• D.I.C Lover

Reputation: 672
• Posts: 5,074
• Joined: 25-December 13

## Re: Rearrange the array by Recursion

Posted 06 August 2017 - 06:34 AM

One thing that might help is better variable names than the single letter names.
What are the contents of x,y,n,i?

### #3 Tea_Coffe

Reputation: 0
• Posts: 3
• Joined: 05-August 17

## Re: Rearrange the array by Recursion

Posted 06 August 2017 - 12:12 PM

NormR, on 06 August 2017 - 06:34 AM, said:

One thing that might help is better variable names than the single letter names.
What are the contents of x,y,n,i?

sorry NormR, I coudln't follow what you mean..

x is the unorganized array
y is the output array(Organized)
n is the index (0 ... to x.length-1)
i is an index that goes from 0 to n/2

### #4 NormR

• D.I.C Lover

Reputation: 672
• Posts: 5,074
• Joined: 25-December 13

## Re: Rearrange the array by Recursion

Posted 06 August 2017 - 12:15 PM

Ok, can you get rid of the one letter variable names and replace them with names that describe what the variables contain?

### #5 Tea_Coffe

Reputation: 0
• Posts: 3
• Joined: 05-August 17

## Re: Rearrange the array by Recursion

Posted 06 August 2017 - 01:26 PM

NormR, on 06 August 2017 - 12:15 PM, said:

Ok, can you get rid of the one letter variable names and replace them with names that describe what the variables contain?

``` 	// main method
public static void main(String[] args) {

int [] Unorganized_Array = {1,2,3,4,5,6,7,8};
int[] Empty_Array =new int[Unorganized_Array.length];
Unorganized_Array = arr(Unorganized_Array,0,0,Empty_Array);
for(int x:Unorganized_Array)
System.out.print(x +" ");
}

public static int[] arr(int []Unorganized_Array,int index_Unorganized_Array,int index_Output_Array,int []Output_Array){
// Unorganized_Array = {1,2,3,4,5,6,7,8}
// the output Array will return {2,4,6,8,1,3,5,7} OR any array as long as the even values
// appear before the odd values
if(index_Unorganized_Array==Unorganized_Array.length)
return Output_Array;
else{
// even
if(Unorganized_Array[index_Unorganized_Array]%2==0)
Output_Array[index_Output_Array]=Unorganized_Array[index_Unorganized_Array];
// odd
if(Unorganized_Array[index_Unorganized_Array]%2==1)
Output_Array[Unorganized_Array.length-1-index_Output_Array]=Unorganized_Array[index_Unorganized_Array];

// (i+1)%(x.length/2) will make
// i moves from 0 to the middle of the array
arr(Unorganized_Array,index_Unorganized_Array+1,(index_Output_Array+1)%(Unorganized_Array.length/2) ,Output_Array);
return Output_Array;
}

}

```

### #6 NormR

• D.I.C Lover

Reputation: 672
• Posts: 5,074
• Joined: 25-December 13

## Re: Rearrange the array by Recursion

Posted 06 August 2017 - 01:38 PM

Ok, thanks.
Now, how are you trying to debug the code to see what it is doing?
Add some print statements to see where the values are being moved to.

Should line 22 be an else statement? A number is either even or odd.

This post has been edited by NormR: 06 August 2017 - 01:52 PM