Page 397 - Cs_withBlue_J_C11_Flipbook
P. 397

13.2.3 Advantages of Recursion
                 Some of the advantages of recursion are as follows:

                 •  Recursion is used extensively in backtracking algorithms.   2  1  5  6     4    7     3     9    8
                   The backtracking algorithm is a problem-solving      3    6     8     9     5    2     1     7    4
                   algorithm that involves building a set of all the solutions
                   incrementally. And the solutions that don’t satisfy the   7  9  4     3     8    1     6     5    2
                   conditions are removed during the course of program
                   execution. It uses a brute force approach while trying   5  8   6     2     7    4     9     3    1
                   to  find  a  solution  to  the  problem.  Example:  solving
                   Sudoku, solving crosswords, etc.                     1    4     2     5     9    3     8     6    7
                 •  Recursion adds clarity and (sometimes) minimises the   9  7    3     8     1    6     4     2    5
                   time taken to write and debug a code.

                 •  Certain algorithms like Tree Traversal, and Tower of Hanoi   8  2  1  7    3    9     5     4    6
                   can be solved easily using recursion as compared to
                   iteration.                                           6    5     9     4     2    8     7     1    3

                 •  Recursion decreases time complexity.                4    3     7     1     6    5     2     8    9

                 13.2.4 Disadvantages of Recursion                                          Sudoku
                 Some of the disadvantages of recursion are as follows:

                 •  It consumes a lot of memory. As the method calls itself repeatedly, every time the variable is allocated with a new
                   memory on the stack.
                 •  Because of its memory stack manipulation, recursion is a slow process.
                 •  For a beginner, it is difficult to understand the concept of recursion.

                 13.2.5 Differences between Recursion and Iteration
                 From the coding process, we can see that any iterative structure can be represented by recursive method. Let us see
                 the differences between recursion and iteration:

                                         Recursion                                      Iteration

                       Recursion  is a programming technique  in  which  a  In iteration,  loops are used to execute the programs
                       function calls itself.                         repetitively until the condition becomes false.

                       Fresh memory space is allocated for each recursive call. It uses  the same memory locations  for variables  and
                                                                      repeats the same unit of code.

                       It is a slow process.                          It is faster than recursion.

                       It has a smaller size of code.                 It has a larger size of code.



                     13.3 SIMPLE RECURSIVE METHODS
                 Designing a recursive method involves:
                 •  Identifying the exit case. There may be multiple exit cases also depending on the requirement of the problem.
                 •  Determining the change in the actual parameter of the recursive case and the operation to be performed on the
                   other variables.






                                                                                                                       395
                                                                                                           Recursion   395
   392   393   394   395   396   397   398   399   400   401   402