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


当前回答

算法例(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

其他回答

“什么是明确的英语解释大O?尽可能少的正式定义和简单的数学。

这样一个美丽简单而短暂的问题似乎至少值得一个同样短暂的答案,就像一个学生在教学期间可以得到的那样。

大 O 评级简单地说明一个算法可以运行多长时间,仅仅是输入数据的数量。

(在一个美妙的,无单位的时间感中!)(这就是重要,因为人们总是想要更多,无论他们生活在今天还是明天)

好吧,什么是那么奇妙的关于大O评级,如果这就是它做什么?

实际上,Big O分析是如此有用和重要,因为Big O把重点放在算法本身的复杂性上,完全忽略了一切只是比例性恒定的东西 - 如JavaScript引擎,CPU的速度,您的互联网连接,以及所有快速变成像模型T一样可笑的过时的东西。

大O只是一种方式来“表达”自己,以一种常见的方式,“运行我的代码需要多少时间/空间?”

因此,你可能明白“n2”是什么意思,但要更具体,玩你的想法,你有一个简单的,最简单的分类算法;泡沫分类。

我的名单

比较 1 和 6 是最大的? Ok 6 是正确的位置,前进! 比较 6 和 3, oh, 3 是更少的! 让我们移动, Ok 列表改变了,我们需要从现在开始!

為每個項目,你再看所有項目一次,為比較,這也是“n”,所以為每個項目,你看“n”時刻意味著n*n = n2

我希望这就像你想要的那样简单。

TLDR:Big O在数学术语中解释算法的性能。

较慢的算法倾向于在 n 运行到 x 或多个,取决于它的深度,而更快的,如二进制搜索运行在 O(log n),这使得它运行更快,因为数据集变得更大。

可以从算法中最复杂的线路计算大O看。

有了小型或未分类的数据集,Big O 可能令人惊讶,因为 n log n 复杂性算法如二进制搜索可以缓慢较小的或未分类的集,为一个简单的运行例子线性搜索与二进制搜索,请参见我的JavaScript例子:

https://codepen.io/serdarsenay/pen/XELWqN?editors=1011(下面的算法)

function lineerSearch() {
  init();
  var t = timer('lineerSearch benchmark');
  var input = this.event.target.value;
  for(var i = 0;i<unsortedhaystack.length - 1;i++) {
    if (unsortedhaystack[i] === input) {
      document.getElementById('result').innerHTML = 'result is... "' + unsortedhaystack[i] + '", on index: ' + i + ' of the unsorted array. Found' + ' within ' + i + ' iterations';
      console.log(document.getElementById('result').innerHTML);
      t.stop(); 
      return unsortedhaystack[i]; 
    }
  }
}

function binarySearch () {
  init();
  sortHaystack();
  var t = timer('binarySearch benchmark');
  var firstIndex = 0;
  var lastIndex = haystack.length-1;
  var input = this.event.target.value;

  //currently point in the half of the array
  var currentIndex = (haystack.length-1)/2 | 0;
  var iterations = 0;

  while (firstIndex <= lastIndex) {
    currentIndex = (firstIndex + lastIndex)/2 | 0;
    iterations++;
    if (haystack[currentIndex]  < input) {
      firstIndex = currentIndex + 1;
      //console.log(currentIndex + " added, fI:"+firstIndex+", lI: "+lastIndex);
    } else if (haystack[currentIndex] > input) {
      lastIndex = currentIndex - 1;
      //console.log(currentIndex + " substracted, fI:"+firstIndex+", lI: "+lastIndex);
    } else {
      document.getElementById('result').innerHTML = 'result is... "' + haystack[currentIndex] + '", on index: ' + currentIndex + ' of the sorted array. Found' + ' within ' + iterations + ' iterations';
      console.log(document.getElementById('result').innerHTML);
      t.stop(); 
      return true;
    }
  }
}

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