Is recursion truly superior to iteration?

  • Thread starter dashkin111
  • Start date
  • Tags
    Recursion
In summary, recursion is often better than iteration for implementing divide-and-conquer algorithms such as quicksort, fast Fourier transform, and fast multipole method. However, it may not be the best choice for simpler functions such as the factorial function. Recursion can also be useful for certain programming tasks, such as evaluating expressions with parentheses and traversing directory trees. It allows for creating and returning to different instances of a program state. Overall, recursion can be a powerful tool for handling complex problems in programming.
  • #1
dashkin111
47
0
When is recursion really better than iteration. I'm not a programmer by major, but I can't think of any time when recursion would be better
 
Technology news on Phys.org
  • #2
There are many divide-and-conquer algorithms that are much easier to implement by recursion than by iteration. Examples that come to mind are quicksort, the fast Fourier transform, and the fast multipole method. Unfortunately, the most common example used to illustrate recursion is the factorial function, which is better implemented by iteration.
 
  • #3
Try writing a program to compute the Ackermann function in a non-recursive language. I still remember that exercise twenty some years. Doing this in Fortran and assembly was painful. In a recursive language it takes but a few lines of code. The Ackermann function is

Edit: I give up. Sometimes the limited LaTeX here bites.
A(m,n) = n+1 if m=0,
A(m,n) = A(m-1,1) if n=0,
A(m,n) = A(m-1,A(m,n-1)) otherwise.
 
Last edited:
  • #4
There are cases where the nature of the programming is recursive. One example is an expression evaluator that allows parenthesis (like a calculator). It's easier to use just make a recursive call for each "(" encountered, and to return with a value for each ")" encountered in such a case, but otherwise just iterate for all the mathematical operators like + - x / (assuming no precedence here).

Another example is a program that follows a directory tree, especially if the program processes all files within a directory before processing sub-directories. I think of this as a program that makes uses recursion to create a "new instance" of a program state, with the ability to return to a "previous instance".

I use the following logic as the basis of a file copy program to backup / restore / defrag partitions on my system. The issue in this sample code is that there are two loops and the second loop needs to be able to "call" a new instance of both loops, then "return" back to a previous instance of the second loop.

Code:
ProcessDirectory(...)
{

    ForEachFile()
    {
        ProcessFile();
    }

    ForEachDirectotry()
    {
        ProcessDirectory()
    }
}

ProcessFile(...)
{

}
 
Last edited:
  • #5
Excellent example, Jeff. Tree traversal is a bear in a non-recursive language. You essentially have to reinvent all of the machinery you get for free with recursion to do tree traversal iteratively.
 
  • #6
Also for enumerating all subsets and permutations of an input set of n elements for example.
 
  • #7
Thanks everyone, this is helpful to me.
 

What is the difference between recursion and iteration?

Recursion is a programming technique in which a function calls itself repeatedly until a certain condition is met. Iteration, on the other hand, is a process of repeating a set of instructions until a specific condition is satisfied. In other words, recursion is a way of solving a problem by breaking it down into smaller subproblems, while iteration is a way of solving a problem through repetition.

Which one is more efficient: recursion or iteration?

In general, iteration is more efficient than recursion because recursion requires the function to be called multiple times, which can lead to a larger overhead. However, in some cases, recursion can be more efficient, especially when the problem can be easily broken down into smaller subproblems.

What are the advantages of using recursion?

Recursion offers a more concise and elegant solution to certain problems, especially those that can be easily broken down into smaller subproblems. It can also be easier to understand and debug compared to iterative solutions.

What are the disadvantages of recursion?

One of the main disadvantages of recursion is that it can lead to a larger overhead compared to iteration. This can result in slower execution time and may even lead to stack overflow if the recursive function is called too many times. Recursion can also be more difficult to implement for certain problems, and it may not be the best approach for all situations.

When should I use recursion and when should I use iteration?

It ultimately depends on the specific problem at hand. In general, if the problem can be easily broken down into smaller subproblems, recursion may be a good choice. However, if efficiency is a concern, iteration may be a better option. It's important to weigh the pros and cons of each approach and choose the one that best suits the problem at hand.

Similar threads

  • Programming and Computer Science
2
Replies
62
Views
10K
  • Programming and Computer Science
Replies
1
Views
271
  • Engineering and Comp Sci Homework Help
Replies
11
Views
896
  • Programming and Computer Science
Replies
11
Views
811
  • Programming and Computer Science
Replies
16
Views
1K
  • Programming and Computer Science
Replies
7
Views
2K
  • Programming and Computer Science
Replies
17
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
940
  • Programming and Computer Science
Replies
1
Views
678
Back
Top