我更喜欢尽可能少的正式定义和简单的数学。
当前回答
大O只是一种方式来“表达”自己,以一种常见的方式,“运行我的代码需要多少时间/空间?”
因此,你可能明白“n2”是什么意思,但要更具体,玩你的想法,你有一个简单的,最简单的分类算法;泡沫分类。
我的名单
比较 1 和 6 是最大的? Ok 6 是正确的位置,前进! 比较 6 和 3, oh, 3 是更少的! 让我们移动, Ok 列表改变了,我们需要从现在开始!
為每個項目,你再看所有項目一次,為比較,這也是“n”,所以為每個項目,你看“n”時刻意味著n*n = n2
我希望这就像你想要的那样简单。
其他回答
最简单的定义我可以给大 Oh 评分是:
智者可能已经意识到,我们可以表达操作的数量如:n2 + 2n. 但是,正如你从我们的例子中看到的两个数字的百万数字左右,第二个术语(2n)变得毫无意义(计算为0.0002%的总操作在这个阶段)。
因此,要找到一个名字给了电话号码(逆转搜索):
最佳案例:O(1);预期案例:O(n)(为500,000);最糟糕案例:O(n)(为1000,000)。
旅行卖家
听起来很简单吗?再想一想。
聚合物时间
另一个我想快速提到的是,任何具有O(na)复杂性的算法都说具有多元复杂性,或者在多元时间可溶解。
测量软件程序的速度非常困难,当我们尝试时,答案可以非常复杂,并且充满了例外和特殊案例,这是一个很大的问题,因为所有这些例外和特殊案例都令人沮丧和无助,当我们想比较两个不同的程序,以确定哪个是“最快”。
好事:
邪恶的:
和那可怕的:
定义 : 大 O 评级是指如果数据输入增加,算法性能将如何表现的评级。
当我们谈论算法时,有3个重要柱子 算法输入、输出和处理 大 O 是象征性的评分,如果数据输入增加到什么速度,算法处理的性能将有所不同。
例如,请参见下面的函数“函数1”,该函数收集并在第一个记录中进行处理,现在该函数的性能将是相同的,无论您放置1000、10万或100000记录。
void Function1(List<string> data)
{
string str = data[0];
}
void Function2(List<string> data)
{
foreach(string str in data)
{
if (str == "shiv")
{
return;
}
}
}
因此,通过查看Big O评级,我们分类算法的好和坏区域。
此分類上一篇
https://www.youtube.com/watch?v=k6kxtzICG_g
actualAlgorithmTime(N) ∈ O(bound(N))
e.g. "time to mergesort N elements
is O(N log(N))"
actualAlgorithmTime(N) e.g. "mergesort_duration(N) "
────────────────────── < constant ───────────────────── < 2.5
bound(N) N log(N)
#handshakes(N)
────────────── ≈ 1/2
N²
N²/2 - N/2 (N²)/2 N/2 1/2
lim ────────── = lim ( ────── - ─── ) = lim ─── = 1/2
N→∞ N² N→∞ N² N² N→∞ 1
┕━━━┙
this is 0 in the limit of N→∞:
graph it, or plug in a really large number for N
这让我们做出这样的陈述......
我把时间的倍增到一个O(N)(“线性时间”)算法所需要的时间。
某些无形上级的算法(例如,非比较的O(N log(N))类型)可能具有如此大的恒定的因素(例如,100000*N log(N))),或相对较大的顶部,如O(N log(N))与隐藏的+100*N,它们很少值得使用,即使在“大数据”。
for(i=0; i<A; i++) // A * ...
some O(1) operation // 1
--> A*1 --> O(A) time
visualization:
|<------ A ------->|
1 2 3 4 5 x x ... x
other languages, multiplying orders of growth:
javascript, O(A) time and space
someListOfSizeA.map((x,i) => [x,i])
python, O(rows*cols) time and space
[[r*c for c in range(cols)] for r in range(rows)]
for every x in listOfSizeA: // A * (...
some O(1) operation // 1
some O(B) operation // B
for every y in listOfSizeC: // C * (...
some O(1) operation // 1))
--> O(A*(1 + B + C))
O(A*(B+C)) (1 is dwarfed)
visualization:
|<------ A ------->|
1 x x x x x x ... x
2 x x x x x x ... x ^
3 x x x x x x ... x |
4 x x x x x x ... x |
5 x x x x x x ... x B <-- A*B
x x x x x x x ... x |
................... |
x x x x x x x ... x v
x x x x x x x ... x ^
x x x x x x x ... x |
x x x x x x x ... x |
x x x x x x x ... x C <-- A*C
x x x x x x x ... x |
................... |
x x x x x x x ... x v
例子3:
function nSquaredFunction(n) {
total = 0
for i in 1..n: // N *
for j in 1..n: // N *
total += i*k // 1
return total
}
// O(n^2)
function nCubedFunction(a) {
for i in 1..n: // A *
print(nSquaredFunction(a)) // A^2
}
// O(a^3)
如果我们做一些有点复杂的事情,你可能仍然能够视觉地想象正在发生的事情:
for x in range(A):
for y in range(1..x):
simpleOperation(x*y)
x x x x x x x x x x |
x x x x x x x x x |
x x x x x x x x |
x x x x x x x |
x x x x x x |
x x x x x |
x x x x |
x x x |
x x |
x___________________|
<----------------------------- N ----------------------------->
x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x
x x x x x x x x x x x x x x x x
x x x x x x x x
x x x x
x x
x
<----------------------------- N ----------------------------->
x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x x
x x x x x x x x x x x x x x x x|x x x x x x x x|x x x x|x x|x
<----------------------------- N ----------------------------->
^ x x x x x x x x x x x x x x x x|x x x x x x x x x x x x x x x x
| x x x x x x x x|x x x x x x x x|x x x x x x x x|x x x x x x x x
lgN x x x x|x x x x|x x x x|x x x x|x x x x|x x x x|x x x x|x x x x
| x x|x x|x x|x x|x x|x x|x x|x x|x x|x x|x x|x x|x x|x x|x x|x x
v x|x|x|x|x|x|x|x|x|x|x|x|x|x|x|x|x|x|x|x|x|x|x|x|x|x|x|x|x|x|x|x
[myDictionary.has(x) for x in listOfSizeA]
\----- O(1) ------/
--> A*1 --> O(A)
混合和中型案例复杂性
(请参见中间案例和折扣分析之间的差异,如果您对此主题感兴趣。
数学 Addenda
Big-O 是由程序所消耗的资源增加率,即问题例大小。
资源:可能是CPU时间,可能是最大 RAM 空间。
说问题是“找到金额”,
int Sum(int*arr,int size){
int sum=0;
while(size-->0)
sum+=arr[size];
return sum;
}
problem-instance= {5,10,15} ==> problem-instance-size = 3, iterations-in-loop= 3
problem-instance= {5,10,15,20,25} ==> problem-instance-size = 5 iterations-in-loop = 5
说问题是“找到组合”,
void Combination(int*arr,int size)
{ int outer=size,inner=size;
while(outer -->0) {
inner=size;
while(inner -->0)
cout<<arr[outer]<<"-"<<arr[inner]<<endl;
}
}
problem-instance= {5,10,15} ==> problem-instance-size = 3, total-iterations = 3*3 = 9
problem-instance= {5,10,15,20,25} ==> problem-instance-size = 5, total-iterations= 5*5 = 25
对于“n”尺寸的输入,该程序以序列中的“n*n”节点的速度生长,因此,Big-O是N2以O(n2)表达。