11. Recursion_outside

This sometimes requires we define additional

Info iconThis preview shows pages 6–12. Sign up to view the full content.

View Full Document Right Arrow Icon
This sometimes requires we define additional  paramaters that are passed to the method. For example, we defined the array reversal  method as ReverseArray( A, i,  j ), not  ReverseArray( A ). © 2010 Goodrich, Tamassia
Background image of page 6

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Using Recursion 7 Computing Powers The power function, p(x,n)=x n , can be defined  recursively: This leads to an power function that runs in  O(n) time (for we make n recursive calls). We can do better than this, however. - = = else ) 1 , ( 0 if 1 ) , ( n x p x n n x p © 2010 Goodrich, Tamassia
Background image of page 7
Using Recursion 8 Recursive Squaring We can derive a more efficient linearly  recursive algorithm by using repeated squaring: For example, 2 4 = 2 ( 4 / 2 ) 2 = ( 2 4 / 2 ) 2 = ( 2 2 ) 2 = 4 2 = 16 2 5 = 2 1 +( 4 / 2 ) 2 = 2 ( 2 4 / 2 ) 2 = 2 ( 2 2 ) 2 = 2 ( 4 2 ) = 32 2 6 = 2 ( 6 / 2)2 = ( 2 6 / 2 ) 2 = ( 2 3 ) 2 = 8 2 = 64 2 7 = 2 1 +( 6 / 2 ) 2 = 2 ( 2 6 / 2 ) 2 = 2 ( 2 3 ) 2 = 2 ( 8 2 ) = 128 . = - = even is 0 if odd is 0 if 0 if ) 2 / , ( ) 2 / ) 1 ( , ( 1 ) , ( 2 2 x x x n x p n x p x n x p © 2010 Goodrich, Tamassia
Background image of page 8

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Using Recursion 9 Recursive Squaring Method Algorithm  Power ( x, n ):       Input:  A number  and integer  n =  0       Output:  The value  x n      if  = 0 then return  1      if  is odd  then y  =  Power ( x,  ( n -  1) 2) return  ∙  y      else y =  Power ( x, n/  2) return   y © 2010 Goodrich, Tamassia
Background image of page 9
Using Recursion 10 Analysis Algorithm  Power ( x, n ):       Input:  A number  and  integer  n =  0       Output:  The value  x n      if  = 0 then return  1      if  is odd  then y  =  Power ( x,  ( n -  1) 2) return  x ∙ y ∙ y      else y =  Power ( x, n/  2) return  y ∙ y It is important that we use a  variable twice here rather than  calling the method twice. Each time we make a recursive  call we halve the value of n;  hence, we make log n recursive  calls. That is, this method runs  in O(log n) time. © 2010 Goodrich, Tamassia
Background image of page 10

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Using Recursion 11 Tail Recursion Tail recursion occurs when a linearly recursive  method makes its recursive call as its last step. The array reversal method is an example.
Background image of page 11
Image of page 12
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

Page6 / 21

This sometimes requires we define additional paramaters...

This preview shows document pages 6 - 12. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online