📜  渐近分析指南

📅  最后修改于: 2022-05-13 01:56:08.129000             🧑  作者: Mango

渐近分析指南

在本文中,重点是学习一些有助于确定算法运行时间的规则。

渐近分析是指以数学计算单位计算任何操作的运行时间。在渐近分析中,根据输入大小(我们不测量实际运行时间)评估算法的性能。还计算了算法占用的时间(或空间)如何随着输入大小的增加而增加。

循环循环的运行时间最多是循环内语句的运行时间,包括测试)乘以迭代次数。

下面是演示上述概念的Python程序:

Python3
# Python program to implement
# the above concept
# execute n times for in
# range(0.00);
 
for i in range(0, n):
print ('Current Number:', i, sep = "")


Python3
# Python program to implement
# the above concept
# outer loop executed n times
for i in range(0, n):
 
    # inner loop executes n times
    for j in range(0, n):
      print("i value % d  and j value % d" % (i, j))


Python3
# Python program that implements
# the above concept
n = 100
 
# executes n times
for i in range (0, n):
    print (Current Number: i, sep = "")
     
    # outer loop executed n times
    for i in range (0, n):
       
      # inner loop executes n times
      for j in range(0, n):
        print(" i value % d and j value % d"%(i, j))
 
X


Python3
# Python program that implements
# the above concept
if n == I:
  print ("Incorrect Value")
  print (n)
 
else:
  for i in range(0, n):
     
    # constant time
    print (CurrNumber:, i, sep = "")


#constant time 
Total time a constant cx n = cn = O(n).

嵌套循环从内到外分析。总运行时间是所有循环大小的乘积。

下面是一个演示上述概念的Python程序:

Python3

# Python program to implement
# the above concept
# outer loop executed n times
for i in range(0, n):
 
    # inner loop executes n times
    for j in range(0, n):
      print("i value % d  and j value % d" % (i, j))
# constant time 
Total time = C x n x n = cn^2 =0(n²).

连续语句添加每个语句的时间复杂度。

下面是一个演示上述概念的Python程序:

Python3

# Python program that implements
# the above concept
n = 100
 
# executes n times
for i in range (0, n):
    print (Current Number: i, sep = "")
     
    # outer loop executed n times
    for i in range (0, n):
       
      # inner loop executes n times
      for j in range(0, n):
        print(" i value % d and j value % d"%(i, j))
 
X


Total time = co + c1n + c2n^2 = 0(n^2).

If-then-else 语句最坏情况运行时间:测试,加上 else 部分的 then 部分,以最大者为准。

下面是一个演示上述概念的Python程序:

Python3

# Python program that implements
# the above concept
if n == I:
  print ("Incorrect Value")
  print (n)
 
else:
  for i in range(0, n):
     
    # constant time
    print (CurrNumber:, i, sep = "")
Total time = co + c1*n = 0(n).