Questions
When should you consider using recursive algorithms when writing a program? Discuss in terms of advantages and disadvantages.
1. Definition of recursion
“A recursive method is a method that calls itself” (Seiter & Palmer, 2019). A recursive method usually consists of two parts: one is the base case, which halts the recursion; the other is the recursive call, that is, calling the method itself inside the method. “Every recursive method must have at least one base case” (Seiter & Palmer, 2019). If a recursive method has no base case, then the recursion is called infinite recursion. This method will call itself repeatedly until the program eventually crashes.
Codes:
1 | /** |
Outputs:
1 | hello |
For example, in the above program, the printHello() method calls itself internally, so printHello() is a recursive method. However, since this recursive method lacks a base case, it is called infinite recursion. Due to the lack of a base case to stop the recursion, the printHello() will crash with a StackOverflowError.
2. Scenarios suitable for using recursive algorithms
“Recursion is most useful when it is used to solve problems where the structure of the problem repeats” (Seiter & Palmer, 2019). For example, we can use recursion to calculate Fibonacci numbers, which is defined by the recurrence relation: f0 =0; f1 =1; fn = fn-1 + f n-2, for n>1.
According to Seiter & Palmer, “any recursive solution could be written with iteration (loops) instead” (2019). So, in what scenarios is recursion used, and what problems are suitable for iteration? Generally speaking, “iteration is usually preferred and more efficient, but recursive solutions can be elegant and require less code” (Downey & Mayfield, 2019, p. 127).
To further illustrate when recursive algorithms should be considered when writing programs, I will give an example that uses recursion and iteration to calculate Fibonacci numbers. Then, I will illustrate the recursion‘s pros and cons by comparing the two algorithms’ code size and time complexity.
Codes:
1 | /** |
Outputs:
1 | recursiveFibonacci() runtimes: 424100 ns; result: 6765 |
In the above example, the pros and cons of using recursion are clear. On the one hand, we can see that the recursive method recursiveFibonacci() is more concise than the iterative method iterativeFibonacci(). In other words, recursiveFibonacci() has less code than iterative Fibonacci(). This is the advantage of recursion. On the other hand, when calculating the Fibonacci number f(20), recursion takes 424100 ns, and iteration takes 2500 ns. Therefore, recursion requires more time than iteration in this case, which is “because the call stack is so heavily used during recursion” (Stack Overflow, n.d.). This is the disadvantage of recursion.
3. In Conclusion
In general, recursion is more elegant and requires less code than iteration, but in some cases, it may take longer to run because recursion requires a lot of call stacks. Hence, if we seek code simplicity, recursion may be a better choice than iteration; if we pursue a faster running time, iteration may be better than recursion.
Word count: 853
References
1.Seiter, L., & Palmer, D. (2020). Introduction to Programming with Java.
https://runestone.academy/runestone/books/published/csjava/index.html
2.Eck, D. J. (2019). Introduction to Programming Using Java.
http://math.hws.edu/javanotes
3.Downey, A. B., & Mayfield, C. (2019). Think Java (2nd ed.). Green Tea Press.
4.Are recursive methods always better than iterative methods in Java? (n.d.). Stack Overflow.
https://stackoverflow.com/questions/15346774/are-recursive-methods-always-better-than-iterative-methods-in-java