递归的优点和缺点是什么?

发布于 2024-10-20 23:56:18 字数 49 浏览 2 评论 0原文

关于在排序算法中使用递归而不是非递归方法,或者就此而言,任何算法的优点和缺点是什么?

With respect to using recursion over non-recursive methods in sorting algorithms or, for that matter, any algorithm what are its pros and cons?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(11

旧竹 2024-10-27 23:56:18

大多数情况下,递归速度较慢,并且占用更多堆栈。递归的主要优点是,对于像树遍历这样的问题,它使算法变得更容易或更“优雅”。
查看一些比较:

链接

For the most part recursion is slower, and takes up more of the stack as well. The main advantage of recursion is that for problems like tree traversal it make the algorithm a little easier or more "elegant".
Check out some of the comparisons:

link

盛夏尉蓝 2024-10-27 23:56:18

递归意味着函数重复调用

它使用系统堆栈来完成其任务。由于堆栈使用 LIFO 方法
当调用函数时,受控对象被移动到定义函数的位置,该函数存储在具有某个地址的内存中,该地址存储在堆栈中

其次,它降低了程序的时间复杂度。

虽然有点题外话,但有点相关。必须阅读。 : 递归与迭代

Recursion means a function calls repeatedly

It uses system stack to accomplish its task. As stack uses LIFO approach
and when a function is called the controlled is moved to where function is defined which has it is stored in memory with some address, this address is stored in stack

Secondly, it reduces a time complexity of a program.

Though bit off-topic,a bit related. Must read. : Recursion vs Iteration

迟月 2024-10-27 23:56:18

所有算法都可以递归定义。这使得它变得非常非常容易可视化和证明。

某些算法(例如,阿克曼函数)无法(轻松)迭代指定。

如果无法执行尾调用优化,则递归实现将使用比循环更多的内存。虽然迭代可能比无法优化的递归函数使用更少的内存,但它的表达能力有一些限制。

All algorithms can be defined recursively. That makes it much, much easier to visualize and prove.

Some algorithms (e.g., the Ackermann Function) cannot (easily) be specified iteratively.

A recursive implementation will use more memory than a loop if tail call optimization can't be performed. While iteration may use less memory than a recursive function that can't be optimized, it has some limitations in its expressive power.

迟到的我 2024-10-27 23:56:18

任何使用递归实现的算法也可以使用迭代来实现。

为什么使用递归

  1. 由于维护堆栈的开销,递归通常会比较慢。
  2. 它通常会为堆栈使用更多内存。

为什么使用递归

  1. 递归增加了清晰度并且(有时)减少了编写和调试代码所需的时间(但不一定减少空间要求或执行速度)。
  2. 降低时间复杂度。
  3. 在解决基于树结构的问题时表现更好。

例如,与迭代相比,使用递归更容易解决汉诺塔问题。

Any algorithm implemented using recursion can also be implemented using iteration.

Why not to use recursion

  1. It is usually slower due to the overhead of maintaining the stack.
  2. It usually uses more memory for the stack.

Why to use recursion

  1. Recursion adds clarity and (sometimes) reduces the time needed to write and debug code (but doesn't necessarily reduce space requirements or speed of execution).
  2. Reduces time complexity.
  3. Performs better in solving problems based on tree structures.

For example, the Tower of Hanoi problem is more easily solved using recursion as opposed to iteration.

海螺姑娘 2024-10-27 23:56:18

我个人更喜欢使用迭代而不是递归函数。特别是如果您的函数具有复杂/繁重的逻辑并且迭代次数很大。这是因为随着每次递归调用,调用堆栈都会增加。如果您的操作太大并且还会减慢进程,则可能会导致堆栈崩溃。

I personally prefer using Iterative over recursive function. Especially if you function has complex/heavy logic and number of iterations are large. This because with every recursive call call stack increases. It could potentially crash the stack if you operations are too large and also slow up process.

妥活 2024-10-27 23:56:18

首先:

优点:

  • 这是实现可变数量嵌套循环的独特方法(也是实现大量恒定数量嵌套循环的唯一优雅方法)。

缺点:

  • 处理大集合时,递归方法通常会抛出 StackOverflowException。但递归循环不存在这个问题。

To start:

Pros:

  • It is the unique way of implementing a variable number of nested loops (and the only elegant way of implementing a big constant number of nested loops).

Cons:

  • Recursive methods will often throw a StackOverflowException when processing big sets. Recursive loops don't have this problem though.
晨与橙与城 2024-10-27 23:56:18

开始:

递归:
调用自身的函数称为递归函数,这种技术称为递归。

优点:

1. Reduce unnecessary calling of functions.
2. Through Recursion one can solve problems in easy way while its iterative solution is very big and complex.
3. Extremely useful when applying the same solution.

缺点:

1. Recursive solution is always logical and it is very difficult to trace.
2. In recursive we must have an if statement somewhere to force the function to return without the recursive call being executed, otherwise the function will never return.
3. Recursion uses more processor time.

To start :

Recursion:
A function that calls itself is called as recursive function and this technique is called as recursion.

Pros:

1. Reduce unnecessary calling of functions.
2. Through Recursion one can solve problems in easy way while its iterative solution is very big and complex.
3. Extremely useful when applying the same solution.

Cons:

1. Recursive solution is always logical and it is very difficult to trace.
2. In recursive we must have an if statement somewhere to force the function to return without the recursive call being executed, otherwise the function will never return.
3. Recursion uses more processor time.
计㈡愣 2024-10-27 23:56:18

表达力

大多数问题自然可以通过递归来表达,例如斐波那契、归并排序和快速排序。从这方面来说,代码是为人类而不是机器编写的。

不变性

迭代解决方案通常依赖于变化的临时变量,这使得代码难以阅读。这可以通过递归来避免。

性能

递归对堆栈不友好。当递归设计不佳或不支持尾部优化时,堆栈可能会溢出。

Expressiveness

Most problems are naturally expressed by recursion such as Fibonacci, Merge sorting and quick sorting. In this respect, the code is written for humans, not machines.

Immutability

Iterative solutions often rely on varying temporary variables which makes the code hard to read. This can be avoided with recursion.

Performance

Recursion is not stack friendly. Stack can overflow when the recursion is not well designed or tail optimization is not supported.

没有心的人 2024-10-27 23:56:18

在某些情况下,您必须在递归似乎对您有利的问题中放弃递归,这是因为对于递归必须发生数千次的问题,这将导致堆栈溢出错误,即使您的代码确实如此不要陷入无限递归。大多数编程语言都会限制堆栈调用的数量,因此如果您的递归超出此限制,那么您可能会考虑不使用递归。

Some situation would arise where you would have to abandon recursion in a problem where recursion appears to be to your advantage, this is because for problems where your recursion would have to occur thousand of times this would result in a stackoverflow error even though your code did not get stuck in an infinite recursion. Most programming languages limits you to a number of stack calls, so if your recursion goes beyond this limit, then you might consider not using recursion.

孤城病女 2024-10-27 23:56:18

我们应该在以下场景中使用递归:

  • 我们的函数退出条件基于动态编程(记忆)
  • 当我们不知道有限的迭代次数时,例如,当我们需要对元素的逆序执行操作时, 。这意味着我们要先处理最后一个元素,然后处理 n-1、n-2 等,直到第一个元素

。递归将节省多次遍历。如果我们可以像这样划分堆栈分配,那么它将很有用:

int N = 10;
int output = process(N) + process(N/2);
public void process(int n) {
    if (n==N/2 + 1 || n==1) {
       return 1;
    }

    return process(n-1) + process(n-2);
}

在这种情况下,在任何给定时间都只会分配一半堆栈。

We should use recursion in following scenarios:

  • when we don't know the finite number of iteration for example our fuction exit condition is based on dynamic programming (memoization)
  • when we need to perform operations on reverse order of the elements. Meaning we want to process last element first and then n-1, n-2 and so on till first element

Recursion will save multiple traversals. And it will be useful, if we can divide the stack allocation like:

int N = 10;
int output = process(N) + process(N/2);
public void process(int n) {
    if (n==N/2 + 1 || n==1) {
       return 1;
    }

    return process(n-1) + process(n-2);
}

In this case only half stacks will be allocated at any given time.

一百个冬季 2024-10-27 23:56:18

递归名声不好,我总是惊讶于有多少开发人员甚至不会接触递归,因为有人告诉他们这是邪恶的化身。

我通过反复试验了解到,如果做得正确,递归可以是迭代某些内容的最快方法之一,但这不是一个坚定的规则,并且每种语言/编译器/ 发动机有其自身的怪癖,因此里程会有所不同。

在 javascript 中,我可以通过引入递归来可靠地加速几乎任何迭代过程,并具有减少副作用并使代码更加清晰简洁和可重用的额外好处。另外,专业提示可以解决堆栈溢出问题(不,您不能禁用警告)。

我个人的优点和优点缺点:

优点:

- Reduces side effects.
- Makes code more concise and easier to reason about.
- Reduces system resource usage and performs better than the traditional for loop.

缺点:

- Can lead to stack overflow.
- More complicated to setup than a traditional for loop.

里程数会因语言/编译器/引擎而异。

Recursion gets a bad rep, I'm always surprised by the number of developers that wont even touch recursion because someone told them it was evil incarnate.

I've learned through trial and error that when done properly recursion can be one of the fastest ways to iterate over something, it is not a steadfast rule and each language/ compiler/ engine has it's own quirks so mileage will vary.

In javascript I can reliably speed up almost any iterative process by introducing recursion with the added benefit of reducing side effects and making the code more clear concise and reusable. Also pro tip its possible to get around the stack overflow issue (and no you dont disable the warning).

My personal Pros & Cons:

Pros:

- Reduces side effects.
- Makes code more concise and easier to reason about.
- Reduces system resource usage and performs better than the traditional for loop.

Cons:

- Can lead to stack overflow.
- More complicated to setup than a traditional for loop.

Mileage will vary depending on language/ complier/ engine.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文