From 5e4f0e2bdee419fefcb43475c9827a723eb7c433 Mon Sep 17 00:00:00 2001 From: xblakicex Date: Tue, 10 Jan 2023 15:59:41 +0100 Subject: [PATCH 1/7] =?UTF-8?q?=E2=9C=A8=20feat:=20Add=20rust=20installati?= =?UTF-8?q?on=20environment?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/chapter_preface/installation.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/docs/chapter_preface/installation.md b/docs/chapter_preface/installation.md index c6c1b2ee9..a21ac247d 100644 --- a/docs/chapter_preface/installation.md +++ b/docs/chapter_preface/installation.md @@ -46,3 +46,8 @@ comments: true 1. 下载并安装 [Swift](https://www.swift.org/download/); 2. 在 VSCode 的插件市场中搜索 `swift`,安装 [Swift for Visual Studio Code](https://marketplace.visualstudio.com/items?itemName=sswg.swift-lang)。 + +## Rust 环境 + +1. 下载并安装 [Rust](https://www.rust-lang.org/tools/install); +2. 在 VSCode 的插件市场中搜索 `rust`,安装 [rust-analyzer](https://marketplace.visualstudio.com/items?itemName=rust-lang.rust-analyzer)。 \ No newline at end of file From 520201a645a0c0a7dc562753a5d0eb3b59522520 Mon Sep 17 00:00:00 2001 From: xblakicex Date: Tue, 10 Jan 2023 19:37:40 +0100 Subject: [PATCH 2/7] =?UTF-8?q?=E2=9C=A8=20feat(time=5Fcomplexity):=20add?= =?UTF-8?q?=20rust=20lang=20code?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../time_complexity.md | 279 ++++++++++++++++++ 1 file changed, 279 insertions(+) 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 我们在实际应用中很少使用「最佳时间复杂度」,因为往往只有很小概率下才能达到,会带来一定的误导性。反之,「最差时间复杂度」最为实用,因为它给出了一个“效率安全值”,让我们可以放心地使用算法。 From 81b5d9f0bd74a7b1e2fc2cb4d114c241a8e4bcc6 Mon Sep 17 00:00:00 2001 From: xblakicex Date: Tue, 10 Jan 2023 22:29:06 +0100 Subject: [PATCH 3/7] =?UTF-8?q?=F0=9F=8C=88=20style:=20remov=20redundant?= =?UTF-8?q?=20line?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/chapter_computational_complexity/time_complexity.md | 2 -- 1 file changed, 2 deletions(-) diff --git a/docs/chapter_computational_complexity/time_complexity.md b/docs/chapter_computational_complexity/time_complexity.md index 676ee1689..a6a749fa1 100644 --- a/docs/chapter_computational_complexity/time_complexity.md +++ b/docs/chapter_computational_complexity/time_complexity.md @@ -1626,7 +1626,6 @@ $$ } ``` - === "Rust" ```rust title="time_complexity.swift" @@ -2377,7 +2376,6 @@ $$ } ``` - ![time_complexity_logarithmic_linear](time_complexity.assets/time_complexity_logarithmic_linear.png)

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

From 29c5049c7de2d49d43f904ba89bbc9c77d2e9b16 Mon Sep 17 00:00:00 2001 From: xblakicex Date: Tue, 10 Jan 2023 22:38:32 +0100 Subject: [PATCH 4/7] =?UTF-8?q?=E2=9C=A8=20feat(code):=20add=20rust=20time?= =?UTF-8?q?=5Fcomplexity.rs=20example?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../time_complexity.rs | 163 ++++++++++++++++++ 1 file changed, 163 insertions(+) create mode 100644 codes/rust/chapter_computational_complexity/time_complexity.rs diff --git a/codes/rust/chapter_computational_complexity/time_complexity.rs b/codes/rust/chapter_computational_complexity/time_complexity.rs new file mode 100644 index 000000000..a6aa60b31 --- /dev/null +++ b/codes/rust/chapter_computational_complexity/time_complexity.rs @@ -0,0 +1,163 @@ +#![allow(unused_variables)] + +/* 常数阶 */ +fn constant(n: i32) -> i32 { + let mut count = 0; + let size = 100000; + for _ in 0..size { + count += 1 + } + count +} + +fn linear(n: i32) -> i32 { + let mut count = 0; + for _ in 0..n { + count += 1; + } + count +} + +/* 线性阶(遍历数组) */ +fn array_traversal(nums: &[i32]) -> i32 { + let mut count = 0; + // 循环次数与数组长度成正比 + for _ in nums { + count += 1; + } + count +} + +fn quadratic(n: i32) -> i32 { + let mut count = 0; + // 循环次数与数组长度成平方关系 + for _ in 0..n { + for _ in 0..n { + count += 1; + } + } + count +} + +/* 平方阶(冒泡排序) */ +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 +} + +/* 指数阶(循环实现) */ +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 +} + +/* 指数阶(递归实现) */ +fn exp_recur(n: i32) -> i32 { + if n == 1 { + return 1; + } + exp_recur(n - 1) + exp_recur(n - 1) + 1 +} + +/* 对数阶(循环实现) */ +fn logarithmic(mut n: i32) -> i32 { + let mut count = 0; + + while n > 1 { + n = n / 2; + count += 1; + } + count +} + +fn log_recur(n: i32) -> i32 { + if n <= 1 { + return 0; + } + log_recur(n / 2) + 1 +} + +/* 线性对数阶 */ +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 +} + +/* 阶乘阶(递归实现) */ +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 +} + +/* Driver Code */ +fn main() { + // 可以修改 n 运行,体会一下各种复杂度的操作数量变化趋势 + let n: i32 = 8; + println!("输入数据大小 n = {}", n); + + let mut count = constant(n); + println!("常数阶的计算操作数量 = {}", count); + + count = linear(n); + println!("线性阶的计算操作数量 = {}", count); + count = array_traversal(&vec![0; n as usize]); + println!("线性阶(遍历数组)的计算操作数量 = {}", count); + + count = quadratic(n); + println!("平方阶的计算操作数量 = {}", count); + + let mut nums = (1..=n).rev().collect::>(); // [n,n-1,...,2,1] + count = bubble_sort(&mut nums); + println!("平方阶(冒泡排序)的计算操作数量 = {}", count); + + count = exponential(n); + println!("指数阶(循环实现)的计算操作数量 = {}", count); + count = exp_recur(n); + println!("指数阶(递归实现)的计算操作数量 = {}", count); + + count = logarithmic(n); + println!("对数阶(循环实现)的计算操作数量 = {}", count); + count = log_recur(n); + println!("对数阶(递归实现)的计算操作数量 = {}", count); + + count = linear_log_recur(n.into()); + println!("线性对数阶(递归实现)的计算操作数量 = {}", count); + + count = factorial_recur(n); + println!("阶乘阶(递归实现)的计算操作数量 = {}", count); +} \ No newline at end of file From 342ee2b242d2e2a8e13243ad8cd9b04d66ff6ac9 Mon Sep 17 00:00:00 2001 From: xblakicex Date: Wed, 11 Jan 2023 12:19:28 +0100 Subject: [PATCH 5/7] =?UTF-8?q?=F0=9F=A6=84=20refactor(docs):=20temp=20rem?= =?UTF-8?q?ove=20rust=20code=20in=20time=5Fcomplexity.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../time_complexity.md | 276 ------------------ docs/chapter_preface/installation.md | 5 - 2 files changed, 281 deletions(-) diff --git a/docs/chapter_computational_complexity/time_complexity.md b/docs/chapter_computational_complexity/time_complexity.md index a6a749fa1..92f5a5193 100644 --- a/docs/chapter_computational_complexity/time_complexity.md +++ b/docs/chapter_computational_complexity/time_complexity.md @@ -153,21 +153,6 @@ $$ } ``` -=== "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 - } - } - ``` - 但实际上, **统计算法的运行时间既不合理也不现实**。首先,我们不希望预估时间和运行平台绑定,毕竟算法需要跑在各式各样的平台之上。其次,我们很难获知每一种操作的运行时间,这为预估过程带来了极大的难度。 ## 统计时间增长趋势 @@ -372,29 +357,6 @@ $$ } ``` -=== "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)

Fig. 算法 A, B, C 的时间增长趋势

@@ -541,20 +503,6 @@ $$ } ``` -=== "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」。 @@ -777,25 +725,6 @@ $$ } ``` -=== "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. 判断渐近上界 @@ -959,20 +888,6 @@ $$ } ``` -=== "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)$ 线性阶的操作数量相对输入数据大小成线性级别增长。线性阶常出现于单层循环。 @@ -1086,19 +1001,6 @@ $$ } ``` -=== "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 @@ -1231,20 +1133,6 @@ $$ } ``` -=== "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)$ 。 @@ -1393,22 +1281,6 @@ $$ } ``` -=== "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. 常数阶、线性阶、平方阶的时间复杂度

