diff --git a/docs/chapter_computational_complexity/time_complexity.md b/docs/chapter_computational_complexity/time_complexity.md index 09aee7b79..676ee1689 100644 --- a/docs/chapter_computational_complexity/time_complexity.md +++ b/docs/chapter_computational_complexity/time_complexity.md @@ -153,6 +153,21 @@ $$ } ``` +=== "Rust" + + ```rust title="" + // 在某运行平台下 + fn algorithm(n: i32) { + let mut a = 2; // 1 ns + a = a + 1; // 1 ns + a = a * 2; // 10 ns + // 循环 n 次 + for _ in 0..n { // 1 ns + println!("{}", 0); // 5 ns + } + } + ``` + 但实际上, **统计算法的运行时间既不合理也不现实**。首先,我们不希望预估时间和运行平台绑定,毕竟算法需要跑在各式各样的平台之上。其次,我们很难获知每一种操作的运行时间,这为预估过程带来了极大的难度。 ## 统计时间增长趋势 @@ -355,6 +370,29 @@ $$ print(0) } } + + ``` +=== "Rust" + + ```rust title="" + // 算法 A 时间复杂度:常数阶 + fn algorithm_A(n: i32) { + println!("{}", 0); + } + + // 算法 B 时间复杂度:线性阶 + fn algorithm_B(n: i32) { + for i in 0..n { + println!("{}", 0); + } + } + + // 算法 C 时间复杂度:常数阶 + fn algorithm_C(n: i32) { + for i in 0..1000000 { + println!("{}", 0); + } + } ``` ![time_complexity_first_example](time_complexity.assets/time_complexity_first_example.png) @@ -503,6 +541,20 @@ $$ } ``` +=== "Rust" + + ```rust title="" + fn algorithm(n: i32) { + let mut a = 2; // +1 + a = a + 1; // +1 + a = a * 2; // +1 + // 循环 n 次 + for _ in 0..n { // +1 + println!("0"); // +1 + } + } + ``` + $T(n)$ 是个一次函数,说明时间增长趋势是线性的,因此易得时间复杂度是线性阶。 我们将线性阶的时间复杂度记为 $O(n)$ ,这个数学符号被称为「大 $O$ 记号 Big-$O$ Notation」,代表函数 $T(n)$ 的「渐近上界 asymptotic upper bound」。 @@ -725,6 +777,26 @@ $$ } ``` +=== "Rust" + + ```rust title="" + fn algorithm(n: i32) { + let mut a = 1; // +0(技巧 1) + a = a + n; // +0(技巧 1) + // +n(技巧 2) + for _ in 0..(5 * n + 1) { + println!("{}", 0); + } + // +n*n(技巧 3) + for _ in 0..(2 * n) { + for _ in 0..(n + 1) { + println!("{}", 0); + } + } + } + ``` + + ### 2. 判断渐近上界 **时间复杂度由多项式 $T(n)$ 中最高阶的项来决定**。这是因为在 $n$ 趋于无穷大时,最高阶的项将处于主导作用,其它项的影响都可以被忽略。 @@ -887,6 +959,20 @@ $$ } ``` +=== "Rust" + + ```rust title="time_complexity.rust" + /* 常数阶 */ + fn constant(n: i32) -> i32 { + let mut count = 0; + let size = 100000; + for _ in 0..size { + count += 1 + } + count + } + ``` + ### 线性阶 $O(n)$ 线性阶的操作数量相对输入数据大小成线性级别增长。线性阶常出现于单层循环。 @@ -1000,6 +1086,19 @@ $$ } ``` +=== "Rust" + + ```rust title="time_complexity.rust" + /* 线性阶 */ + fn linear(n: i32) -> i32 { + let mut count = 0; + for _ in 0..n { + count += 1; + } + count + } + ``` + 「遍历数组」和「遍历链表」等操作,时间复杂度都为 $O(n)$ ,其中 $n$ 为数组或链表的长度。 !!! tip @@ -1132,6 +1231,20 @@ $$ } ``` +=== "Rust" + + ```rust title="time_complexity.rust" + /* 线性阶(遍历数组) */ + fn array_traversal(nums: &[i32]) -> i32 { + let mut count = 0; + // 循环次数与数组长度成正比 + for _ in nums { + count += 1; + } + count + } + ``` + ### 平方阶 $O(n^2)$ 平方阶的操作数量相对输入数据大小成平方级别增长。平方阶常出现于嵌套循环,外层循环和内层循环都为 $O(n)$ ,总体为 $O(n^2)$ 。 @@ -1280,6 +1393,22 @@ $$ } ``` +=== "Rust" + + ```rust title="time_complexity.rust" + /* 平方阶 */ + fn quadratic(n: i32) -> i32 { + let mut count = 0 + // 循环次数与数组长度成平方关系 + for _ in 0..n { + for _ in 0..n { + count += 1; + } + } + count + } + ``` + ![time_complexity_constant_linear_quadratic](time_complexity.assets/time_complexity_constant_linear_quadratic.png)

Fig. 常数阶、线性阶、平方阶的时间复杂度

