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

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

问题描述

有什么区别?这些是一样的吗?如果没有,谁能给我举个例子?

兆瓦:迭代 - 1 :迭代或重复的动作或过程:作为 a :重复一系列操作产生的结果逐渐接近所需结果的过程 b :将计算机指令序列重复指定次数或直到满足条件

递归 - 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天全站免登陆