我更喜欢尽可能少的正式定义和简单的数学。


当前回答


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

其他回答


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

这里来了大O,告诉我们这个数学是多么艰难。

现在他们要求我添加一个到十个!为什么我会这样做?我不想添加一个到六个?添加一个到十个......好......这会更难!

对于大 n 而言, n 平方比 n 更大。

一个:我选择一张卡在我们现在工作的地板部分,你可以为我选择一个,如果你愿意(第一次我们这样做,“我们现在工作的地板部分”是整个地板,当然。

大 O 告诉我们:这个类型需要 O(n log n) 工作完成,在中间情况下。

现在你知道我的朋友,大O,他帮助我们做不到工作,如果你知道大O,你也可以做不到工作!

你和我一起学到了这一切!你太聪明了!谢谢你!

现在这个工作已经完成了,让我们玩吧!


定义 : 大 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

什么是“大O”笔记的明确英语解释?

我想强调“大O”评分的驱动动力是一件事,当算法的输入尺寸变得太大时,算法的某些部分(即恒数、比例、术语)的方程式描述算法的尺寸变得如此无意义,以至于我们忽略它们。

因此,如果输入尺寸不太大,那么“大O”评分(上限)的想法将毫无意义。


Lets say you want to quantify the performance of the following algorithm
int sumArray (int[] nums){
    int sum=0;   // here we've 1 operation
    for(int i=0; i < nums.length;i++){   // we've n times
        sum += nums[i]; // taking initialization and assignments, 3 ops
    }
    return sum;
}

在上面的算法中,让我们说你发现T(n)如下(时间复杂性):

T(n) = 3*n + 2

n= 1,000,000   -> T(1,000,000) = 3,000,002
n=1,000,000,000  -> T(1,000,000,000) = 3,000,000,002
n=10,000,000,000  -> T(10,000,000,000) = 30,000,000,002

将此类输入给另一个函数 F(n) = n

n= 1,000,000   -> F(1,000,000) = 1,000,000 
n=1,000,000,000  -> F(1,000,000,000) = 1,000,000,000
n=10,000,000,000  -> F(10,000,000,000) = 10,000,000,000

因为你可以看到输入尺寸变得太大,T(n)大约相当于或接近F(n),所以连续2和比例3变得太不重要,现在大O“评级的想法来了,

O(T(n)) = F(n)
O(T(n)) = n


最简单的定义我可以给大 Oh 评分是:

智者可能已经意识到,我们可以表达操作的数量如:n2 + 2n. 但是,正如你从我们的例子中看到的两个数字的百万数字左右,第二个术语(2n)变得毫无意义(计算为0.0002%的总操作在这个阶段)。

因此,要找到一个名字给了电话号码(逆转搜索):

最佳案例:O(1);预期案例:O(n)(为500,000);最糟糕案例:O(n)(为1000,000)。

旅行卖家

听起来很简单吗?再想一想。

聚合物时间

另一个我想快速提到的是,任何具有O(na)复杂性的算法都说具有多元复杂性,或者在多元时间可溶解。