java - Understanding double recursion -
i'm able comprehend recursion if there 1 recursive call within function. however, getting confused when see 2 or more recursive calls within same function. example:
int maximumelement(int array[], int index, int n) { int maxval1, maxval2; if ( n==1 ) return array[index]; maxval1 = maximumelement(array, index, n/2); maxval2 = maximumelement(array, index+(n/2), n-(n/2)); if (maxval1 > maxval2) return maxval1; else return maxval2; }
i understand 1 thing n gets decremented half during each recursive call. don't understand how next recursive call works. gets confusing , understanding until point falls apart , give up. thankful if please illustrate manually neat example. did programming, , printed outputs. however, don't understand how calculations behind work. here understanding until point gets nothing:
int a[] = {1,2,10,15,16,4,8}
initial call: maximumelement(a, 0, 7)
the function begins: first call: maximumelement(a, 0, 7/2) n becomes 7/2 = 3
second call: maximumelement(2,0,3/2) n becomes 3/2 = 1
the base condition met , max1 gets a[0] = 1
here hell breaks loose: second recursive call begins index 0 , n = index + n/2 = 0 + 1/2 = 0? when print values program shows 3 value n when second call being made.
i have programmed extensively, having nightmare this. many can break down me!!
that pseudocode above, see below java code wrote (it might make easier if trying run it):
public int maximumelement(int a[], int i, int n) { int max1, max2; system.out.println("1: " + + " 2: " + n); if(n == 1) { system.out.println("returning " + a[i]); return a[i]; } max1 = maximumelement(a, i, n/2); system.out.println("index: "+i+" "+" variable: "+max1+" n value: "+n); max2 = maximumelement(a, + (n/2), n - (n/2)); system.out.println("index2: " + + " " + "variable2: " + max2); if(max1 > max2) { system.out.println("returning.... " + max1 ); return max1; } else { system.out.println("returning.... " + max2); return max2; } }
it sounds understand base case , know how recursion works, key understanding particular example note given initial array
a = [1,2,10,15,16,4,8]
you are, @ "top level" computing 2 things:
maxval1 = maximumelement(array, 0, 3); maxval2 = maximumelement(array, 3, 4);
which says
- make
maxval1
maximum value array in range starting index 0 of size 3 - make
maxval2
maximum value array in range index 3 of size 4
so
maxval1
indeed 10maxval2
indeed 16
and answer 16.
the nice thing recursion don't have worry tracing things extensively. if trust base case , manner in base case, understanding 1 level should suffice.
i think got stuck said "all hell breaks loose" because second recursive call begins starting index of 0. doesn't. starts @ index 3. (that is, assuming second recursive call 1 computing maxval2
).
here bit of abbreviated trace of how computation works out. i've taken liberty rename function m
, assume maxval1
, maxval2
computed little more "functionally".
a = [1,2,10,15,16,4,8] m(a, 0, 7) = m(m(a, 0, 3), m(a, 3, 4)) = m(m(m(a, 0, 1), m(a, 1, 2)), m(a, 3, 4)) = m(m(a[0], m(a, 1, 2)), m(a, 3, 4)) = m(m(1, m(a, 1, 2)), m(a, 3, 4)) = m(m(1, m(m(a, 1, 1), m(a, 2, 1)), m(a, 3, 4)) = m(m(1, m(a[1], a[2])), m(a, 3, 4)) = m(m(1, m(2, 10)), m(a, 3, 4)) = m(m(1, 10), m(a, 3, 4)) = m(10, m(a, 3, 4)) = … = 16
Comments
Post a Comment