Results 1 to 5 of 5
  1. #1
    chupalo17 is offline Member
    Join Date
    Sep 2009
    Posts
    2
    Rep Power
    0

    Default 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.

    Java 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);
    		}
    	}	
    }

  2. #2
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default

    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.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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

  4. #4
    Mr.Beans's Avatar
    Mr.Beans is offline Senior Member
    Join Date
    Apr 2009
    Location
    Virginia Beach, Virginia
    Posts
    149
    Rep Power
    0

    Default

    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.

    However, I will try to help you help yourself find the answer.

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

    Java 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
    Java 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
    Last edited by Mr.Beans; 09-07-2009 at 07:07 AM.

  5. #5
    chupalo17 is offline Member
    Join Date
    Sep 2009
    Posts
    2
    Rep Power
    0

    Default

    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

    Java 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;
                }
        }       
    }

Similar Threads

  1. Java Recursive method problem
    By kj2009 in forum Advanced Java
    Replies: 2
    Last Post: 02-25-2009, 04:19 PM
  2. exercise of recursive method
    By amexudo in forum New To Java
    Replies: 2
    Last Post: 03-09-2008, 06:55 PM
  3. Replies: 1
    Last Post: 02-16-2008, 10:10 PM
  4. Replies: 2
    Last Post: 02-16-2008, 09:52 PM
  5. Recursive Method
    By bluegreen7hi in forum New To Java
    Replies: 5
    Last Post: 11-29-2007, 05:45 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •