mirror of
https://github.com/krahets/hello-algo.git
synced 2025-12-19 07:17:54 +08:00
Release Rust code to documents. (#656)
This commit is contained in:
@@ -168,6 +168,12 @@ $$
|
||||
}
|
||||
```
|
||||
|
||||
=== "Rust"
|
||||
|
||||
```rust title=""
|
||||
|
||||
```
|
||||
|
||||
然而实际上,**统计算法的运行时间既不合理也不现实**。首先,我们不希望预估时间和运行平台绑定,因为算法需要在各种不同的平台上运行。其次,我们很难获知每种操作的运行时间,这给预估过程带来了极大的难度。
|
||||
|
||||
## 统计时间增长趋势
|
||||
@@ -394,6 +400,12 @@ $$
|
||||
}
|
||||
```
|
||||
|
||||
=== "Rust"
|
||||
|
||||
```rust title=""
|
||||
|
||||
```
|
||||
|
||||

|
||||
|
||||
相较于直接统计算法运行时间,时间复杂度分析有哪些优势和局限性呢?
|
||||
@@ -556,6 +568,12 @@ $$
|
||||
}
|
||||
```
|
||||
|
||||
=== "Rust"
|
||||
|
||||
```rust title=""
|
||||
|
||||
```
|
||||
|
||||
$T(n)$ 是一次函数,说明时间增长趋势是线性的,因此可以得出时间复杂度是线性阶。
|
||||
|
||||
我们将线性阶的时间复杂度记为 $O(n)$ ,这个数学符号称为「大 $O$ 记号 Big-$O$ Notation」,表示函数 $T(n)$ 的「渐近上界 Asymptotic Upper Bound」。
|
||||
@@ -795,6 +813,12 @@ $$
|
||||
}
|
||||
```
|
||||
|
||||
=== "Rust"
|
||||
|
||||
```rust title=""
|
||||
|
||||
```
|
||||
|
||||
### 第二步:判断渐近上界
|
||||
|
||||
**时间复杂度由多项式 $T(n)$ 中最高阶的项来决定**。这是因为在 $n$ 趋于无穷大时,最高阶的项将发挥主导作用,其他项的影响都可以被忽略。
|
||||
@@ -902,6 +926,12 @@ $$
|
||||
[class]{}-[func]{constant}
|
||||
```
|
||||
|
||||
=== "Rust"
|
||||
|
||||
```rust title="time_complexity.rs"
|
||||
[class]{}-[func]{constant}
|
||||
```
|
||||
|
||||
### 线性阶 $O(n)$
|
||||
|
||||
线性阶的操作数量相对于输入数据大小以线性级别增长。线性阶通常出现在单层循环中。
|
||||
@@ -972,6 +1002,12 @@ $$
|
||||
[class]{}-[func]{linear}
|
||||
```
|
||||
|
||||
=== "Rust"
|
||||
|
||||
```rust title="time_complexity.rs"
|
||||
[class]{}-[func]{linear}
|
||||
```
|
||||
|
||||
遍历数组和遍历链表等操作的时间复杂度均为 $O(n)$ ,其中 $n$ 为数组或链表的长度。
|
||||
|
||||
!!! question "如何确定输入数据大小 $n$ ?"
|
||||
@@ -1044,6 +1080,12 @@ $$
|
||||
[class]{}-[func]{arrayTraversal}
|
||||
```
|
||||
|
||||
=== "Rust"
|
||||
|
||||
```rust title="time_complexity.rs"
|
||||
[class]{}-[func]{array_traversal}
|
||||
```
|
||||
|
||||
### 平方阶 $O(n^2)$
|
||||
|
||||
平方阶的操作数量相对于输入数据大小以平方级别增长。平方阶通常出现在嵌套循环中,外层循环和内层循环都为 $O(n)$ ,因此总体为 $O(n^2)$ 。
|
||||
@@ -1114,6 +1156,12 @@ $$
|
||||
[class]{}-[func]{quadratic}
|
||||
```
|
||||
|
||||
=== "Rust"
|
||||
|
||||
```rust title="time_complexity.rs"
|
||||
[class]{}-[func]{quadratic}
|
||||
```
|
||||
|
||||

