Polish the content

This commit is contained in:
krahets
2023-08-21 03:06:53 +08:00
parent 981144e42d
commit f5dda8d99a
19 changed files with 49 additions and 51 deletions

View File

@ -34,7 +34,7 @@
/* 函数 */
int function() {
// do something...
// 执行某些操作...
return 0;
}
@ -59,7 +59,7 @@
/* 函数 */
int func() {
// do something...
// 执行某些操作...
return 0;
}
@ -83,7 +83,7 @@
def function() -> int:
"""函数"""
# do something...
# 执行某些操作...
return 0
def algorithm(n) -> int: # 输入数据
@ -110,7 +110,7 @@
/* 函数 */
func function() int {
// do something...
// 执行某些操作...
return 0
}
@ -138,7 +138,7 @@
/* 函数 */
function constFunc() {
// do something
// 执行某些操作
return 0;
}
@ -166,7 +166,7 @@
/* 函数 */
function constFunc(): number {
// do something
// 执行某些操作
return 0;
}
@ -184,7 +184,7 @@
```c title=""
/* 函数 */
int func() {
// do something...
// 执行某些操作...
return 0;
}
@ -208,7 +208,7 @@
/* 函数 */
int function() {
// do something...
// 执行某些操作...
return 0;
}
@ -236,7 +236,7 @@
/* 函数 */
func function() -> Int {
// do something...
// 执行某些操作...
return 0
}
@ -267,7 +267,7 @@
/* 函数 */
int function() {
// do something...
// 执行某些操作...
return 0;
}
@ -435,7 +435,7 @@
```java title=""
int function() {
// do something
// 执行某些操作
return 0;
}
/* 循环 O(1) */
@ -455,7 +455,7 @@
```cpp title=""
int func() {
// do something
// 执行某些操作
return 0;
}
/* 循环 O(1) */
@ -475,7 +475,7 @@
```python title=""
def function() -> int:
# do something
# 执行某些操作
return 0
def loop(n: int):
@ -493,7 +493,7 @@
```go title=""
func function() int {
// do something
// 执行某些操作
return 0
}
@ -517,7 +517,7 @@
```javascript title=""
function constFunc() {
// do something
// 执行某些操作
return 0;
}
/* 循环 O(1) */
@ -537,7 +537,7 @@
```typescript title=""
function constFunc(): number {
// do something
// 执行某些操作
return 0;
}
/* 循环 O(1) */
@ -557,7 +557,7 @@
```c title=""
int func() {
// do something
// 执行某些操作
return 0;
}
/* 循环 O(1) */
@ -577,7 +577,7 @@
```csharp title=""
int function() {
// do something
// 执行某些操作
return 0;
}
/* 循环 O(1) */
@ -598,7 +598,7 @@
```swift title=""
@discardableResult
func function() -> Int {
// do something
// 执行某些操作
return 0
}
@ -628,7 +628,7 @@
```dart title=""
int function() {
// do something
// 执行某些操作
return 0;
}
/* 循环 O(1) */
@ -847,7 +847,7 @@ $$
[class]{}-[func]{linear}
```
以下递归函数会同时存在 $n$ 个未返回的 `algorithm()` 函数,使用 $O(n)$ 大小的栈帧空间:
以下函数的递归深度为 $n$ ,即同时存在 $n$ 个未返回的 `linear_recur()` 函数,使用 $O(n)$ 大小的栈帧空间:
=== "Java"
@ -999,7 +999,7 @@ $$
[class]{}-[func]{quadratic}
```
以下递归函数中,同时存在 $n$ 个未返回的 `algorithm()` ,并且每个函数中都初始化了一个数组,长度分别为 $n, n-1, n-2, ..., 2, 1$ ,平均长度为 $\frac{n}{2}$ ,因此总体占用 $O(n^2)$ 空间。
以下函数的递归深度为 $n$ ,在每个递归函数中都初始化了一个数组,长度分别为 $n, n-1, n-2, ..., 2, 1$ ,平均长度为 $n / 2$ ,因此总体占用 $O(n^2)$ 空间。
=== "Java"
@ -1155,11 +1155,9 @@ $$
### 对数阶 $O(\log n)$
对数阶常见于分治算法和数据类型转换等
对数阶常见于分治算法。例如归并排序,输入长度为 $n$ 的数组,每轮递归将数组从中点划分为两半,形成高度为 $\log n$ 的递归树,使用 $O(\log n)$ 栈帧空间
例如归并排序算法,输入长度为 $n$ 的数组,每轮递归将数组从中点划分为两半,形成高度为 $\log n$ 的递归树,使用 $O(\log n)$ 栈帧空间
再例如将数字转化为字符串,输入任意正整数 $n$ ,它的位数为 $\log_{10} n + 1$ ,即对应字符串长度为 $\log_{10} n + 1$ ,因此空间复杂度为 $O(\log_{10} n + 1) = O(\log n)$ 。
例如将数字转化为字符串,输入一个正整数 $n$ ,它的位数为 $\log_{10} n + 1$ ,即对应字符串长度为 $\log_{10} n + 1$ ,因此空间复杂度为 $O(\log_{10} n + 1) = O(\log n)$ 。
## 权衡时间与空间

View File

@ -1210,11 +1210,7 @@ $$
![常数阶、线性阶和平方阶的时间复杂度](time_complexity.assets/time_complexity_constant_linear_quadratic.png)
以冒泡排序为例,外层循环执行 $n - 1$ 次,内层循环执行 $n-1, n-2, \cdots, 2, 1$ 次,平均为 $\frac{n}{2}$ 次,因此时间复杂度为 $O(n^2)$
$$
O((n - 1) \frac{n}{2}) = O(n^2)
$$
以冒泡排序为例,外层循环执行 $n - 1$ 次,内层循环执行 $n-1, n-2, \cdots, 2, 1$ 次,平均为 $n / 2$ 次,因此时间复杂度为 $O((n - 1) n / 2) = O(n^2)$
=== "Java"
@ -1596,7 +1592,11 @@ $$
[class]{}-[func]{log_recur}
```
对数阶常出现于基于分治策略的算法中,体现了“一分为多”和“化繁为简”的算法思想。它增长缓慢,是理想的时间复杂度,仅次于常数阶
对数阶常出现于基于分治策略的算法中,体现了“一分为多”和“化繁为简”的算法思想。它增长缓慢,是仅次于常数阶的理想的时间复杂度。
!!! tip
“一分为 $m$”对应的时间复杂度 $O(\log_m n)$ 。我们通常会省略底数 $m$ ,直接将其记为 $O(\log n)$ 。
### 线性对数阶 $O(n \log n)$
@ -1762,7 +1762,7 @@ $$
![阶乘阶的时间复杂度](time_complexity.assets/time_complexity_factorial.png)
请注意,因为 $n! > 2^n$ ,所以阶乘阶比指数阶增长得更快,在 $n$ 较大时也是不可接受的。
请注意,因为当 $n \geq 4$ 时恒有 $n! > 2^n$ ,所以阶乘阶比指数阶增长得更快,在 $n$ 较大时也是不可接受的。
## 最差、最佳、平均时间复杂度
@ -1892,7 +1892,7 @@ $$
从上述示例可以看出,最差或最佳时间复杂度只出现于“特殊的数据分布”,这些情况的出现概率可能很小,并不能真实地反映算法运行效率。相比之下,**平均时间复杂度可以体现算法在随机输入数据下的运行效率**,用 $\Theta$ 记号来表示。
对于部分算法,我们可以简单地推算出随机数据分布下的平均情况。比如上述示例,由于输入数组是被打乱的,因此元素 $1$ 出现在任意索引的概率都是相等的,那么算法的平均循环次数就是数组长度的一半 $\frac{n}{2}$ ,平均时间复杂度为 $\Theta(\frac{n}{2}) = \Theta(n)$ 。
对于部分算法,我们可以简单地推算出随机数据分布下的平均情况。比如上述示例,由于输入数组是被打乱的,因此元素 $1$ 出现在任意索引的概率都是相等的,那么算法的平均循环次数就是数组长度的一半 $n / 2$ ,平均时间复杂度为 $\Theta(n / 2) = \Theta(n)$ 。
但对于较为复杂的算法,计算平均时间复杂度往往是比较困难的,因为很难分析出在数据分布下的整体数学期望。在这种情况下,我们通常使用最差时间复杂度作为算法效率的评判标准。