比较类似术语之间的差异

Difference Between

Home / 技术 / IT / Programming /递归和迭代之间的差异

递归和迭代之间的差异

2018年1月2日发表Lithmee

关键区别 - 递归与迭代

递归和迭代可用于解决编程问题。使用递归或迭代解决问题的方法取决于解决问题的方法。这key difference在递归和迭代之间是recursion is a mechanism to调用功能within the same function while iteration is to execute a set of instructions repeatedly until the given condition is true.递归和迭代是开发的主要技术算法and building software applications.

内容

1.Overview and Key Difference
2.什么是递归
3.什么是迭代
4.Similarities Between Recursion and Iteration
5.并排比较 - 递归与表格形式的迭代
6.概括

什么是递归?

When a function calls itself within the function, it is known as Recursion. There are two types of recursion. They are finite recursion and infinite recursion.Finite recursion有终止条件。Infinite recursiondoes not have a terminating condition.

可以使用程序来计算阶乘,可以解释递归。

恩!= n * (n-1)!, if n>0

恩!= 1,如果n = 0;

请参阅Bellow代码以计算3(3!= 3*2*1)的阶乘。

intmain () {

int value =factorial (3);

printf(“Factorial is %d\n”, value);

return 0;

}

intfactorial(intn){

如果(n == 0){

返回1;

}

别的 {

返回n*阶乘(n-1);

}

}

在调用阶乘(3)时,该功能将调用阶乘(2)。在调用阶乘(2)时,该功能将调用阶乘(1)。然后,阶乘(1)将调用阶乘(0)。阶乘(0)将返回1.在上述程序中,n == 0条件“如果块”是基本条件。同样,阶乘功能一次又一次地调用。

Recursive functions are related with the堆栈。在C中,主要程序可以具有许多功能。因此,main()是调用函数,主程序调用的函数是调用函数。调用函数时,将控件给予调用函数。函数执行完成后,将返回控件到MAIM。然后主要程序继续。因此,它创建激活记录或堆栈框架以继续执行。

递归和迭代之间的差异

Figure 01: Recursion

In the above program, when calling factorial (3) from main, it creates an activation record in the call stack. Then, factorial (2) stack frame is created on top of the stack and so on. The activation record keeps information aboutlocal variablesetc. Each time the function is called, a new set of local variables are created on the top of the stack. These stack frames can slow down the speed up.Likewise in recursion, a function calls itself. Time complexity for a recursive function is found by the number of times, the function is called. Time complexity for one function call is O(1). For n number of recursive calls, the time complexity is O(n).

什么是迭代?

Iteration is a block of instructions which repeats again and again till the given condition is true. Iteration can be achieved using “forloop”,“时循环”或“循环”。“用于循环”语法如下。

for (initialization; condition; modify) {

// statements;

}

递归与迭代之间的关键区别

Figure 02: “for loop flow diagram”

初始化步骤执行。这step is to declare and initialize loop control variables. If the condition is true, the statements inside the curly braces execute. Those statements execute till the condition is true. If the condition is false, the control goes to the next statement after the “for loop”. After executing the statements inside the loop, the control goes to modify section. It is to update the loop control variable. Then the condition is checked again. If the condition is true, the statements inside the curly braces will execute. This way the “for loop” iterates.

在“ while循环”中,循环内部的语句执行直到条件为真。

而(条件){

//statements

}

在“ do-while”循环中, the condition is checked at the end of the loop. So, the loop executes at least once.

做{

//statements

} while(condition)

Program to find the factorial of 3 (3!) using iteration (“for loop”) is as follows.

int main(){

intn=3, factorial=1;

inti;

for(i=1; i<=n ; i++){

factorial = factorial * i;

}

printf(“Factorial is %d\n”, factorial);

return 0;

}

What are the Similarities Between Recursion and Iteration?

  • Both are techniques to solve a problem.
  • 这task can be solved either in recursion or iteration.

What is the Difference Between Recursion and Iteration?

递归与迭代

递归是在同一函数中调用功能的一种方法。 Iteration is a block of instructions which repeats until the given condition is true.
空间复杂性
递归程序的空间复杂性高于迭代。 Space complexity is lower in iterations.
Speed
递归execution is slow. 通常,迭代比递归快。
Condition
If there is no termination condition, there can be an infinite recursion. 如果条件永远不会变成错误,它将是无限的迭代。
Stack
In recursion, the stack is used to store local variables when the function is called. In an iteration, the stack is not used.
代码可读性
递归程序更可读。 这iterative program is harder to read than a recursive program.

概括– Recursion与迭代

本文讨论了递归与迭代之间的差异。两者都可以用来解决编程问题。递归和迭代之间的区别在于,递归是一种在相同函数中调用函数的机制,并迭代它可以重复执行一组指令,直到给定条件为真。如果可以以递归形式解决问题,则也可以使用迭代来解决。

Download the PDF Version of Recursion vs Iteration

您可以下载本文的PDF版本,并根据引文注释将其用于离线目的。请在此处下载PDF版本递归和迭代之间的差异

参考:

1.Point, Tutorials. “Data Structures and Algorithms Recursion Basics.”,Tutorials Point, 15 Aug. 2017.在这里可用
2.纳什特技术。“ C函数中的递归|C语言教程” YouTube,YouTube,2016年9月12日。在这里可用
3.yusuf shakeel. “Recursion Algorithm | Factorial – step by step guide”YouTube, YouTube, 14 Oct. 2013.在这里可用

Image Courtesy:

1.’CPT-Recursion-Factorial-Code’By Pluke – Own work, (Public Domain) viaCommons Wikimedia
2.’For-loop-diagram’By No machine readable author provided – Own work assumed.(CC BY-SA 2.5)viaCommons Wikimedia

Related posts:

Microsoft .NET Framework 3.5和.NET Framework 4.0之间的区别 Difference Between Performance and Load Testing 二进制搜索和线性搜索之间的区别 Difference Between Static and Non Static Method Difference Between Struts and Struts2

提交以下:Programming标记为:比较递归和迭代,Finite recursion,Infinite recursion,Iteration,Iteration Condition,迭代定义,迭代空间复杂性,Iteration Speed,Iteration Stack,递归,递归and Iteration Differences,递归and Iteration Similarities,递归Condition,递归定义,递归空间复杂性,递归Speed,递归堆栈,递归与迭代

关于作者:Lithmee

LithmeeMandula is a BEng (Hons) graduate in Computer Systems Engineering. She is currently pursuing a Master’s Degree in Computer Science. Her areas of interests in writing and research include programming, data science, and computer systems.

发表评论取消回复

您的电子邮件地址不会被公开。Required fields are marked*

Request Article

Featured Posts

冠状病毒和冷症状之间的差异

冠状病毒和冷症状之间的差异

冠状病毒和SARS之间的差异

冠状病毒和SARS之间的差异

冠状病毒和流感的差异

冠状病毒和流感的差异

冠状病毒和covid 19之间的差异

冠状病毒和covid 19之间的差异

You May Like

Difference Between Nephrologist and Urologist

加湿器和除湿器之间的差异

濒危和灭绝之间的区别

濒危和灭绝之间的区别

Difference Between Flowering and Nonflowering Plants

三星JS9000 4K SUHD LED和LG EG9600 4K OLED电视的区别

三星JS9000 4K SUHD LED和LG EG9600 4K OLED电视的区别

Latest Posts

  • What is the Difference Between Aquagenic Urticaria and Aquagenic Pruritus
  • 收敛剂和碳粉有什么区别
  • What is the Difference Between Esophagitis and Barrett’s Esophagus
  • 酒精墨水和树脂染料有什么区别
  • 甲状旁腺功能亢进和甲状腺功能亢进之间有什么区别
  • What is the Difference Between Pearlescent and Iridescent
  • Home
  • Vacancies
  • About
  • Request Article
  • 联系我们

Copyright © 2010-2018Difference Between。All rights reserved.Terms of Useand Privacy Policy:Legal。