递归的优点和缺点是什么?
关于在排序算法中使用递归而不是非递归方法,或者就此而言,任何算法的优点和缺点是什么?
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
大多数情况下,递归速度较慢,并且占用更多堆栈。递归的主要优点是,对于像树遍历这样的问题,它使算法变得更容易或更“优雅”。
查看一些比较:
链接
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
递归意味着函数重复调用
它使用系统堆栈来完成其任务。由于堆栈使用 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
所有算法都可以递归定义。这使得它变得非常非常容易可视化和证明。
某些算法(例如,阿克曼函数)无法(轻松)迭代指定。
如果无法执行尾调用优化,则递归实现将使用比循环更多的内存。虽然迭代可能比无法优化的递归函数使用更少的内存,但它的表达能力有一些限制。
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.
任何使用递归实现的算法也可以使用迭代来实现。
为什么不使用递归
为什么使用递归
例如,与迭代相比,使用递归更容易解决汉诺塔问题。
Any algorithm implemented using recursion can also be implemented using iteration.
Why not to use recursion
Why to use recursion
For example, the Tower of Hanoi problem is more easily solved using recursion as opposed to iteration.
我个人更喜欢使用迭代而不是递归函数。特别是如果您的函数具有复杂/繁重的逻辑并且迭代次数很大。这是因为随着每次递归调用,调用堆栈都会增加。如果您的操作太大并且还会减慢进程,则可能会导致堆栈崩溃。
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.
首先:
优点:
缺点:
To start:
Pros:
Cons:
开始:
递归:
调用自身的函数称为递归函数,这种技术称为递归。
优点:
缺点:
To start :
Recursion:
A function that calls itself is called as recursive function and this technique is called as recursion.
Pros:
Cons:
表达力
大多数问题自然可以通过递归来表达,例如斐波那契、归并排序和快速排序。从这方面来说,代码是为人类而不是机器编写的。
不变性
迭代解决方案通常依赖于变化的临时变量,这使得代码难以阅读。这可以通过递归来避免。
性能
递归对堆栈不友好。当递归设计不佳或不支持尾部优化时,堆栈可能会溢出。
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.
在某些情况下,您必须在递归似乎对您有利的问题中放弃递归,这是因为对于递归必须发生数千次的问题,这将导致堆栈溢出错误,即使您的代码确实如此不要陷入无限递归。大多数编程语言都会限制堆栈调用的数量,因此如果您的递归超出此限制,那么您可能会考虑不使用递归。
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.
我们应该在以下场景中使用递归:
。递归将节省多次遍历。如果我们可以像这样划分堆栈分配,那么它将很有用:
在这种情况下,在任何给定时间都只会分配一半堆栈。
We should use recursion in following scenarios:
Recursion will save multiple traversals. And it will be useful, if we can divide the stack allocation like:
In this case only half stacks will be allocated at any given time.
递归名声不好,我总是惊讶于有多少开发人员甚至不会接触递归,因为有人告诉他们这是邪恶的化身。
我通过反复试验了解到,如果做得正确,递归可以是迭代某些内容的最快方法之一,但这不是一个坚定的规则,并且每种语言/编译器/ 发动机有其自身的怪癖,因此里程会有所不同。
在 javascript 中,我可以通过引入递归来可靠地加速几乎任何迭代过程,并具有减少副作用并使代码更加清晰简洁和可重用的额外好处。另外,专业提示可以解决堆栈溢出问题(不,您不能禁用警告)。
我个人的优点和优点缺点:
优点:
缺点:
里程数会因语言/编译器/引擎而异。
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:
Cons:
Mileage will vary depending on language/ complier/ engine.