mirror of
https://github.com/krahets/hello-algo.git
synced 2025-07-05 13:15:30 +08:00
build
This commit is contained in:
@ -72,7 +72,7 @@ $$
|
||||
|
||||
并行优化在多核或多处理器的环境中尤其有效,因为系统可以同时处理多个子问题,更加充分地利用计算资源,从而显著减少总体的运行时间。
|
||||
|
||||
比如在图 12-3 所示的“桶排序”中,我们将海量的数据平均分配到各个桶中,则可所有桶的排序任务分散到各个计算单元,完成后再合并结果。
|
||||
比如在图 12-3 所示的“桶排序”中,我们将海量的数据平均分配到各个桶中,则可将所有桶的排序任务分散到各个计算单元,完成后再合并结果。
|
||||
|
||||
{ class="animation-figure" }
|
||||
|
||||
|
@ -431,7 +431,7 @@ comments: true
|
||||
|
||||
## 8.1.2 堆的实现
|
||||
|
||||
下文实现的是大顶堆。若要将其转换为小顶堆,只需将所有大小逻辑判断取逆(例如,将 $\geq$ 替换为 $\leq$ )。感兴趣的读者可以自行实现。
|
||||
下文实现的是大顶堆。若要将其转换为小顶堆,只需将所有大小逻辑判断进行逆转(例如,将 $\geq$ 替换为 $\leq$ )。感兴趣的读者可以自行实现。
|
||||
|
||||
### 1. 堆的存储与表示
|
||||
|
||||
|
@ -870,7 +870,7 @@ $$
|
||||
|
||||
## 11.9.3 算法特性
|
||||
|
||||
- **时间复杂度为 $O(n + m)$** :涉及遍历 `nums` 和遍历 `counter` ,都使用线性时间。一般情况下 $n \gg m$ ,时间复杂度趋于 $O(n)$ 。
|
||||
- **时间复杂度为 $O(n + m)$、非自适应排序** :涉及遍历 `nums` 和遍历 `counter` ,都使用线性时间。一般情况下 $n \gg m$ ,时间复杂度趋于 $O(n)$ 。
|
||||
- **空间复杂度为 $O(n + m)$、非原地排序**:借助了长度分别为 $n$ 和 $m$ 的数组 `res` 和 `counter` 。
|
||||
- **稳定排序**:由于向 `res` 中填充元素的顺序是“从右向左”的,因此倒序遍历 `nums` 可以避免改变相等元素之间的相对位置,从而实现稳定排序。实际上,正序遍历 `nums` 也可以得到正确的排序结果,但结果是非稳定的。
|
||||
|
||||
|
@ -761,6 +761,6 @@ $$
|
||||
|
||||
相较于计数排序,基数排序适用于数值范围较大的情况,**但前提是数据必须可以表示为固定位数的格式,且位数不能过大**。例如,浮点数不适合使用基数排序,因为其位数 $k$ 过大,可能导致时间复杂度 $O(nk) \gg O(n^2)$ 。
|
||||
|
||||
- **时间复杂度为 $O(nk)$**:设数据量为 $n$、数据为 $d$ 进制、最大位数为 $k$ ,则对某一位执行计数排序使用 $O(n + d)$ 时间,排序所有 $k$ 位使用 $O((n + d)k)$ 时间。通常情况下,$d$ 和 $k$ 都相对较小,时间复杂度趋向 $O(n)$ 。
|
||||
- **时间复杂度为 $O(nk)$、非自适应排序**:设数据量为 $n$、数据为 $d$ 进制、最大位数为 $k$ ,则对某一位执行计数排序使用 $O(n + d)$ 时间,排序所有 $k$ 位使用 $O((n + d)k)$ 时间。通常情况下,$d$ 和 $k$ 都相对较小,时间复杂度趋向 $O(n)$ 。
|
||||
- **空间复杂度为 $O(n + d)$、非原地排序**:与计数排序相同,基数排序需要借助长度为 $n$ 和 $d$ 的数组 `res` 和 `counter` 。
|
||||
- **稳定排序**:当计数排序稳定时,基数排序也稳定;当计数排序不稳定时,基数排序无法保证得到正确的排序结果。
|
||||
|
@ -131,7 +131,9 @@ comments: true
|
||||
=== "Ruby"
|
||||
|
||||
```ruby title=""
|
||||
|
||||
### 二叉树的数组表示 ###
|
||||
# 使用 nil 来表示空位
|
||||
tree = [1, 2, 3, 4, nil, 6, 7, 8, 9, nil, nil, 12, nil, nil, 15]
|
||||
```
|
||||
|
||||
=== "Zig"
|
||||
|
@ -137,9 +137,9 @@ AVL 树既是二叉搜索树,也是平衡二叉树,同时满足这两类二
|
||||
right: TreeNode | null; // 右子节点指针
|
||||
constructor(val?: number, height?: number, left?: TreeNode | null, right?: TreeNode | null) {
|
||||
this.val = val === undefined ? 0 : val;
|
||||
this.height = height === undefined ? 0 : height;
|
||||
this.left = left === undefined ? null : left;
|
||||
this.right = right === undefined ? null : right;
|
||||
this.height = height === undefined ? 0 : height;
|
||||
this.left = left === undefined ? null : left;
|
||||
this.right = right === undefined ? null : right;
|
||||
}
|
||||
}
|
||||
```
|
||||
@ -222,7 +222,18 @@ AVL 树既是二叉搜索树,也是平衡二叉树,同时满足这两类二
|
||||
=== "Ruby"
|
||||
|
||||
```ruby title=""
|
||||
### AVL 树节点类 ###
|
||||
class TreeNode
|
||||
attr_accessor :val # 节点值
|
||||
attr_accessor :height # 节点高度
|
||||
attr_accessor :left # 左子节点引用
|
||||
attr_accessor :right # 右子节点引用
|
||||
|
||||
def initialize(val)
|
||||
@val = val
|
||||
@height = 0
|
||||
end
|
||||
end
|
||||
```
|
||||
|
||||
=== "Zig"
|
||||
|
@ -110,7 +110,7 @@ comments: true
|
||||
val: number;
|
||||
left: TreeNode | null;
|
||||
right: TreeNode | null;
|
||||
|
||||
|
||||
constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
|
||||
this.val = val === undefined ? 0 : val; // 节点值
|
||||
this.left = left === undefined ? null : left; // 左子节点引用
|
||||
@ -193,7 +193,16 @@ comments: true
|
||||
=== "Ruby"
|
||||
|
||||
```ruby title=""
|
||||
### 二叉树节点类 ###
|
||||
class TreeNode
|
||||
attr_accessor :val # 节点值
|
||||
attr_accessor :left # 左子节点引用
|
||||
attr_accessor :right # 右子节点引用
|
||||
|
||||
def initialize(val)
|
||||
@val = val
|
||||
end
|
||||
end
|
||||
```
|
||||
|
||||
=== "Zig"
|
||||
@ -440,7 +449,18 @@ comments: true
|
||||
=== "Ruby"
|
||||
|
||||
```ruby title="binary_tree.rb"
|
||||
|
||||
# 初始化二叉树
|
||||
# 初始化节点
|
||||
n1 = TreeNode.new(1)
|
||||
n2 = TreeNode.new(2)
|
||||
n3 = TreeNode.new(3)
|
||||
n4 = TreeNode.new(4)
|
||||
n5 = TreeNode.new(5)
|
||||
# 构建节点之间的引用(指针)
|
||||
n1.left = n2
|
||||
n1.right = n3
|
||||
n2.left = n4
|
||||
n2.right = n5
|
||||
```
|
||||
|
||||
=== "Zig"
|
||||
@ -605,7 +625,13 @@ comments: true
|
||||
=== "Ruby"
|
||||
|
||||
```ruby title="binary_tree.rb"
|
||||
|
||||
# 插入与删除节点
|
||||
_p = TreeNode.new(0)
|
||||
# 在 n1 -> n2 中间插入节点 _p
|
||||
n1.left = _p
|
||||
_p.left = n2
|
||||
# 删除节点
|
||||
n1.left = n2
|
||||
```
|
||||
|
||||
=== "Zig"
|
||||
|
Reference in New Issue
Block a user