|
|
|
@ -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}
|
|
|
|
|
|
|
|
|
|
[class]{}-[func]{find_one}
|
|
|
|
|
### 生成一个数组,元素为: 1, 2, ..., n ,顺序被打乱 ###
|
|
|
|
|
def random_numbers(n)
|
|
|
|
|
# 生成数组 nums =: 1, 2, 3, ..., n
|
|
|
|
|
nums = Array.new(n) { |i| i + 1 }
|
|
|
|
|
# 随机打乱数组元素
|
|
|
|
|
nums.shuffle!
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
### 查找数组 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"
|
|
|
|
|