Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Logic of Recursion in General

  1. #1
    Junior Member
    Join Date
    Jul 2011
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Wink Logic of Recursion in General

    Hello Everyone,

    I'm just starting to get into Recursions and Sorting Algorithms and all that jazz, and I'm having a hard time getting my head around the idea of a
    method calling itself until a point. Well, I understand that part, but it's simply the syntax I don't get. Specifically, the factorial recursive method.


    public int factorial(int i){
     
    if(i!=1){
    return (i * factorial(i-1));
    }
    else{
    return 1;   
    }        
     
    This is the part I don't understand. At some point in time, any integer decreasing will become 1. Doesn't that mean that thiswould always return 1? 
     
    It's difficult to explain. Pretend that I'm running the number "3" through this method. 3 is not equal to one, so it continues by multiplying 3 by factorial of (3-1) which is 2. So right now we have 3*2, and 2 is not equal to one, so it also continues by saying 2* factorial of (2-1) which is 1. 1 is equal to 1, so it breaks this "loop" of multiplying by simply returning 1. But what happened to the (3*2) part?It seems like by stating another return statement, that part is discarded, but it is for some reason multiplied by 1.

    :/ Any help?

    Thanks,

    Jake
    Last edited by Jakesta42; December 3rd, 2011 at 02:23 PM.


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,139
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: Logic of Recursion in General

    Remember the return mechanism has the previous results remaining on the stack when it finally returns the 1. As the returns are undone, each returned value is multiplied by i and returned to its caller to do the same again.

  3. The Following User Says Thank You to Norm For This Useful Post:

    Jakesta42 (December 3rd, 2011)

  4. #3
    Junior Member
    Join Date
    Jul 2011
    Posts
    10
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Logic of Recursion in General

    Quote Originally Posted by Norm View Post
    Remember the return mechanism has the previous results remaining on the stack when it finally returns the 1. As the returns are undone, each returned value is multiplied by i and returned to its caller to do the same again.
    Thanks for the reply! Yes, that actually puts what I don't understand in perfect words. How is it that the entire "stack" is returned? The factorial method makes more sense now that you said that, but the recursive call for merge sort is still puzzling me. It goes something like this:


    //In the method static int[] mergeSort()
    int[] left = mergeSort()

    This is puzzling, because I know that in the end groups of arrays will be returned. However, the return type of the method is a single array. It's slightly mind boggling the fact that an entire "stack" of arrays is returned, rather than just one array. I'm just trying to get my head around that I suppose.

  5. #4
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Logic of Recursion in General

    Somewhere in the code it will actually merge 2 arrays together.
    Improving the world one idiot at a time!

Similar Threads

  1. need logic
    By arunjib in forum Java Theory & Questions
    Replies: 3
    Last Post: November 27th, 2011, 05:39 PM
  2. Problem with the Logic
    By sudh in forum Java Theory & Questions
    Replies: 4
    Last Post: March 31st, 2011, 07:21 AM
  3. whats logic behind this?
    By X0X0 in forum What's Wrong With My Code?
    Replies: 0
    Last Post: February 26th, 2011, 02:48 PM
  4. Help with java logic
    By dever in forum What's Wrong With My Code?
    Replies: 7
    Last Post: February 7th, 2011, 08:41 AM
  5. Simple recursion logic
    By chronoz13 in forum Algorithms & Recursion
    Replies: 3
    Last Post: December 24th, 2009, 10:53 PM

Tags for this Thread