# Recursive method using int array, help needed

• 09-07-2009, 05:35 AM
chupalo17
Recursive method using int array, help needed
I was wondering if anyone had a few minutes to look over this code. I'm trying to make a recursive method that will take an array of int and sort the elements so that all even values appear before the odd values.

This is not my homework. I am actually trying to teach myself Java. I am not looking for a solution but maybe someone has a clue on what knowledge I might be missing when it comes to using arrays in a recursive method. I feel this code should be simpler but I can't figure out how to handle the array as I test values. What I am trying to attempt in the code below is test the last element and then place it at the beginning or end of the array depending on if it's true or false but as I call the method I get stumped trying to handle how to adjust the original array. I have no problem handling this with strings as a string can easily be made and appended but arrays are a different animal.

Thanks in advance for anyone who has time to take a look and point me in the right direction.

Code:

```import java.util.Arrays; public class SortTest {         public static void main(String[] args){                 int[] array2 = {1,2,3,4,5,6};                 SortTest.sortEvenBeforeOdd(array2);                 System.out.println(Arrays.toString(array2));         }         public static void sortEvenBeforeOdd(int[] array){                 int arrayLength = array.length;                 if(arrayLength == 0 || arrayLength == 1){return;}                                 int x = array[arrayLength-1]%2;                                 int[] newArray = new int[arrayLength-1];                 for(int j=0;j<newArray.length;j++)                         newArray[j] = array[j];                                 if(x == 0){                                                 int temp1 = array[arrayLength-1];                                                 for(int i=arrayLength-1; i>0; i--){                                 array[i] = array[i-1];}                         array[0] = temp1;                         sortEvenBeforeOdd(newArray);                         }                 if(x == 1){                                                 int temp2 = array[0];                         for(int i=1; i<arrayLength-1; i++){                                 array[i-1] = array[i];}                         array[arrayLength-1] = temp2;                         sortEvenBeforeOdd(newArray);                 }         }        }```
• 09-07-2009, 05:46 AM
Just a quick question -- have you written other recursive algorithms before? Like fibonacci sequence, factorial or sum of numbers? This is a more complex algorithm, and if its your first recursive one, it might be tricky to start on. If you have not done other recurisve stuff before (though it sounds like you have) look up what I mentioned on wiki if you're unfamiliar.
• 09-07-2009, 05:47 AM
Fubarable
Welcome to the forum. Please note the forum policy on cross-posting. you should notify forum members when this is done so that they know what has been discussed about this elsewhere and so they don't duplicate effort. Thanks for your cooperation and much luck.

Cross-post: New To Java - Re: Recursive method using int array, help needed
• 09-07-2009, 06:01 AM
Mr.Beans
I wrote a method (didn't test it however). I'm fairly confident it will work though. Since I want you to actually learn, I won't post my code.

When I wrote my code the signature of my recursive method was

Code:

`public void sortEvenBeforeOdd(int[] array, int pos) { ... }`
In my method, I would use pos to indicate where in the array I would be checking.

If the item in the position pos was odd, I would start at the end of the array and iterate down looking for an even one. If I found an even one I switched it's place with the item at pos. I then called
Code:

`sortEvenBeforeOdd(array, pos + 1);`
Maybe you can try and implement this algorithm yourself. If you run into troubles, paste the code and explain what you are having trouble with.

Also note that there is always tons of information on the internet. The page here at wikipedia does a great job at explaining how recursive methods work. It even has an example in java.

If you can't see the url because of your post count just paste /wiki/Recursion_(computer_science) after wikipedia.org

Mr. Beans
• 09-07-2009, 11:15 PM
chupalo17
quad64bit: I have been writing recursive functions lately and doing fairly well up until this array problem. I have successfully done the factorial and sum of numbers functions. The Fib sequence is actually next on my list (maybe should've done that before this one).

Fubarable: I guess I'm not clear on what a cross post is. I am new to java so thought this forum was appropriate. My apologies for any distractions.

Mr.Beans: thanks for looking through the code and helping me find the right direction. My first instinct was to pass a second variable to help iterate through like you suggested but was hoping that I could accomplish this without the need for a second variable. I think I've stumbled on code that works without a second parameter. Let me know what you think. My concern is that it works for wrong reasons. I really appreciate the helpful comments and focus.

FJ

Code:

```import java.util.Arrays;     public class SortTest2 {     public static void main(String[] args){         int[] array2 = {1,3,5,7,9,2,4,6,8};         sortEvenBeforeOdd(array2);         System.out.println(Arrays.toString(array2));     }       public static void sortEvenBeforeOdd(int[] array){         if(array.length == 1){return;}         int[] subArray = new int[array.length-1];         for(int j=0;j<subArray.length;j++)             subArray[j] = array[j];                 if(array[array.length-1]%2 == 0){             array[0] = array[array.length-1];             sortEvenBeforeOdd(subArray);             for(int j=0;j<array.length-1;j++)                 array[j+1] = subArray[j];             return;             }                       else{             sortEvenBeforeOdd(subArray);             for(int j=0;j<array.length-1;j++)                 array[j] = subArray[j];             return;             }     }      }```