|
|
@ -153,6 +153,12 @@ $$
|
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
=== "Zig"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
```zig title=""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
但实际上, **统计算法的运行时间既不合理也不现实**。首先,我们不希望预估时间和运行平台绑定,毕竟算法需要跑在各式各样的平台之上。其次,我们很难获知每一种操作的运行时间,这为预估过程带来了极大的难度。
|
|
|
|
但实际上, **统计算法的运行时间既不合理也不现实**。首先,我们不希望预估时间和运行平台绑定,毕竟算法需要跑在各式各样的平台之上。其次,我们很难获知每一种操作的运行时间,这为预估过程带来了极大的难度。
|
|
|
|
|
|
|
|
|
|
|
|
## 2.2.2. 统计时间增长趋势
|
|
|
|
## 2.2.2. 统计时间增长趋势
|
|
|
@ -357,6 +363,12 @@ $$
|
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
=== "Zig"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
```zig title=""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
![time_complexity_first_example](time_complexity.assets/time_complexity_first_example.png)
|
|
|
|
![time_complexity_first_example](time_complexity.assets/time_complexity_first_example.png)
|
|
|
|
|
|
|
|
|
|
|
|
<p align="center"> Fig. 算法 A, B, C 的时间增长趋势 </p>
|
|
|
|
<p align="center"> Fig. 算法 A, B, C 的时间增长趋势 </p>
|
|
|
@ -503,6 +515,12 @@ $$
|
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
=== "Zig"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
```zig title=""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
$T(n)$ 是个一次函数,说明时间增长趋势是线性的,因此易得时间复杂度是线性阶。
|
|
|
|
$T(n)$ 是个一次函数,说明时间增长趋势是线性的,因此易得时间复杂度是线性阶。
|
|
|
|
|
|
|
|
|
|
|
|
我们将线性阶的时间复杂度记为 $O(n)$ ,这个数学符号被称为「大 $O$ 记号 Big-$O$ Notation」,代表函数 $T(n)$ 的「渐近上界 asymptotic upper bound」。
|
|
|
|
我们将线性阶的时间复杂度记为 $O(n)$ ,这个数学符号被称为「大 $O$ 记号 Big-$O$ Notation」,代表函数 $T(n)$ 的「渐近上界 asymptotic upper bound」。
|
|
|
@ -725,6 +743,12 @@ $$
|
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
=== "Zig"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
```zig title=""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
### 2) 判断渐近上界
|
|
|
|
### 2) 判断渐近上界
|
|
|
|
|
|
|
|
|
|
|
|
**时间复杂度由多项式 $T(n)$ 中最高阶的项来决定**。这是因为在 $n$ 趋于无穷大时,最高阶的项将处于主导作用,其它项的影响都可以被忽略。
|
|
|
|
**时间复杂度由多项式 $T(n)$ 中最高阶的项来决定**。这是因为在 $n$ 趋于无穷大时,最高阶的项将处于主导作用,其它项的影响都可以被忽略。
|
|
|
@ -887,6 +911,12 @@ $$
|
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
=== "Zig"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
```zig title="time_complexity.zig"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
### 线性阶 $O(n)$
|
|
|
|
### 线性阶 $O(n)$
|
|
|
|
|
|
|
|
|
|
|
|
线性阶的操作数量相对输入数据大小成线性级别增长。线性阶常出现于单层循环。
|
|
|
|
线性阶的操作数量相对输入数据大小成线性级别增长。线性阶常出现于单层循环。
|
|
|
@ -1000,6 +1030,12 @@ $$
|
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
=== "Zig"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
```zig title="time_complexity.zig"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
「遍历数组」和「遍历链表」等操作,时间复杂度都为 $O(n)$ ,其中 $n$ 为数组或链表的长度。
|
|
|
|
「遍历数组」和「遍历链表」等操作,时间复杂度都为 $O(n)$ ,其中 $n$ 为数组或链表的长度。
|
|
|
|
|
|
|
|
|
|
|
|
!!! tip
|
|
|
|
!!! tip
|
|
|
@ -1132,6 +1168,12 @@ $$
|
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
=== "Zig"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
```zig title="time_complexity.zig"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
### 平方阶 $O(n^2)$
|
|
|
|
### 平方阶 $O(n^2)$
|
|
|
|
|
|
|
|
|
|
|
|
平方阶的操作数量相对输入数据大小成平方级别增长。平方阶常出现于嵌套循环,外层循环和内层循环都为 $O(n)$ ,总体为 $O(n^2)$ 。
|
|
|
|
平方阶的操作数量相对输入数据大小成平方级别增长。平方阶常出现于嵌套循环,外层循环和内层循环都为 $O(n)$ ,总体为 $O(n^2)$ 。
|
|
|
@ -1280,6 +1322,12 @@ $$
|
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
=== "Zig"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
```zig title="time_complexity.zig"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
![time_complexity_constant_linear_quadratic](time_complexity.assets/time_complexity_constant_linear_quadratic.png)
|
|
|
|
![time_complexity_constant_linear_quadratic](time_complexity.assets/time_complexity_constant_linear_quadratic.png)
|
|
|
|
|
|
|
|
|
|
|
|
<p align="center"> Fig. 常数阶、线性阶、平方阶的时间复杂度 </p>
|
|
|
|
<p align="center"> Fig. 常数阶、线性阶、平方阶的时间复杂度 </p>
|
|
|
@ -1500,6 +1548,12 @@ $$
|
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
=== "Zig"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
```zig title="time_complexity.zig"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
### 指数阶 $O(2^n)$
|
|
|
|
### 指数阶 $O(2^n)$
|
|
|
|
|
|
|
|
|
|
|
|
!!! note
|
|
|
|
!!! note
|
|
|
@ -1675,6 +1729,12 @@ $$
|
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
=== "Zig"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
```zig title="time_complexity.zig"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
![time_complexity_exponential](time_complexity.assets/time_complexity_exponential.png)
|
|
|
|
![time_complexity_exponential](time_complexity.assets/time_complexity_exponential.png)
|
|
|
|
|
|
|
|
|
|
|
|
<p align="center"> Fig. 指数阶的时间复杂度 </p>
|
|
|
|
<p align="center"> Fig. 指数阶的时间复杂度 </p>
|
|
|
@ -1776,6 +1836,12 @@ $$
|
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
=== "Zig"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
```zig title="time_complexity.zig"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
### 对数阶 $O(\log n)$
|
|
|
|
### 对数阶 $O(\log n)$
|
|
|
|
|
|
|
|
|
|
|
|
对数阶与指数阶正好相反,后者反映“每轮增加到两倍的情况”,而前者反映“每轮缩减到一半的情况”。对数阶仅次于常数阶,时间增长得很慢,是理想的时间复杂度。
|
|
|
|
对数阶与指数阶正好相反,后者反映“每轮增加到两倍的情况”,而前者反映“每轮缩减到一半的情况”。对数阶仅次于常数阶,时间增长得很慢,是理想的时间复杂度。
|
|
|
@ -1911,6 +1977,12 @@ $$
|
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
=== "Zig"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
```zig title="time_complexity.zig"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
![time_complexity_logarithmic](time_complexity.assets/time_complexity_logarithmic.png)
|
|
|
|
![time_complexity_logarithmic](time_complexity.assets/time_complexity_logarithmic.png)
|
|
|
|
|
|
|
|
|
|
|
|
<p align="center"> Fig. 对数阶的时间复杂度 </p>
|
|
|
|
<p align="center"> Fig. 对数阶的时间复杂度 </p>
|
|
|
@ -2011,6 +2083,12 @@ $$
|
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
=== "Zig"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
```zig title="time_complexity.zig"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
### 线性对数阶 $O(n \log n)$
|
|
|
|
### 线性对数阶 $O(n \log n)$
|
|
|
|
|
|
|
|
|
|
|
|
线性对数阶常出现于嵌套循环中,两层循环的时间复杂度分别为 $O(\log n)$ 和 $O(n)$ 。
|
|
|
|
线性对数阶常出现于嵌套循环中,两层循环的时间复杂度分别为 $O(\log n)$ 和 $O(n)$ 。
|
|
|
@ -2153,6 +2231,12 @@ $$
|
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
=== "Zig"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
```zig title="time_complexity.zig"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
![time_complexity_logarithmic_linear](time_complexity.assets/time_complexity_logarithmic_linear.png)
|
|
|
|
![time_complexity_logarithmic_linear](time_complexity.assets/time_complexity_logarithmic_linear.png)
|
|
|
|
|
|
|
|
|
|
|
|
<p align="center"> Fig. 线性对数阶的时间复杂度 </p>
|
|
|
|
<p align="center"> Fig. 线性对数阶的时间复杂度 </p>
|
|
|
@ -2305,6 +2389,12 @@ $$
|
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
=== "Zig"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
```zig title="time_complexity.zig"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
![time_complexity_factorial](time_complexity.assets/time_complexity_factorial.png)
|
|
|
|
![time_complexity_factorial](time_complexity.assets/time_complexity_factorial.png)
|
|
|
|
|
|
|
|
|
|
|
|
<p align="center"> Fig. 阶乘阶的时间复杂度 </p>
|
|
|
|
<p align="center"> Fig. 阶乘阶的时间复杂度 </p>
|
|
|
@ -2692,6 +2782,12 @@ $$
|
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
=== "Zig"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
```zig title="worst_best_time_complexity.zig"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
|
|
!!! tip
|
|
|
|
!!! tip
|
|
|
|
|
|
|
|
|
|
|
|
我们在实际应用中很少使用「最佳时间复杂度」,因为往往只有很小概率下才能达到,会带来一定的误导性。反之,「最差时间复杂度」最为实用,因为它给出了一个“效率安全值”,让我们可以放心地使用算法。
|
|
|
|
我们在实际应用中很少使用「最佳时间复杂度」,因为往往只有很小概率下才能达到,会带来一定的误导性。反之,「最差时间复杂度」最为实用,因为它给出了一个“效率安全值”,让我们可以放心地使用算法。
|
|
|
|