@@ -1497,6 +1626,30 @@ $$ } ``` + +=== "Rust" + + ```rust title="time_complexity.swift" + /* 平方阶(冒泡排序) */ + fn bubble_sort(nums: &mut [i32]) -> i32 { + let mut count = 0; // 计数器 + // 外循环:待排序元素数量为 n-1, n-2, ..., 1 + for i in (1..nums.len()).rev() { + // 内循环:冒泡操作 + for j in 0..i { + if nums[j] > nums[j + 1] { + // 交换 nums[j] 与 nums[j + 1] + let tmp = nums[j]; + nums[j] = nums[j + 1]; + nums[j + 1] = tmp; + count += 3; // 元素交换包含 3 个单元操作 + } + } + } + count + } + ``` + ### 指数阶 $O(2^n)$ !!! note @@ -1672,6 +1825,25 @@ $$ } ``` +=== "Rust" + + ```rust title="time_complexity.rust" + /* 指数阶(循环实现) */ + fn exponential(n: i32) -> i32 { + let mut count = 0; + let mut base = 1; + // cell 每轮一分为二,形成数列 1, 2, 4, 8, ..., 2^(n-1) + for _ in 0..n { + for _ in 0..base { + count += 1 + } + base *= 2; + } + // count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1 + count + } + ``` + ![time_complexity_exponential](time_complexity.assets/time_complexity_exponential.png)

Fig. 指数阶的时间复杂度

@@ -1773,6 +1945,18 @@ $$ } ``` +=== "Rust" + + ```rust title="time_complexity.rust" + /* 指数阶(递归实现) */ + fn exp_recur(n: i32) -> i32 { + if n == 1 { + return 1; + } + exp_recur(n - 1) + exp_recur(n - 1) + 1 + } + ``` + ### 对数阶 $O(\log n)$ 对数阶与指数阶正好相反,后者反映“每轮增加到两倍的情况”,而前者反映“每轮缩减到一半的情况”。对数阶仅次于常数阶,时间增长得很慢,是理想的时间复杂度。 @@ -1908,6 +2092,21 @@ $$ } ``` +=== "Rust" + + ```rust title="time_complexity.rust" + /* 对数阶(循环实现) */ + fn logarithmic(mut n: i32) -> i32 { + let mut count = 0; + + while n > 1 { + n = n / 2; + count += 1; + } + count + } + ``` + ![time_complexity_logarithmic](time_complexity.assets/time_complexity_logarithmic.png)

Fig. 对数阶的时间复杂度

@@ -2008,6 +2207,18 @@ $$ } ``` +=== "Rust" + + ```rust title="time_complexity.rust" + /* 对数阶(递归实现) */ + fn log_recur(n: i32) -> i32 { + if n <= 1 { + return 0; + } + log_recur(n / 2) + 1 + } + ``` + ### 线性对数阶 $O(n \log n)$ 线性对数阶常出现于嵌套循环中,两层循环的时间复杂度分别为 $O(\log n)$ 和 $O(n)$ 。 @@ -2150,6 +2361,23 @@ $$ } ``` +=== "Rust" + + ```rust title="time_complexity.rust" + /* 线性对数阶 */ + fn linear_log_recur(n: f64) -> i32 { + if n <= 1.0 { + return 1; + } + let mut count = linear_log_recur(n / 2.0) + linear_log_recur(n / 2.0); + for _ in 0 ..n as i32 { + count += 1; + } + return count + } + ``` + + ![time_complexity_logarithmic_linear](time_complexity.assets/time_complexity_logarithmic_linear.png)

Fig. 线性对数阶的时间复杂度

@@ -2302,6 +2530,23 @@ $$ } ``` +=== "Rust" + + ```rust title="time_complexity.rust" + /* 阶乘阶(递归实现) */ + fn factorial_recur(n: i32) -> i32 { + if n == 0 { + return 1; + } + let mut count = 0; + // 从 1 个分裂出 n 个 + for _ in 0..n { + count += factorial_recur(n - 1); + } + count + } + ``` + ![time_complexity_factorial](time_complexity.assets/time_complexity_factorial.png)

Fig. 阶乘阶的时间复杂度

@@ -2689,6 +2934,40 @@ $$ } ``` +=== "Rust" + + ```rust title="worst_best_time_complexity.rust" + /* 生成一个数组,元素为 { 1, 2, ..., n },顺序被打乱 */ + fn random_numbers(n: i32) -> Vec { + // 生成数组 nums = { 1, 2, 3, ..., n } + let mut nums = (1..n + 1).collect::>(); + // 随机打乱数组元素 + nums.shuffle(&mut thread_rng()); + nums + } + + /* 查找数组 nums 中数字 1 所在索引 */ + fn find_one(nums: &[i32]) -> Option { + for i in 0..nums.len() { + if nums[i] == 1 { + return Some(i); + } + } + None + } + + /* Driver Code */ + fn main() { + for _ in 0..10 { + let n = 100; + let nums = random_numbers(n); + let index = find_one(&nums); + println!("\n数组 [ 1, 2, ..., n ] 被打乱后 = {:?}", nums); + println!("数字 1 的索引为 {:?}", index); + } + } + ``` + !!! tip 我们在实际应用中很少使用「最佳时间复杂度」,因为往往只有很小概率下才能达到,会带来一定的误导性。反之,「最差时间复杂度」最为实用,因为它给出了一个“效率安全值”,让我们可以放心地使用算法。