第二章 Big O notation

来源:互联网 发布:火箭nba 知乎 编辑:程序博客网 时间:2024/06/05 06:38


第二章 

Big O notation

计算机的计算过程中,基本上都是一些复杂的计算,数以千计,数以万计或是数以亿计的计算,那么如何计算和总结为让我们更加简单易懂的语言呢,与成绩分层是一个道理,A是好的,B次好等等等等,那么我们就引入了big O notation这个概念。

在这里,我们程序员如果要进行编程,我们不希望计算机花费大量的时间去进行一个运算,对于我们和用户来说,我们要尽全力将big O 弄到最小。

1,2,3,4,5,6,7的是O(1),是属于常数的
n,2n,2n+1,4m+4的是O(n),是属于线性函数
n^2 4n^2+3等等的话就是O(n^2),二次的线性函数
等等

那么,如果一个函数它是以多项式来表示的,那么他的big O notation是什么呢?
给大家一个我们学校的例子,

当我们要看一个多项式的时候,General rule,pick the term that grows the fastest and remove
the constants from it:
5n + 2log(n) is O(n).
3n^3 +2^n/6 is O(2^n ).
8log(n) + 7n is O(n).

从增长速度最小到速度最快:
loglogn
logn (logarithmic)
sqrt n
n (linear)
n log(n)
n^2 (quadratic)
n^3 (cubic)
...
n^k (polynomial hierarchy)
a^n (exponential hierarchy)
n!

以上是big o notation的基础,下一期我会详细的介绍到实例

0 0