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


当前回答

算法例(Java):

public boolean search(/* for */Integer K,/* in */List</* of */Integer> L)
{
    for(/* each */Integer i:/* in */L)
    {
        if(i == K)
        {
            return true;
        }
    }
    
    return false;
}

算法描述:

这个算法搜索一个列表,项目按项目,寻找一个密钥,在列表中的每个项目,如果它是密钥,然后返回真实,如果循环没有找到密钥,返回虚假。

Big-O 评分代表了复杂性(时间、空间等)的顶端。

要找到 The Big-O on Time Complexity:

计算时间(考虑到输入大小)最糟糕的案例需要: 最糟糕的案例: 关键不在列表中 时间(Worst-Case) = 4n+1 时间: O(4n+1) = O(n) <unk>在大O,恒例被忽视 O(n) ~ 线性

还有大欧米加,它代表了最佳案例的复杂性:

最佳案例:关键是第一个项目 时间(最佳案例) = 4 时间: Ω(4) = O(1) ~ Instant\Constant

其他回答

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)表达。


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

算法例(Java):

public boolean search(/* for */Integer K,/* in */List</* of */Integer> L)
{
    for(/* each */Integer i:/* in */L)
    {
        if(i == K)
        {
            return true;
        }
    }
    
    return false;
}

算法描述:

这个算法搜索一个列表,项目按项目,寻找一个密钥,在列表中的每个项目,如果它是密钥,然后返回真实,如果循环没有找到密钥,返回虚假。

Big-O 评分代表了复杂性(时间、空间等)的顶端。

要找到 The Big-O on Time Complexity:

计算时间(考虑到输入大小)最糟糕的案例需要: 最糟糕的案例: 关键不在列表中 时间(Worst-Case) = 4n+1 时间: O(4n+1) = O(n) <unk>在大O,恒例被忽视 O(n) ~ 线性

还有大欧米加,它代表了最佳案例的复杂性:

最佳案例:关键是第一个项目 时间(最佳案例) = 4 时间: Ω(4) = O(1) ~ Instant\Constant

告诉你从亚马逊订购哈利·波特:完整的8电影收藏(Blu-ray)并同时在线下载相同的电影收藏。你想测试哪种方法更快。

从实验中,我们知道在线购物的规模比在线下载更好,很重要的是要了解大O评级,因为它有助于分析算法的规模性和效率。

注意: 大 O 评级是算法最糟糕的场景,假设 O(1) 和 O(n) 是上面的例子最糟糕的场景。

参考: http://carlcheo.com/compsci

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