|
||||
|
||||
以「冒泡排序」为例,外层循环执行 $n - 1$ 次,内层循环执行 $n-1, n-2, \cdots, 2, 1$ 次,平均为 $\frac{n}{2}$ 次,因此时间复杂度为 $O(n^2)$ 。
|
||||
@@ -1188,6 +1236,12 @@ $$
|
||||
[class]{}-[func]{bubbleSort}
|
||||
```
|
||||
|
||||
=== "Rust"
|
||||
|
||||
```rust title="time_complexity.rs"
|
||||
[class]{}-[func]{bubble_sort}
|
||||
```
|
||||
|
||||
### 指数阶 $O(2^n)$
|
||||
|
||||
!!! note
|
||||
@@ -1262,6 +1316,12 @@ $$
|
||||
[class]{}-[func]{exponential}
|
||||
```
|
||||
|
||||
=== "Rust"
|
||||
|
||||
```rust title="time_complexity.rs"
|
||||
[class]{}-[func]{exponential}
|
||||
```
|
||||
|
||||

|
||||
|
||||
在实际算法中,指数阶常出现于递归函数。例如以下代码,不断地一分为二,经过 $n$ 次分裂后停止。
|
||||
@@ -1332,6 +1392,12 @@ $$
|
||||
[class]{}-[func]{expRecur}
|
||||
```
|
||||
|
||||
=== "Rust"
|
||||
|
||||
```rust title="time_complexity.rs"
|
||||
[class]{}-[func]{exp_recur}
|
||||
```
|
||||
|
||||
### 对数阶 $O(\log n)$
|
||||
|
||||
与指数阶相反,对数阶反映了“每轮缩减到一半的情况”。对数阶仅次于常数阶,时间增长缓慢,是理想的时间复杂度。
|
||||
@@ -1406,6 +1472,12 @@ $$
|
||||
[class]{}-[func]{logarithmic}
|
||||
```
|
||||
|
||||
=== "Rust"
|
||||
|
||||
```rust title="time_complexity.rs"
|
||||
[class]{}-[func]{logarithmic}
|
||||
```
|
||||
|
||||

|
||||
|
||||
与指数阶类似,对数阶也常出现于递归函数。以下代码形成了一个高度为 $\log_2 n$ 的递归树。
|
||||
@@ -1476,6 +1548,12 @@ $$
|
||||
[class]{}-[func]{logRecur}
|
||||
```
|
||||
|
||||
=== "Rust"
|
||||
|
||||
```rust title="time_complexity.rs"
|
||||
[class]{}-[func]{log_recur}
|
||||
```
|
||||
|
||||
### 线性对数阶 $O(n \log n)$
|
||||
|
||||
线性对数阶常出现于嵌套循环中,两层循环的时间复杂度分别为 $O(\log n)$ 和 $O(n)$ 。
|
||||
@@ -1548,6 +1626,12 @@ $$
|
||||
[class]{}-[func]{linearLogRecur}
|
||||
```
|
||||
|
||||
=== "Rust"
|
||||
|
||||
```rust title="time_complexity.rs"
|
||||
[class]{}-[func]{linear_log_recur}
|
||||
```
|
||||
|
||||

|
||||
|
||||
### 阶乘阶 $O(n!)$
|
||||
@@ -1626,6 +1710,12 @@ $$
|
||||
[class]{}-[func]{factorialRecur}
|
||||
```
|
||||
|
||||
=== "Rust"
|
||||
|
||||
```rust title="time_complexity.rs"
|
||||
[class]{}-[func]{factorial_recur}
|
||||
```
|
||||
|
||||

|
||||
|
||||
## 最差、最佳、平均时间复杂度
|
||||
@@ -1744,6 +1834,14 @@ $$
|
||||
[class]{}-[func]{findOne}
|
||||
```
|
||||
|
||||
=== "Rust"
|
||||
|
||||
```rust title="worst_best_time_complexity.rs"
|
||||
[class]{}-[func]{random_numbers}
|
||||
|
||||
[class]{}-[func]{find_one}
|
||||
```
|
||||
|
||||
!!! tip
|
||||
|
||||
实际应用中我们很少使用「最佳时间复杂度」,因为通常只有在很小概率下才能达到,可能会带来一定的误导性。相反,「最差时间复杂度」更为实用,因为它给出了一个“效率安全值”,让我们可以放心地使用算法。
|
||||
|
||||
Reference in New Issue
Block a user