📜  Big-O分析的Ionic

📅  最后修改于: 2021-04-22 03:47:49             🧑  作者: Mango

先决条件:算法分析|大O分析

在上一篇文章中,讨论了使用Big O渐近符号表示的算法分析。在本文中,将讨论一些示例来说明Big O时间复杂度表示法,并学习如何计算任何程序的时间复杂度。

有不同的渐近符号可用来度量算法的时间复杂度。在这里,使用“ O”(大O)表示法来获取时间复杂度。时间复杂度估计了运行算法的时间。它是通过计算基本运算来计算的。始终以仅依赖于算法及其输入的方式来了解执行时间的原因始终是一种好习惯。这可以通过选择算法重复执行的基本运算并将时间复杂度T(N)定义为算法在给定长度N的数组中执行的此类运算的数量来实现。

范例1

带有基本操作的循环的时间复杂度假设这些操作花费单位时间来执行。这个单位时间可以用O(1)表示。如果循环运行N次而没有任何比较。以下是相同的插图:

C++
// C++ program to illustrate time
// complexity for single for-loop
#include 
using namespace std;
 
// Driver Code
int main()
{
    int a = 0, b = 0;
    int N = 4, M = 4;
 
    // This loop runs for N time
    for (int i = 0; i < N; i++) {
        a = a + 10;
    }
    // This loop runs for M time
    for (int i = 0; i < M; i++) {
        b = b + 40;
    }
 
    cout << a << ' ' << b;
 
    return 0;
}


Java
// Java program to illustrate time
// complexity for single for-loop
class GFG
{
 
  // Driver Code
  public static void main(String[] args)
  {
    int a = 0, b = 0;
    int N = 4, M = 4;
 
    // This loop runs for N time
    for (int i = 0; i < N; i++)
    {
      a = a + 10;
    }
 
    // This loop runs for M time
    for (int i = 0; i < M; i++)
    {
      b = b + 40;
    }
    System.out.print(a + " " + b);
  }
}
 
// This code is contributed by rutvik_56


C#
// C# program to illustrate time
// complexity for single for-loop
using System;
class GFG
{
 
  // Driver Code
  public static void Main(string[] args)
  {
    int a = 0, b = 0;
    int N = 4, M = 4;
 
    // This loop runs for N time
    for (int i = 0; i < N; i++)
    {
      a = a + 10;
    }
 
    // This loop runs for M time
    for (int i = 0; i < M; i++)
    {
      b = b + 40;
    }
    Console.Write(a + " " + b);
  }
}
 
// This code is contributed by pratham76.


C++
// C++ program to illustrate time
// complexity for nested loop
#include 
using namespace std;
 
// Driver Code
int main()
{
    int a = 0, b = 0;
    int N = 4, M = 5;
 
    // Nested loops
    for (int i = 0; i < N; i++) {
        for (int j = 0; j < M; j++) {
            a = a + j;
 
            // Print the current
            // value of a
            cout << a << ' ';
        }
        cout << endl;
    }
    return 0;
}


C++
// C++ program to illustrate time
// complexity of the form O(log2 N)
#include 
using namespace std;
 
// Driver Code
int main()
{
    int N = 8, k = 0;
 
    // First loop run N/2 times
    for (int i = N / 2; i <= N; i++) {
 
        // Inner loop run log N
        // times for all i
        for (int j = 2; j <= N;
             j = j * 2) {
 
            // Print the value k
            cout << k << ' ';
 
            k = k + N / 2;
        }
    }
 
    return 0;
}


C++
// C++ program to illustrate time
// complexity while updating the
// iteration
#include 
using namespace std;
 
// Driver Code
int main()
{
    int N = 18;
    int i = N, a = 0;
 
    // Iterate until i is greater
    // than 0
    while (i > 0) {
 
        // Print the value of a
        cout << a << ' ';
        a = a + i;
 
        // Update i
        i = i / 2;
    }
 
    return 0;
}


输出:
40 160

说明:这里的时间复杂度将是O(N + M) 。循环1是运行N次的单个for循环,其内部的计算需要O(1)时间。类似地,另一个循环通过将两个不同的循环相加(相加)而花费M倍
O(N + M + 1)= O(N + M)

范例2

熟悉基本操作和单循环之后。现在,要查找嵌套循环的时间复杂度,假定两个循环的迭代次数不同。可以看到,如果外部循环运行一次,内部循环将运行M次,从而给我们一个序列,即M + M + M + M + M……….N次,则可以写成N * M。以下是相同的插图:

C++

// C++ program to illustrate time
// complexity for nested loop
#include 
using namespace std;
 
// Driver Code
int main()
{
    int a = 0, b = 0;
    int N = 4, M = 5;
 
    // Nested loops
    for (int i = 0; i < N; i++) {
        for (int j = 0; j < M; j++) {
            a = a + j;
 
            // Print the current
            // value of a
            cout << a << ' ';
        }
        cout << endl;
    }
    return 0;
}
输出:
0 1 3 6 10 
10 11 13 16 20 
20 21 23 26 30 
30 31 33 36 40

例子3

得到以上问题后。我们有两个迭代器,其中一个迭代器运行N / 2次,并且我们知道循环的时间复杂度被视为O(log N) ,如果迭代器被常数K乘以/则其时间复杂度被认为是O(log K N) 。下面是相同的插图:

C++

// C++ program to illustrate time
// complexity of the form O(log2 N)
#include 
using namespace std;
 
// Driver Code
int main()
{
    int N = 8, k = 0;
 
    // First loop run N/2 times
    for (int i = N / 2; i <= N; i++) {
 
        // Inner loop run log N
        // times for all i
        for (int j = 2; j <= N;
             j = j * 2) {
 
            // Print the value k
            cout << k << ' ';
 
            k = k + N / 2;
        }
    }
 
    return 0;
}

输出:
0 4 8 12 16 20 24 28 32 36 40 44 48 52 56

例子4

现在,让我们了解while循环,并尝试将迭代器更新为表达式。以下是相同的插图:

C++

// C++ program to illustrate time
// complexity while updating the
// iteration
#include 
using namespace std;
 
// Driver Code
int main()
{
    int N = 18;
    int i = N, a = 0;
 
    // Iterate until i is greater
    // than 0
    while (i > 0) {
 
        // Print the value of a
        cout << a << ' ';
        a = a + i;
 
        // Update i
        i = i / 2;
    }
 
    return 0;
}

输出:
0 18 27 31 33

说明:上面代码的等式可以给出为:

示例5找到时间复杂度的另一种方法是将它们转换为表达式,然后使用以下方法获得所需的结果。给定基于该算法的表达式,任务是求解并找到时间复杂度。这种方法更容易,因为它使用基本的数学计算来扩展给定的公式以获得特定的解决方案。下面是了解该方法的两个示例。

脚步:

  • 找到第(N – 1)迭代/步骤的解决方案。
  • 同样,为下一步计算。
  • 一旦,你熟悉的模式,找到k步骤的解决方案。
  • 找到N次解,求解得到的表达式。

以下是相同的插图:

第三种也是最简单的方法是使用Master定理或计算时间复杂度。有关使用“大师定理”查找时间复杂度的信息,请参阅本文。