Answer to Question #161420 in Java | JSP | JSF for GEORGETTE IZAC

Question #161420

When should you consider using recursive algorithms when writing a program?

Discuss in terms of advantages and disadvantages


1
Expert's answer
2021-02-05T08:06:35-0500

Why Use Recursion?

So the main reason we use recursion is to simplify (not optimize) an algorithm into terms easily understood by most people. A classic example is a binary search.

The reason why the binary search is suitable for recursion is a base case. The base case is the most simple problem that can be solved without using recursion. There is one more base case in the recursive binary search – and that’s the case that the list is empty. If the list is empty, we don’t need to do anything, just stop the algorithm.

Why Not Use Recursion?

The reason recursion can be bad is that it can be incredibly inefficient. The example of binary search is an example of the most efficient case for a recursive method. That’s because when the base case is met, and a value is returned to the previous method in the recursion stack, no new recursive calls are made. In other words, each method in the recursion stack is only called once.

So the simple answer is that if you have a large problem that makes more intuitive sense to think about recursively rather than iteratively then it’s a good idea to use recursion, so long as the cost in performance doesn’t hinder your application.




Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog