📜  递归与迭代之间的区别

📅  最后修改于: 2021-05-08 17:19:31             🧑  作者: Mango

当实体调用自身时,该程序称为递归程序。有循环(或重复)时,程序被称为迭代程序。

示例:查找数字阶乘的程序

C++
// C++ program to find factorial of given number
#include
using namespace std;
  
// ----- Recursion -----
// method to find factorial of given number
int factorialUsingRecursion(int n)
{
    if (n == 0)
        return 1;
  
    // recursion call
    return n * factorialUsingRecursion(n - 1);
}
  
// ----- Iteration -----
// Method to find the factorial of a given number
int factorialUsingIteration(int n)
{
    int res = 1, i;
  
    // using iteration
    for (i = 2; i <= n; i++)
        res *= i;
  
    return res;
}
  
// Driver method
int main()
{
    int num = 5;
    cout << "Factorial of " << num << 
            " using Recursion is: " <<
            factorialUsingRecursion(5) << endl;
  
    cout << "Factorial of " << num <<
            " using Iteration is: " << 
            factorialUsingIteration(5);
  
    return 0;
}
  
// This code is contributed by mits


Java
// Java program to find factorial of given number
class GFG {
  
    // ----- Recursion -----
    // method to find factorial of given number
    static int factorialUsingRecursion(int n)
    {
        if (n == 0)
            return 1;
  
        // recursion call
        return n * factorialUsingRecursion(n - 1);
    }
  
    // ----- Iteration -----
    // Method to find the factorial of a given number
    static int factorialUsingIteration(int n)
    {
        int res = 1, i;
  
        // using iteration
        for (i = 2; i <= n; i++)
            res *= i;
  
        return res;
    }
  
    // Driver method
    public static void main(String[] args)
    {
        int num = 5;
        System.out.println("Factorial of " + num
                           + " using Recursion is: "
                           + factorialUsingRecursion(5));
  
        System.out.println("Factorial of " + num
                           + " using Iteration is: "
                           + factorialUsingIteration(5));
    }
}


Python3
# Python3 program to find factorial of given number
  
# ----- Recursion -----
# method to find factorial of given number
def factorialUsingRecursion(n):
    if (n == 0):
        return 1;
  
    # recursion call
    return n * factorialUsingRecursion(n - 1);
  
# ----- Iteration -----
# Method to find the factorial of a given number
def factorialUsingIteration(n):
    res = 1;
  
    # using iteration
    for i in range(2, n + 1):
        res *= i;
  
    return res;
  
# Driver method
num = 5;
print("Factorial of",num,"using Recursion is:",
                    factorialUsingRecursion(5));
  
print("Factorial of",num,"using Iteration is:",
                    factorialUsingIteration(5));
      
# This code is contributed by mits


C#
// C# program to find factorial of 
// given number
using System;
  
class GFG
{
  
    // ----- Recursion -----
    // method to find factorial of 
    // given number
    static int factorialUsingRecursion(int n)
    {
        if (n == 0)
            return 1;
  
        // recursion call
        return n * factorialUsingRecursion(n - 1);
    }
  
    // ----- Iteration -----
    // Method to find the factorial of
    // a given number
    static int factorialUsingIteration(int n)
    {
        int res = 1, i;
  
        // using iteration
        for (i = 2; i <= n; i++)
            res *= i;
  
        return res;
    }
  
    // Driver Code
    public static void Main(String[] args)
    {
        int num = 5;
        Console.WriteLine("Factorial of " + num + 
                          " using Recursion is: " + 
                          factorialUsingRecursion(5));
  
        Console.WriteLine("Factorial of " + num + 
                          " using Iteration is: " + 
                          factorialUsingIteration(5));
    }
}
  
// This code has been contributed by Rajput-Ji


PHP


输出:
Factorial of 5 using Recursion is: 120
Factorial of 5 using Iteration is: 120

下面是详细的示例,以说明两者之间的区别:

  1. 时间复杂度:找到递归的时间复杂度比迭代的难度大。
    • 递归递归的时间复杂度可以通过根据先前的调用找到第n个递归调用的值来找到。因此,根据基本情况找到目标情况,并根据基本情况进行求解,可以使我们对递归方程的时间复杂度有所了解。有关更多详细信息,请参见解决重复。
    • 迭代迭代的时间复杂度可以通过找到循环内重复的周期数来找到。
  2. 用法:这两种技术的使用都是时间复杂度和代码大小之间的折衷。如果时间复杂度是重点,并且递归调用的数量很大,则最好使用迭代。但是,如果时间复杂度不是问题,而代码又很短,那么递归将是解决之道。
    • 递归:递归涉及再次调用相同的函数,因此,其代码长度非常短。但是,正如我们在分析中所看到的,当存在大量的递归调用时,递归的时间复杂度可能会成指数增长。因此,递归的使用在较短的代码中是有利的,但是在时间复杂度上较高。
    • 迭代:迭代是代码块的重复。这涉及到较大的代码量,但是时间复杂度通常比递归要小。
  3. 开销:与迭代相比,递归具有大量开销。
    • 递归:递归具有重复调用函数的开销,这是由于重复调用同一函数导致代码的时间复杂性增加。
    • 迭代:迭代不涉及任何此类开销。
  4. 无限重复:无限重复递归会导致CPU崩溃,但在迭代中,它将在内存耗尽时停止。
    • 递归:在递归中,由于指定基本条件时会发生一些错误,因此可能会发生无限递归调用,这种错误永远不会变为假,而是继续调用该函数,这可能会导致系统CPU崩溃。
    • 迭代:由于迭代器分配或增量错误或终止条件而导致的无限迭代将导致无限循环,这可能会或可能不会导致系统错误,但肯定会进一步停止程序执行。
Property Recursion Iteration
Definition Function calls itself. A set of instructions repeatedly executed.
Application For functions. For loops.
Termination Through base case, where there will be no function call. When the termination condition for the iterator ceases to be satisfied.
Usage Used when code size needs to be small, and time complexity is not an issue. Used when time complexity needs to be balanced against an expanded code size.
Code Size Smaller code size Larger Code Size.
Time Complexity Very high(generally exponential) time complexity. Relatively lower time complexity(generally polynomial-logarithmic).