11. Recursion_outside

The array reversal method is an example such methods

This preview shows pages 11–16. Sign up to view the full content.

The array reversal method is an example. Such methods can be easily converted to non- recursive methods (which saves on some resources). Example: Algorithm  IterativeReverseArray( A, i, j  ):       Input:  An array  and nonnegative integer indices  and  j       Output:  The reversal of the elements in  starting at index  and ending at  j      while  i <  j  do Swap  A [ ] and  A ] i  = i  + 1 j  = j -  1      return © 2010 Goodrich, Tamassia

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

View Full Document
Using Recursion 12 Binary Recursion Binary recursion occurs whenever there are  two   recursive calls for each non-base case. Example: the DrawTicks method for drawing ticks  on an English ruler. © 2010 Goodrich, Tamassia
Using Recursion 13 A Binary Recursive Method for  Drawing Ticks // draw a tick with no label public static void drawOneTick ( int tickLength ) { drawOneTick ( tickLength , - 1 ); } // draw one tick public static void drawOneTick ( int tickLength , int tickLabel ) { for ( int i = 0 ; i < tickLength ; i ++) System . out . print ( "-" ); if ( tickLabel > = 0 ) System . out . print ( " " + tickLabel ); System . out . print ( "\n" ); } public static void drawTick s ( int tickLength ) { // draw ticks of given length if ( tickLength > 0 ) { // stop when length drops to 0 drawTicks ( tickLength- 1 ); // recursively draw left ticks drawOneTick ( tickLength ); // draw center tick drawTicks ( tickLength- 1 ); // recursively draw right ticks } } public static void drawRuler ( int nInches , int majorLength ) { // draw ruler drawOneTick ( majorLength , 0 ); // draw tick 0 and its label for ( int i = 1 ; i < = nInches ; i ++) { drawTicks ( majorLength- 1 ); // draw ticks for this inch drawOneTick ( majorLength , i ); // draw tick i and its label } } Note the two  recursive calls © 2010 Goodrich, Tamassia

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

View Full Document
Using Recursion 14 Another Binary Recusive Method Problem: add all the numbers in an integer array A: Algorithm  BinarySum( A, i, n ):       Input:  An array  and integers  and  n       Output:  The sum of the  integers in  starting at index  i      if  = 1  then return  A [ ]      return  BinarySum( A, i, n/  2) + BinarySum( A, i  n/  2 , n/  2) Example trace: 3, 1 2, 2 0, 4 2, 1 1, 1 0, 1 0, 8 0, 2 7, 1 6, 2 4, 4 6, 1 5, 1 4, 2 4, 1 © 2010 Goodrich, Tamassia
Using Recursion 15 Computing Fibonacci  Numbers Fibonacci numbers are defined recursively: F 0 = 0 F 1 = 1 F i = F i - 1 + F i - 2 for i > 1. Recursive algorithm (first attempt):

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

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

Page11 / 21

The array reversal method is an example Such methods can be...

This preview shows document pages 11 - 16. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online