@@ -1626,29 +1498,6 @@ $$ } ``` -=== "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 @@ -1824,25 +1673,6 @@ $$ } ``` -=== "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. 指数阶的时间复杂度

@@ -1944,18 +1774,6 @@ $$ } ``` -=== "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)$ 对数阶与指数阶正好相反,后者反映“每轮增加到两倍的情况”,而前者反映“每轮缩减到一半的情况”。对数阶仅次于常数阶,时间增长得很慢,是理想的时间复杂度。 @@ -2091,21 +1909,6 @@ $$ } ``` -=== "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. 对数阶的时间复杂度

@@ -2206,18 +2009,6 @@ $$ } ``` -=== "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)$ 。 @@ -2360,22 +2151,6 @@ $$ } ``` -=== "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. 线性对数阶的时间复杂度

@@ -2528,23 +2303,6 @@ $$ } ``` -=== "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. 阶乘阶的时间复杂度

@@ -2932,40 +2690,6 @@ $$ } ``` -=== "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 我们在实际应用中很少使用「最佳时间复杂度」,因为往往只有很小概率下才能达到,会带来一定的误导性。反之,「最差时间复杂度」最为实用,因为它给出了一个“效率安全值”,让我们可以放心地使用算法。 diff --git a/docs/chapter_preface/installation.md b/docs/chapter_preface/installation.md index a21ac247d..c6c1b2ee9 100644 --- a/docs/chapter_preface/installation.md +++ b/docs/chapter_preface/installation.md @@ -46,8 +46,3 @@ comments: true 1. 下载并安装 [Swift](https://www.swift.org/download/); 2. 在 VSCode 的插件市场中搜索 `swift`,安装 [Swift for Visual Studio Code](https://marketplace.visualstudio.com/items?itemName=sswg.swift-lang)。 - -## Rust 环境 - -1. 下载并安装 [Rust](https://www.rust-lang.org/tools/install); -2. 在 VSCode 的插件市场中搜索 `rust`,安装 [rust-analyzer](https://marketplace.visualstudio.com/items?itemName=rust-lang.rust-analyzer)。 \ No newline at end of file From 3aa4cc729db548f9b48f5389ad0aa02fe65983c6 Mon Sep 17 00:00:00 2001 From: Yudong Jin Date: Wed, 11 Jan 2023 19:42:59 +0800 Subject: [PATCH 6/7] Update installation.md --- docs/chapter_preface/installation.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/docs/chapter_preface/installation.md b/docs/chapter_preface/installation.md index c6c1b2ee9..828365edf 100644 --- a/docs/chapter_preface/installation.md +++ b/docs/chapter_preface/installation.md @@ -46,3 +46,8 @@ comments: true 1. 下载并安装 [Swift](https://www.swift.org/download/); 2. 在 VSCode 的插件市场中搜索 `swift`,安装 [Swift for Visual Studio Code](https://marketplace.visualstudio.com/items?itemName=sswg.swift-lang)。 + +## Rust 环境 + +1. 下载并安装 [Rust](https://www.rust-lang.org/tools/install); +2. 在 VSCode 的插件市场中搜索 `rust`,安装 [rust-analyzer](https://marketplace.visualstudio.com/items?itemName=rust-lang.rust-analyzer)。 From b0c4efa706896c866212910c9ff480c3693659bd Mon Sep 17 00:00:00 2001 From: Yudong Jin Date: Wed, 11 Jan 2023 19:43:49 +0800 Subject: [PATCH 7/7] Update time_complexity.md --- docs/chapter_computational_complexity/time_complexity.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/docs/chapter_computational_complexity/time_complexity.md b/docs/chapter_computational_complexity/time_complexity.md index 92f5a5193..09aee7b79 100644 --- a/docs/chapter_computational_complexity/time_complexity.md +++ b/docs/chapter_computational_complexity/time_complexity.md @@ -355,8 +355,8 @@ $$ print(0) } } - ``` + ![time_complexity_first_example](time_complexity.assets/time_complexity_first_example.png)

Fig. 算法 A, B, C 的时间增长趋势

@@ -725,7 +725,6 @@ $$ } ``` - ### 2. 判断渐近上界 **时间复杂度由多项式 $T(n)$ 中最高阶的项来决定**。这是因为在 $n$ 趋于无穷大时,最高阶的项将处于主导作用,其它项的影响都可以被忽略。