算法学习之Asymptotic Analysis

Asymptotic Analysis是一种简化的分析算法时间复杂度的方法,它主要应用big Oh notation。

the rules for computing and manipulating big oh expressions greatly simplify the analysis of the running time of a program when all we are interested in is its asymptotic behavior.


一、几种基本的Big Oh表达式

O(1)                         --    constant
O(log10(n))             --    logarithmic
O((log10(n))^2)       --    log squared
O(n)                          --    linear
O(n * log10(n))        --    n log n
O(n^2)                      --    quadratic
O(n^3)                      --    cubic
O(2^n)                      --    exponential

一般地,使用这几种表达式去套一种算法,然后再证明它们是最匹配的。


二、3个基本规则

Rule 1 : (Sequential composition) The worst- case running time of a sequence

即,某个算法函数中,有多条语句(statement)序列,应用big Oh的和性质,该算法函数的Big Oh表达式等于,这些语句中,big Oh表达式函数中最大的那个。参考:点击打开链接


Rule 2 : (Iteration) The worst-case running time of a C++ for loop 

对于C++中的for循环,应用big Oh的积性质,它的big Oh表达式等于,循环体语句的big Oh函数乘以迭代次数。


Rule 3 : (Execution) The worst-case running time of a C++ if/else

对于C++中的if/else语句,它的运行时间等于条件判断语句+执行体语句中时间开销较大的那个分支。




你可能感兴趣的:(算法学习之Asymptotic Analysis)