fix: update O(n) section and O(n2) section and updated time

This commit is contained in:
Leon Xu committed 2022-03-15 13:25:23 -07:00
1 parent 6ed0de7776
commit baa3858b97
1 file changed
+3 -3
+3 -3
View File
@@ -7,7 +7,7 @@ categories:
abbrlink: 18e39c1f
mathjax: true
date: 2021-10-05 12:51:00
updated: 2022-03-08 13:32:00
updated: 2022-03-15 13:21:00
---
在我之前的[BigO(大O表示法)](https://www.angulareportfolio.pw/posts/64501)文章里面,我详细的谈到了关于大O表示法的运行时间,我想写这篇文章来补充一些新的运行时间以及一些例子。
@@ -41,7 +41,7 @@ for (let i = 0; i <= n; i+=1) {
```
这段代码,for循环里面的代码会执行n遍,因此它消耗的时间是随着n的变化而变化的,因此这类的代码都可以用<math xmlns='http://www.w3.org/1998/Math/MathML'><mi>O</mi><mrow><mo>(</mo><mi>n</mi><mo>)</mo></mrow></math>来表示它的时间复杂度。
# <math xmlns='http://www.w3.org/1998/Math/MathML'><mi>O</mi><mrow><mo>(</mo><mi>1</mi><mo>)</mo></mrow><mo>+</mo><mi>O</mi><mrow><mo>(</mo><mi>n</mi><mo>)</mo></mrow><mo>=</mo><mi>O</mi><mrow><mo>(</mo><mi>n</mi><mo>)</mo></mrow></math>
# 常数阶+线性阶=线性阶 <math xmlns='http://www.w3.org/1998/Math/MathML'><mi>O</mi><mrow><mo>(</mo><mi>1</mi><mo>)</mo></mrow><mo>+</mo><mi>O</mi><mrow><mo>(</mo><mi>n</mi><mo>)</mo></mrow><mo>=</mo><mi>O</mi><mrow><mo>(</mo><mi>n</mi><mo>)</mo></mrow></math>
那如果我将上面的<math xmlns='http://www.w3.org/1998/Math/MathML'><mi>O</mi><mrow><mo>(</mo><mi>1</mi><mo>)</mo></mrow></math>时间复杂度的代码进行执行,接下来在执行一个for循环,那整个的时间复杂度是多少呢?我们来看一下以下代码:
```javascript
let i = 0;
@@ -52,7 +52,7 @@ for (let j = 0; j < n: j+=1) {
```
计算时间复杂度的时候,如果两个时间复杂度先后排列,我们就把它们相加,而且要取增长趋势更快的时间复杂度。所以,它整段的代码的时间复杂度将会是<math xmlns='http://www.w3.org/1998/Math/MathML'><mi>O</mi><mrow><mo>(</mo><mi>n</mi><mo>)</mo></mrow></math>
# <math xmlns='http://www.w3.org/1998/Math/MathML'><mi> O </mi><mrow><mo>(</mo><mi>n</mi><mo>)</mo></mrow><mo>&#x00D7;<!-- multiplication sign --></mo><mi>O</mi><mrow><mo>(</mo><mi>n</mi><mo>)</mo></mrow><mo>=</mo><mi>O</mi><mrow><mo>(</mo><msup><mrow><mi>n</mi></mrow><mrow><mn>2</mn></mrow></msup><mo>)</mo></mrow></math>
# 线性阶×线性阶 = 平方阶 <math xmlns='http://www.w3.org/1998/Math/MathML'><mi> O </mi><mrow><mo>(</mo><mi>n</mi><mo>)</mo></mrow><mo> x </mo><mi>O</mi><mrow><mo>(</mo><mi>n</mi><mo>)</mo></mrow><mo>=</mo><mi>O</mi><mrow><mo>(</mo><msup><mrow><mi>n</mi></mrow><mrow><mn>2</mn></mrow></msup><mo>)</mo></mrow></math>
一般含有双层嵌套,且每层循环下的语句执行次数为n的算法涉及平方时间复杂度。这类算法随着问题规模n的增大,对应计算次数呈平方关系增长。