Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You need to understand <strong>mathematical induction</strong> to really grasp the concept. Once it is understood recursion is simply straightforward. Consider a simple function ,</p> <pre><code> def fun(a): if a == 0: return a else return a + 10 </code></pre> <p>what does the return statement do here? It simply returns <code>a+10</code>. Why is this easy to understand? Of course, one reason is that it doesn't have recursion.;) Why is the return statement so easy to understand is that it has a and 10 available when it is called.</p> <p>Now, consider a simple <code>sum of n numbers</code> program using recursion. Now, one important thing before coding a recursion is that you must understand how mathematically it is supposed to work. In the case of sum of <code>n</code> numbers we know that if <code>sum</code> of <code>n-1</code> numbers is known we could return that <code>sum + n</code>. Now what if do not know that <code>sum</code>. Well, we find sum of <code>n-2</code> terms and add <code>n-1</code> to it.</p> <p>So, <code>sumofN(n) = n + sum(n-1)</code>. </p> <p>Now, comes the terminating part. We know that this cant go on indefinitely. Because <code>sumofN(0) = 0</code> </p> <p>so, </p> <pre><code> sumofN(n) = 0, if n = 0, n + sum(n-1) , otherwise </code></pre> <p>In code this would mean,</p> <pre><code> def sumofN(n): if n == 0: return 0 return n + sum(n-1) </code></pre> <p>Here suppose we call sumofN(10). It returns <code>10 + sumofN(9)</code>. We have <code>10</code> with us. What about the other term. It is the return value of some other function. So what we do is we wait till that function returns. Here, since the function being called is nothing but itself, it waits till sumofN(9) returns. And when we reach 9 + sumofN(8) it waits till sumofN(8) returns. </p> <p>What actually happens is</p> <blockquote> <p>10 + sumofN(9) , which is<br> 10 + 9 + sumofN(8), which is<br> 10 + 9 + 8 + sumofN(7) ..... </p> </blockquote> <p>and finally when sumofN(0) returns we have, </p> <blockquote> <p>10 + 9 + 8 + 7 + 6 + 5 + 4 + 3 + 2 + 1 + 0 = 55 </p> </blockquote> <p>This concept is all that is needed to understand recursion. :). Now, what about mergesort?</p> <pre><code> mergesort(someArray) = { l = mergesort the left part of array, r = mergesort the right part of the array, merge(l and r) } </code></pre> <p>Until the left part is available to be returned, it goes on calling mergesort on the "leftest" arrays. Once we have that, we find the right array which indeed finds the "leftest" array. Once we have a left and right we merge them. </p> <p>One thing about recursion is that it is so damn easy once you look at it from the right perspective and that right perspective is called <strong>mathematical induction</strong></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload