递归和迭代之间的区别 [英] Difference between recursion and iteration

查看:16
本文介绍了递归和迭代之间的区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么区别?这些是一样的吗?如果没有,可以举个例子吗?

分子量:迭代 - 1 : 迭代或重复的动作或过程或者直到满足一个条件

递归 - 3 : 一种计算机编程技术,涉及使用一个或多次调用自身直到满足指定条件的过程、子例程、函数或算法,此时每个重复的其余部分从最后一个开始处理一个叫第一个

解决方案

我们可以从 类似于迭代:

正如 Pi 表示法.类似于如何将一些(数学)递归公式重写为迭代公式,一些(但不是全部)递归过程具有迭代等价物.通过在您自己的数据结构中跟踪部分结果,而不是使用函数调用堆栈,所有递归过程都可以转换为迭代过程.

What is the difference? Are these the same? If not, can someone please give me an example?

MW: Iteration - 1 : the action or a process of iterating or repeating: as a : a procedure in which repetition of a sequence of operations yields results successively closer to a desired result b : the repetition of a sequence of computer instructions a specified number of times or until a condition is met

Recursion - 3 : a computer programming technique involving the use of a procedure, subroutine, function, or algorithm that calls itself one or more times until a specified condition is met at which time the rest of each repetition is processed from the last one called to the first

解决方案

We can distinguish (as is done in SICP) recursive and iterative procedures from recursive and iterative processes. The former are as your definition describes, where recursion is basically the same as mathematical recursion: a recursive procedure is defined in terms of itself. An iterative procedure repeats a block of code with a loop statement. A recursive process, however, is one that takes non-constant (e.g. O(n) or O(lg(n)) space) to execute, while an iterative process takes O(1) (constant) space.

For mathematical examples, the Fibonacci numbers are defined recursively:

Sigma notation is analogous to iteration:

as is Pi notation. Similar to how some (mathematical) recursive formulae can be rewritten as iterative ones, some (but not all) recursive processes have iterative equivalents. All recursive procedures can be transformed into iterative ones by keeping track of partial results in your own data structure, rather than using the function call stack.

这篇关于递归和迭代之间的区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