diff --git a/docs/chapter_array_and_linkedlist/array.md b/docs/chapter_array_and_linkedlist/array.md index 0aa056c2b..cb7035b04 100755 --- a/docs/chapter_array_and_linkedlist/array.md +++ b/docs/chapter_array_and_linkedlist/array.md @@ -314,7 +314,7 @@ comments: true ### 随机访问元素 ### def random_access(nums) # 在区间 [0, nums.length) 中随机抽取一个数字 - random_index = Random.rand 0...(nums.length - 1) + random_index = Random.rand(0...nums.length) # 获取并返回随机元素 nums[random_index] diff --git a/docs/chapter_array_and_linkedlist/linked_list.md b/docs/chapter_array_and_linkedlist/linked_list.md index e53bb5988..839cf9114 100755 --- a/docs/chapter_array_and_linkedlist/linked_list.md +++ b/docs/chapter_array_and_linkedlist/linked_list.md @@ -427,11 +427,11 @@ comments: true ```ruby title="linked_list.rb" # 初始化链表 1 -> 3 -> 2 -> 5 -> 4 # 初始化各个节点 - n0 = ListNode.new 1 - n1 = ListNode.new 3 - n2 = ListNode.new 2 - n3 = ListNode.new 5 - n4 = ListNode.new 4 + n0 = ListNode.new(1) + n1 = ListNode.new(3) + n2 = ListNode.new(2) + n3 = ListNode.new(5) + n4 = ListNode.new(4) # 构建节点之间的引用 n0.next = n1 n1.next = n2 diff --git a/docs/chapter_array_and_linkedlist/list.md b/docs/chapter_array_and_linkedlist/list.md index 9612c17be..2f3fc7982 100755 --- a/docs/chapter_array_and_linkedlist/list.md +++ b/docs/chapter_array_and_linkedlist/list.md @@ -551,10 +551,10 @@ comments: true nums << 4 # 在中间插入元素 - nums.insert 3, 6 # 在索引 3 处插入数字 6 + nums.insert(3, 6) # 在索引 3 处插入数字 6 # 删除元素 - nums.delete_at 3 # 删除索引 3 处的元素 + nums.delete_at(3) # 删除索引 3 处的元素 ``` === "Zig" @@ -2288,7 +2288,7 @@ comments: true @capacity = 10 @size = 0 @extend_ratio = 2 - @arr = Array.new capacity + @arr = Array.new(capacity) end ### 访问元素 ### @@ -2360,9 +2360,9 @@ comments: true def to_array sz = size # 仅转换有效长度范围内的列表元素 - arr = Array.new sz + arr = Array.new(sz) for i in 0...sz - arr[i] = get i + arr[i] = get(i) end arr end diff --git a/docs/chapter_computational_complexity/iteration_and_recursion.md b/docs/chapter_computational_complexity/iteration_and_recursion.md index 5fe60d7a1..7430d6755 100644 --- a/docs/chapter_computational_complexity/iteration_and_recursion.md +++ b/docs/chapter_computational_complexity/iteration_and_recursion.md @@ -185,7 +185,17 @@ comments: true === "Ruby" ```ruby title="iteration.rb" - [class]{}-[func]{for_loop} + ### for 循环 ### + def for_loop(n) + res = 0 + + # 循环求和 1, 2, ..., n-1, n + for i in 1..n + res += i + end + + res + end ``` === "Zig" @@ -417,7 +427,19 @@ comments: true === "Ruby" ```ruby title="iteration.rb" - [class]{}-[func]{while_loop} + ### while 循环 ### + def while_loop(n) + res = 0 + i = 1 # 初始化条件变量 + + # 循环求和 1, 2, ..., n-1, n + while i <= n + res += i + i += 1 # 更新条件变量 + end + + res + end ``` === "Zig" @@ -664,7 +686,21 @@ comments: true === "Ruby" ```ruby title="iteration.rb" - [class]{}-[func]{while_loop_ii} + ### while 循环(两次更新)### + def while_loop_ii(n) + res = 0 + i = 1 # 初始化条件变量 + + # 循环求和 1, 4, 10, ... + while i <= n + res += i + # 更新条件变量 + i += 1 + i *= 2 + end + + res + end ``` === "Zig" @@ -904,7 +940,20 @@ comments: true === "Ruby" ```ruby title="iteration.rb" - [class]{}-[func]{nested_for_loop} + ### 双层 for 循环 ### + def nested_for_loop(n) + res = "" + + # 循环 i = 1, 2, ..., n-1, n + for i in 1..n + # 循环 j = 1, 2, ..., n-1, n + for j in 1..n + res += "(#{i}, #{j}), " + end + end + + res + end ``` === "Zig" @@ -1139,7 +1188,15 @@ comments: true === "Ruby" ```ruby title="recursion.rb" - [class]{}-[func]{recur} + ### 递归 ### + def recur(n) + # 终止条件 + return 1 if n == 1 + # 递:递归调用 + res = recur(n - 1) + # 归:返回结果 + n + res + end ``` === "Zig" @@ -1362,7 +1419,13 @@ comments: true === "Ruby" ```ruby title="recursion.rb" - [class]{}-[func]{tail_recur} + ### 尾递归 ### + def tail_recur(n, res) + # 终止条件 + return res if n == 0 + # 尾递归调用 + tail_recur(n - 1, res + n) + end ``` === "Zig" @@ -1594,7 +1657,15 @@ comments: true === "Ruby" ```ruby title="recursion.rb" - [class]{}-[func]{fib} + ### 斐波那契数列:递归 ### + def fib(n) + # 终止条件 f(1) = 0, f(2) = 1 + return n - 1 if n == 1 || n == 2 + # 递归调用 f(n) = f(n-1) + f(n-2) + res = fib(n - 1) + fib(n - 2) + # 返回结果 f(n) + res + end ``` === "Zig" @@ -1936,7 +2007,25 @@ comments: true === "Ruby" ```ruby title="recursion.rb" - [class]{}-[func]{for_loop_recur} + ### 使用迭代模拟递归 ### + def for_loop_recur(n) + # 使用一个显式的栈来模拟系统调用栈 + stack = [] + res = 0 + + # 递:递归调用 + for i in n.downto(0) + # 通过“入栈操作”模拟“递” + stack << i + end + # 归:返回结果 + while !stack.empty? + res += stack.pop + end + + # res = 1+2+3+...+n + res + end ``` === "Zig" diff --git a/docs/chapter_computational_complexity/space_complexity.md b/docs/chapter_computational_complexity/space_complexity.md index 8e7da085b..6929ebf51 100755 --- a/docs/chapter_computational_complexity/space_complexity.md +++ b/docs/chapter_computational_complexity/space_complexity.md @@ -341,7 +341,30 @@ comments: true === "Ruby" ```ruby title="" + ### 类 ### + class Node + attr_accessor :val # 节点值 + attr_accessor :next # 指向下一节点的引用 + def initialize(x) + @val = x + end + end + + ### 函数 ### + def function + # 执行某些操作... + 0 + end + + ### 算法 ### + def algorithm(n) # 输入数据 + a = 0 # 暂存数据(常量) + b = 0 # 暂存数据(变量) + node = Node.new(0) # 暂存数据(对象) + c = function # 栈帧空间(调用函数) + a + b + c # 输出数据 + end ``` === "Zig" @@ -505,7 +528,11 @@ comments: true === "Ruby" ```ruby title="" - + def algorithm(n) + a = 0 # O(1) + b = Array.new(10000) # O(1) + nums = Array.new(n) if n > 10 # O(n) + end ``` === "Zig" @@ -542,13 +569,13 @@ comments: true // 执行某些操作 return 0; } - /* 循环 O(1) */ + /* 循环的空间复杂度为 O(1) */ void loop(int n) { for (int i = 0; i < n; i++) { func(); } } - /* 递归 O(n) */ + /* 递归的空间复杂度为 O(n) */ void recur(int n) { if (n == 1) return; return recur(n - 1); @@ -562,13 +589,13 @@ comments: true // 执行某些操作 return 0; } - /* 循环 O(1) */ + /* 循环的空间复杂度为 O(1) */ void loop(int n) { for (int i = 0; i < n; i++) { function(); } } - /* 递归 O(n) */ + /* 递归的空间复杂度为 O(n) */ void recur(int n) { if (n == 1) return; return recur(n - 1); @@ -582,13 +609,13 @@ comments: true // 执行某些操作 return 0; } - /* 循环 O(1) */ + /* 循环的空间复杂度为 O(1) */ void Loop(int n) { for (int i = 0; i < n; i++) { Function(); } } - /* 递归 O(n) */ + /* 递归的空间复杂度为 O(n) */ int Recur(int n) { if (n == 1) return 1; return Recur(n - 1); @@ -603,14 +630,14 @@ comments: true return 0 } - /* 循环 O(1) */ + /* 循环的空间复杂度为 O(1) */ func loop(n int) { for i := 0; i < n; i++ { function() } } - /* 递归 O(n) */ + /* 递归的空间复杂度为 O(n) */ func recur(n int) { if n == 1 { return @@ -628,14 +655,14 @@ comments: true return 0 } - /* 循环 O(1) */ + /* 循环的空间复杂度为 O(1) */ func loop(n: Int) { for _ in 0 ..< n { function() } } - /* 递归 O(n) */ + /* 递归的空间复杂度为 O(n) */ func recur(n: Int) { if n == 1 { return @@ -651,13 +678,13 @@ comments: true // 执行某些操作 return 0; } - /* 循环 O(1) */ + /* 循环的空间复杂度为 O(1) */ function loop(n) { for (let i = 0; i < n; i++) { constFunc(); } } - /* 递归 O(n) */ + /* 递归的空间复杂度为 O(n) */ function recur(n) { if (n === 1) return; return recur(n - 1); @@ -671,13 +698,13 @@ comments: true // 执行某些操作 return 0; } - /* 循环 O(1) */ + /* 循环的空间复杂度为 O(1) */ function loop(n: number): void { for (let i = 0; i < n; i++) { constFunc(); } } - /* 递归 O(n) */ + /* 递归的空间复杂度为 O(n) */ function recur(n: number): void { if (n === 1) return; return recur(n - 1); @@ -691,13 +718,13 @@ comments: true // 执行某些操作 return 0; } - /* 循环 O(1) */ + /* 循环的空间复杂度为 O(1) */ void loop(int n) { for (int i = 0; i < n; i++) { function(); } } - /* 递归 O(n) */ + /* 递归的空间复杂度为 O(n) */ void recur(int n) { if (n == 1) return; return recur(n - 1); @@ -711,13 +738,13 @@ comments: true // 执行某些操作 return 0; } - /* 循环 O(1) */ + /* 循环的空间复杂度为 O(1) */ fn loop(n: i32) { for i in 0..n { function(); } } - /* 递归 O(n) */ + /* 递归的空间复杂度为 O(n) */ fn recur(n: i32) { if n == 1 { return; @@ -733,13 +760,13 @@ comments: true // 执行某些操作 return 0; } - /* 循环 O(1) */ + /* 循环的空间复杂度为 O(1) */ void loop(int n) { for (int i = 0; i < n; i++) { func(); } } - /* 递归 O(n) */ + /* 递归的空间复杂度为 O(n) */ void recur(int n) { if (n == 1) return; return recur(n - 1); @@ -753,13 +780,13 @@ comments: true // 执行某些操作 return 0 } - /* 循环 O(1) */ + /* 循环的空间复杂度为 O(1) */ fun loop(n: Int) { for (i in 0.. 0 + + # 数组 nums 长度为 n, n-1, ..., 2, 1 + nums = Array.new(n, 0) + quadratic_recur(n - 1) + end ``` === "Zig" @@ -2253,7 +2335,15 @@ $$ === "Ruby" ```ruby title="space_complexity.rb" - [class]{}-[func]{build_tree} + ### 指数阶(建立满二叉树)### + def build_tree(n) + return if n == 0 + + TreeNode.new.tap do |root| + root.left = build_tree(n - 1) + root.right = build_tree(n - 1) + end + end ``` === "Zig" diff --git a/docs/chapter_computational_complexity/time_complexity.md b/docs/chapter_computational_complexity/time_complexity.md index ff56b9cf0..d22cf5ac2 100755 --- a/docs/chapter_computational_complexity/time_complexity.md +++ b/docs/chapter_computational_complexity/time_complexity.md @@ -193,7 +193,16 @@ comments: true === "Ruby" ```ruby title="" - + # 在某运行平台下 + def algorithm(n) + a = 2 # 1 ns + a = a + 1 # 1 ns + a = a * 2 # 10 ns + # 循环 n 次 + (n...0).each do # 1 ns + puts 0 # 5 ns + end + end ``` === "Zig" @@ -478,7 +487,20 @@ $$ === "Ruby" ```ruby title="" + # 算法 A 的时间复杂度:常数阶 + def algorithm_A(n) + puts 0 + end + # 算法 B 的时间复杂度:线性阶 + def algorithm_B(n) + (0...n).each { puts 0 } + end + + # 算法 C 的时间复杂度:常数阶 + def algorithm_C(n) + (0...1_000_000).each { puts 0 } + end ``` === "Zig" @@ -694,7 +716,15 @@ $$ === "Ruby" ```ruby title="" - + def algorithm(n) + a = 1 # +1 + a = a + 1 # +1 + a = a * 2 # +1 + # 循环 n 次 + (0...n).each do # +1 + puts 0 # +1 + end + end ``` === "Zig" @@ -978,7 +1008,16 @@ $T(n)$ 是一次函数,说明其运行时间的增长趋势是线性的,因 === "Ruby" ```ruby title="" - + def algorithm(n) + a = 1 # +0(技巧 1) + a = a + n # +0(技巧 1) + # +n(技巧 2) + (0...(5 * n + 1)).each do { puts 0 } + # +n*n(技巧 3) + (0...(2 * n)).each do + (0...(n + 1)).each do { puts 0 } + end + end ``` === "Zig" @@ -1216,7 +1255,15 @@ $$ === "Ruby" ```ruby title="time_complexity.rb" - [class]{}-[func]{constant} + ### 常数阶 ### + def constant(n) + count = 0 + size = 100000 + + (0...size).each { count += 1 } + + count + end ``` === "Zig" @@ -1394,7 +1441,12 @@ $$ === "Ruby" ```ruby title="time_complexity.rb" - [class]{}-[func]{linear} + ### 线性阶 ### + def linear(n) + count = 0 + (0...n).each { count += 1 } + count + end ``` === "Zig" @@ -1587,7 +1639,17 @@ $$ === "Ruby" ```ruby title="time_complexity.rb" - [class]{}-[func]{array_traversal} + ### 线性阶(遍历数组)### + def array_traversal(nums) + count = 0 + + # 循环次数与数组长度成正比 + for num in nums + count += 1 + end + + count + end ``` === "Zig" @@ -1807,7 +1869,19 @@ $$ === "Ruby" ```ruby title="time_complexity.rb" - [class]{}-[func]{quadratic} + ### 平方阶 ### + def quadratic(n) + count = 0 + + # 循环次数与数据大小 n 成平方关系 + for i in 0...n + for j in 0...n + count += 1 + end + end + + count + end ``` === "Zig" @@ -2113,7 +2187,26 @@ $$ === "Ruby" ```ruby title="time_complexity.rb" - [class]{}-[func]{bubble_sort} + ### 平方阶(冒泡排序)### + def bubble_sort(nums) + count = 0 # 计数器 + + # 外循环:未排序区间为 [0, i] + for i in (nums.length - 1).downto(0) + # 内循环:将未排序区间 [0, i] 中的最大元素交换至该区间的最右端 + for j in 0...i + if nums[j] > nums[j + 1] + # 交换 nums[j] 与 nums[j + 1] + tmp = nums[j] + nums[j] = nums[j + 1] + nums[j + 1] = tmp + count += 3 # 元素交换包含 3 个单元操作 + end + end + end + + count + end ``` === "Zig" @@ -2375,7 +2468,19 @@ $$ === "Ruby" ```ruby title="time_complexity.rb" - [class]{}-[func]{exponential} + ### 指数阶(循环实现)### + def exponential(n) + count, base = 0, 1 + + # 细胞每轮一分为二,形成数列 1, 2, 4, 8, ..., 2^(n-1) + (0...n).each do + (0...base).each { count += 1 } + base *= 2 + end + + # count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1 + count + end ``` === "Zig" @@ -2544,7 +2649,11 @@ $$ === "Ruby" ```ruby title="time_complexity.rb" - [class]{}-[func]{exp_recur} + ### 指数阶(递归实现)### + def exp_recur(n) + return 1 if n == 1 + exp_recur(n - 1) + exp_recur(n - 1) + 1 + end ``` === "Zig" @@ -2741,7 +2850,17 @@ $$ === "Ruby" ```ruby title="time_complexity.rb" - [class]{}-[func]{logarithmic} + ### 对数阶(循环实现)### + def logarithmic(n) + count = 0 + + while n > 1 + n /= 2 + count += 1 + end + + count + end ``` === "Zig" @@ -2904,7 +3023,11 @@ $$ === "Ruby" ```ruby title="time_complexity.rb" - [class]{}-[func]{log_recur} + ### 对数阶(递归实现)### + def log_recur(n) + return 0 unless n > 1 + log_recur(n / 2) + 1 + end ``` === "Zig" @@ -3118,7 +3241,15 @@ $$ === "Ruby" ```ruby title="time_complexity.rb" - [class]{}-[func]{linear_log_recur} + ### 线性对数阶 ### + def linear_log_recur(n) + return 1 unless n > 1 + + count = linear_log_recur(n / 2) + linear_log_recur(n / 2) + (0...n).each { count += 1 } + + count + end ``` === "Zig" @@ -3350,7 +3481,16 @@ $$ === "Ruby" ```ruby title="time_complexity.rb" - [class]{}-[func]{factorial_recur} + ### 阶乘阶(递归实现)### + def factorial_recur(n) + return 1 if n == 0 + + count = 0 + # 从 1 个分裂出 n 个 + (0...n).each { count += factorial_recur(n - 1) } + + count + end ``` === "Zig" @@ -3745,9 +3885,24 @@ $$ === "Ruby" ```ruby title="worst_best_time_complexity.rb" - [class]{}-[func]{random_numbers} + ### 生成一个数组,元素为: 1, 2, ..., n ,顺序被打乱 ### + def random_numbers(n) + # 生成数组 nums =: 1, 2, 3, ..., n + nums = Array.new(n) { |i| i + 1 } + # 随机打乱数组元素 + nums.shuffle! + end - [class]{}-[func]{find_one} + ### 查找数组 nums 中数字 1 所在索引 ### + def find_one(nums) + for i in 0...nums.length + # 当元素 1 在数组头部时,达到最佳时间复杂度 O(1) + # 当元素 1 在数组尾部时,达到最差时间复杂度 O(n) + return i if nums[i] == 1 + end + + -1 + end ``` === "Zig" diff --git a/en/docs/chapter_array_and_linkedlist/array.md b/en/docs/chapter_array_and_linkedlist/array.md index 31544e4b2..7f7bf2647 100755 --- a/en/docs/chapter_array_and_linkedlist/array.md +++ b/en/docs/chapter_array_and_linkedlist/array.md @@ -299,7 +299,7 @@ Accessing elements in an array is highly efficient, allowing us to randomly acce ### 随机访问元素 ### def random_access(nums) # 在区间 [0, nums.length) 中随机抽取一个数字 - random_index = Random.rand 0...(nums.length - 1) + random_index = Random.rand(0...nums.length) # 获取并返回随机元素 nums[random_index] diff --git a/en/docs/chapter_array_and_linkedlist/list.md b/en/docs/chapter_array_and_linkedlist/list.md index b5a3d31aa..5b06dd370 100755 --- a/en/docs/chapter_array_and_linkedlist/list.md +++ b/en/docs/chapter_array_and_linkedlist/list.md @@ -2154,7 +2154,7 @@ To enhance our understanding of how lists work, we will attempt to implement a s @capacity = 10 @size = 0 @extend_ratio = 2 - @arr = Array.new capacity + @arr = Array.new(capacity) end ### 访问元素 ### @@ -2226,9 +2226,9 @@ To enhance our understanding of how lists work, we will attempt to implement a s def to_array sz = size # 仅转换有效长度范围内的列表元素 - arr = Array.new sz + arr = Array.new(sz) for i in 0...sz - arr[i] = get i + arr[i] = get(i) end arr end diff --git a/en/docs/chapter_computational_complexity/iteration_and_recursion.md b/en/docs/chapter_computational_complexity/iteration_and_recursion.md index 9a9ad351f..f4505974b 100644 --- a/en/docs/chapter_computational_complexity/iteration_and_recursion.md +++ b/en/docs/chapter_computational_complexity/iteration_and_recursion.md @@ -185,7 +185,17 @@ The following function uses a `for` loop to perform a summation of $1 + 2 + \dot === "Ruby" ```ruby title="iteration.rb" - [class]{}-[func]{for_loop} + ### for 循环 ### + def for_loop(n) + res = 0 + + # 循环求和 1, 2, ..., n-1, n + for i in 1..n + res += i + end + + res + end ``` === "Zig" @@ -417,7 +427,19 @@ Below we use a `while` loop to implement the sum $1 + 2 + \dots + n$. === "Ruby" ```ruby title="iteration.rb" - [class]{}-[func]{while_loop} + ### while 循环 ### + def while_loop(n) + res = 0 + i = 1 # 初始化条件变量 + + # 循环求和 1, 2, ..., n-1, n + while i <= n + res += i + i += 1 # 更新条件变量 + end + + res + end ``` === "Zig" @@ -664,7 +686,21 @@ For example, in the following code, the condition variable $i$ is updated twice === "Ruby" ```ruby title="iteration.rb" - [class]{}-[func]{while_loop_ii} + ### while 循环(两次更新)### + def while_loop_ii(n) + res = 0 + i = 1 # 初始化条件变量 + + # 循环求和 1, 4, 10, ... + while i <= n + res += i + # 更新条件变量 + i += 1 + i *= 2 + end + + res + end ``` === "Zig" @@ -904,7 +940,20 @@ We can nest one loop structure within another. Below is an example using `for` l === "Ruby" ```ruby title="iteration.rb" - [class]{}-[func]{nested_for_loop} + ### 双层 for 循环 ### + def nested_for_loop(n) + res = "" + + # 循环 i = 1, 2, ..., n-1, n + for i in 1..n + # 循环 j = 1, 2, ..., n-1, n + for j in 1..n + res += "(#{i}, #{j}), " + end + end + + res + end ``` === "Zig" @@ -1139,7 +1188,15 @@ Observe the following code, where simply calling the function `recur(n)` can com === "Ruby" ```ruby title="recursion.rb" - [class]{}-[func]{recur} + ### 递归 ### + def recur(n) + # 终止条件 + return 1 if n == 1 + # 递:递归调用 + res = recur(n - 1) + # 归:返回结果 + n + res + end ``` === "Zig" @@ -1362,7 +1419,13 @@ For example, in calculating $1 + 2 + \dots + n$, we can make the result variable === "Ruby" ```ruby title="recursion.rb" - [class]{}-[func]{tail_recur} + ### 尾递归 ### + def tail_recur(n, res) + # 终止条件 + return res if n == 0 + # 尾递归调用 + tail_recur(n - 1, res + n) + end ``` === "Zig" @@ -1594,7 +1657,15 @@ Using the recursive relation, and considering the first two numbers as terminati === "Ruby" ```ruby title="recursion.rb" - [class]{}-[func]{fib} + ### 斐波那契数列:递归 ### + def fib(n) + # 终止条件 f(1) = 0, f(2) = 1 + return n - 1 if n == 1 || n == 2 + # 递归调用 f(n) = f(n-1) + f(n-2) + res = fib(n - 1) + fib(n - 2) + # 返回结果 f(n) + res + end ``` === "Zig" @@ -1936,7 +2007,25 @@ Therefore, **we can use an explicit stack to simulate the behavior of the call s === "Ruby" ```ruby title="recursion.rb" - [class]{}-[func]{for_loop_recur} + ### 使用迭代模拟递归 ### + def for_loop_recur(n) + # 使用一个显式的栈来模拟系统调用栈 + stack = [] + res = 0 + + # 递:递归调用 + for i in n.downto(0) + # 通过“入栈操作”模拟“递” + stack << i + end + # 归:返回结果 + while !stack.empty? + res += stack.pop + end + + # res = 1+2+3+...+n + res + end ``` === "Zig" diff --git a/en/docs/chapter_computational_complexity/space_complexity.md b/en/docs/chapter_computational_complexity/space_complexity.md index 31349d845..8368bbfe5 100644 --- a/en/docs/chapter_computational_complexity/space_complexity.md +++ b/en/docs/chapter_computational_complexity/space_complexity.md @@ -1080,9 +1080,24 @@ Note that memory occupied by initializing variables or calling functions in a lo === "Ruby" ```ruby title="space_complexity.rb" - [class]{}-[func]{function} + ### 函数 ### + def function + # 执行某些操作 + 0 + end - [class]{}-[func]{constant} + ### 常数阶 ### + def constant(n) + # 常量、变量、对象占用 O(1) 空间 + a = 0 + nums = [0] * 10000 + node = ListNode.new + + # 循环中的变量占用 O(1) 空间 + (0...n).each { c = 0 } + # 循环中的函数占用 O(1) 空间 + (0...n).each { function } + end ``` === "Zig" @@ -1384,7 +1399,17 @@ Linear order is common in arrays, linked lists, stacks, queues, etc., where the === "Ruby" ```ruby title="space_complexity.rb" - [class]{}-[func]{linear} + ### 线性阶 ### + def linear(n) + # 长度为 n 的列表占用 O(n) 空间 + nums = Array.new(n, 0) + + # 长度为 n 的哈希表占用 O(n) 空间 + hmap = {} + for i in 0...n + hmap[i] = i.to_s + end + end ``` === "Zig" @@ -1566,7 +1591,12 @@ As shown below, this function's recursive depth is $n$, meaning there are $n$ in === "Ruby" ```ruby title="space_complexity.rb" - [class]{}-[func]{linear_recur} + ### 线性阶(递归实现)### + def linear_recur(n) + puts "递归 n = #{n}" + return if n == 1 + linear_recur(n - 1) + end ``` === "Zig" @@ -1806,7 +1836,11 @@ Quadratic order is common in matrices and graphs, where the number of elements i === "Ruby" ```ruby title="space_complexity.rb" - [class]{}-[func]{quadratic} + ### 平方阶 ### + def quadratic(n) + # 二维列表占用 O(n^2) 空间 + Array.new(n) { Array.new(n, 0) } + end ``` === "Zig" @@ -2001,7 +2035,14 @@ As shown below, the recursive depth of this function is $n$, and in each recursi === "Ruby" ```ruby title="space_complexity.rb" - [class]{}-[func]{quadratic_recur} + ### 平方阶(递归实现)### + def quadratic_recur(n) + return 0 unless n > 0 + + # 数组 nums 长度为 n, n-1, ..., 2, 1 + nums = Array.new(n, 0) + quadratic_recur(n - 1) + end ``` === "Zig" @@ -2199,7 +2240,15 @@ Exponential order is common in binary trees. Observe the below image, a "full bi === "Ruby" ```ruby title="space_complexity.rb" - [class]{}-[func]{build_tree} + ### 指数阶(建立满二叉树)### + def build_tree(n) + return if n == 0 + + TreeNode.new.tap do |root| + root.left = build_tree(n - 1) + root.right = build_tree(n - 1) + end + end ``` === "Zig" diff --git a/en/docs/chapter_computational_complexity/time_complexity.md b/en/docs/chapter_computational_complexity/time_complexity.md index 934dff132..95065b088 100644 --- a/en/docs/chapter_computational_complexity/time_complexity.md +++ b/en/docs/chapter_computational_complexity/time_complexity.md @@ -1143,7 +1143,15 @@ Constant order means the number of operations is independent of the input data s === "Ruby" ```ruby title="time_complexity.rb" - [class]{}-[func]{constant} + ### 常数阶 ### + def constant(n) + count = 0 + size = 100000 + + (0...size).each { count += 1 } + + count + end ``` === "Zig" @@ -1321,7 +1329,12 @@ Linear order indicates the number of operations grows linearly with the input da === "Ruby" ```ruby title="time_complexity.rb" - [class]{}-[func]{linear} + ### 线性阶 ### + def linear(n) + count = 0 + (0...n).each { count += 1 } + count + end ``` === "Zig" @@ -1514,7 +1527,17 @@ Operations like array traversal and linked list traversal have a time complexity === "Ruby" ```ruby title="time_complexity.rb" - [class]{}-[func]{array_traversal} + ### 线性阶(遍历数组)### + def array_traversal(nums) + count = 0 + + # 循环次数与数组长度成正比 + for num in nums + count += 1 + end + + count + end ``` === "Zig" @@ -1734,7 +1757,19 @@ Quadratic order means the number of operations grows quadratically with the inpu === "Ruby" ```ruby title="time_complexity.rb" - [class]{}-[func]{quadratic} + ### 平方阶 ### + def quadratic(n) + count = 0 + + # 循环次数与数据大小 n 成平方关系 + for i in 0...n + for j in 0...n + count += 1 + end + end + + count + end ``` === "Zig" @@ -2040,7 +2075,26 @@ For instance, in bubble sort, the outer loop runs $n - 1$ times, and the inner l === "Ruby" ```ruby title="time_complexity.rb" - [class]{}-[func]{bubble_sort} + ### 平方阶(冒泡排序)### + def bubble_sort(nums) + count = 0 # 计数器 + + # 外循环:未排序区间为 [0, i] + for i in (nums.length - 1).downto(0) + # 内循环:将未排序区间 [0, i] 中的最大元素交换至该区间的最右端 + for j in 0...i + if nums[j] > nums[j + 1] + # 交换 nums[j] 与 nums[j + 1] + tmp = nums[j] + nums[j] = nums[j + 1] + nums[j + 1] = tmp + count += 3 # 元素交换包含 3 个单元操作 + end + end + end + + count + end ``` === "Zig" @@ -2302,7 +2356,19 @@ The following image and code simulate the cell division process, with a time com === "Ruby" ```ruby title="time_complexity.rb" - [class]{}-[func]{exponential} + ### 指数阶(循环实现)### + def exponential(n) + count, base = 0, 1 + + # 细胞每轮一分为二,形成数列 1, 2, 4, 8, ..., 2^(n-1) + (0...n).each do + (0...base).each { count += 1 } + base *= 2 + end + + # count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1 + count + end ``` === "Zig" @@ -2471,7 +2537,11 @@ In practice, exponential order often appears in recursive functions. For example === "Ruby" ```ruby title="time_complexity.rb" - [class]{}-[func]{exp_recur} + ### 指数阶(递归实现)### + def exp_recur(n) + return 1 if n == 1 + exp_recur(n - 1) + exp_recur(n - 1) + 1 + end ``` === "Zig" @@ -2668,7 +2738,17 @@ The following image and code simulate the "halving each round" process, with a t === "Ruby" ```ruby title="time_complexity.rb" - [class]{}-[func]{logarithmic} + ### 对数阶(循环实现)### + def logarithmic(n) + count = 0 + + while n > 1 + n /= 2 + count += 1 + end + + count + end ``` === "Zig" @@ -2831,7 +2911,11 @@ Like exponential order, logarithmic order also frequently appears in recursive f === "Ruby" ```ruby title="time_complexity.rb" - [class]{}-[func]{log_recur} + ### 对数阶(递归实现)### + def log_recur(n) + return 0 unless n > 1 + log_recur(n / 2) + 1 + end ``` === "Zig" @@ -3045,7 +3129,15 @@ Linear-logarithmic order often appears in nested loops, with the complexities of === "Ruby" ```ruby title="time_complexity.rb" - [class]{}-[func]{linear_log_recur} + ### 线性对数阶 ### + def linear_log_recur(n) + return 1 unless n > 1 + + count = linear_log_recur(n / 2) + linear_log_recur(n / 2) + (0...n).each { count += 1 } + + count + end ``` === "Zig" @@ -3277,7 +3369,16 @@ Factorials are typically implemented using recursion. As shown in the image and === "Ruby" ```ruby title="time_complexity.rb" - [class]{}-[func]{factorial_recur} + ### 阶乘阶(递归实现)### + def factorial_recur(n) + return 1 if n == 0 + + count = 0 + # 从 1 个分裂出 n 个 + (0...n).each { count += factorial_recur(n - 1) } + + count + end ``` === "Zig" @@ -3672,9 +3773,24 @@ The "worst-case time complexity" corresponds to the asymptotic upper bound, deno === "Ruby" ```ruby title="worst_best_time_complexity.rb" - [class]{}-[func]{random_numbers} + ### 生成一个数组,元素为: 1, 2, ..., n ,顺序被打乱 ### + def random_numbers(n) + # 生成数组 nums =: 1, 2, 3, ..., n + nums = Array.new(n) { |i| i + 1 } + # 随机打乱数组元素 + nums.shuffle! + end - [class]{}-[func]{find_one} + ### 查找数组 nums 中数字 1 所在索引 ### + def find_one(nums) + for i in 0...nums.length + # 当元素 1 在数组头部时,达到最佳时间复杂度 O(1) + # 当元素 1 在数组尾部时,达到最差时间复杂度 O(n) + return i if nums[i] == 1 + end + + -1 + end ``` === "Zig"