mirror of
https://github.com/krahets/hello-algo.git
synced 2025-07-04 20:31:59 +08:00
build
This commit is contained in:
@ -1963,8 +1963,7 @@ comments: true
|
||||
impl MyList {
|
||||
/* 构造方法 */
|
||||
pub fn new(capacity: usize) -> Self {
|
||||
let mut vec = Vec::new();
|
||||
vec.resize(capacity, 0);
|
||||
let mut vec = vec![0; capacity];
|
||||
Self {
|
||||
arr: vec,
|
||||
capacity,
|
||||
@ -2036,7 +2035,7 @@ comments: true
|
||||
};
|
||||
let num = self.arr[index];
|
||||
// 将将索引 index 之后的元素都向前移动一位
|
||||
for j in (index..self.size - 1) {
|
||||
for j in index..self.size - 1 {
|
||||
self.arr[j] = self.arr[j + 1];
|
||||
}
|
||||
// 更新元素数量
|
||||
@ -2055,7 +2054,7 @@ comments: true
|
||||
}
|
||||
|
||||
/* 将列表转换为数组 */
|
||||
pub fn to_array(&mut self) -> Vec<i32> {
|
||||
pub fn to_array(&self) -> Vec<i32> {
|
||||
// 仅转换有效长度范围内的列表元素
|
||||
let mut arr = Vec::new();
|
||||
for i in 0..self.size {
|
||||
|
@ -15,7 +15,7 @@ comments: true
|
||||
- 子集和问题的目标是在给定集合中找到和为目标值的所有子集。集合不区分元素顺序,而搜索过程会输出所有顺序的结果,产生重复子集。我们在回溯前将数据进行排序,并设置一个变量来指示每一轮的遍历起始点,从而将生成重复子集的搜索分支进行剪枝。
|
||||
- 对于子集和问题,数组中的相等元素会产生重复集合。我们利用数组已排序的前置条件,通过判断相邻元素是否相等实现剪枝,从而确保相等元素在每轮中只能被选中一次。
|
||||
- $n$ 皇后问题旨在寻找将 $n$ 个皇后放置到 $n \times n$ 尺寸棋盘上的方案,要求所有皇后两两之间无法攻击对方。该问题的约束条件有行约束、列约束、主对角线和次对角线约束。为满足行约束,我们采用按行放置的策略,保证每一行放置一个皇后。
|
||||
- 列约束和对角线约束的处理方式类似。对于列约束,我们利用一个数组来记录每一列是否有皇后,从而指示选中的格子是否合法。对于对角线约束,我们借助两个数组来分别记录该主、次对角线上是否存在皇后;难点在于找处在到同一主(副)对角线上格子满足的行列索引规律。
|
||||
- 列约束和对角线约束的处理方式类似。对于列约束,我们利用一个数组来记录每一列是否有皇后,从而指示选中的格子是否合法。对于对角线约束,我们借助两个数组来分别记录该主、次对角线上是否存在皇后;难点在于找出处在同一主(副)对角线上的格子所满足的行列索引规律。
|
||||
|
||||
### 2. Q & A
|
||||
|
||||
|
@ -354,7 +354,7 @@ index = hash(key) % capacity
|
||||
for (const c of key) {
|
||||
hash ^= c.charCodeAt(0);
|
||||
}
|
||||
return hash & MODULUS;
|
||||
return hash % MODULUS;
|
||||
}
|
||||
|
||||
/* 旋转哈希 */
|
||||
@ -398,7 +398,7 @@ index = hash(key) % capacity
|
||||
for (const c of key) {
|
||||
hash ^= c.charCodeAt(0);
|
||||
}
|
||||
return hash & MODULUS;
|
||||
return hash % MODULUS;
|
||||
}
|
||||
|
||||
/* 旋转哈希 */
|
||||
|
@ -1025,10 +1025,10 @@ comments: true
|
||||
```rust title="hash_map_chaining.rs"
|
||||
/* 链式地址哈希表 */
|
||||
struct HashMapChaining {
|
||||
size: i32,
|
||||
capacity: i32,
|
||||
size: usize,
|
||||
capacity: usize,
|
||||
load_thres: f32,
|
||||
extend_ratio: i32,
|
||||
extend_ratio: usize,
|
||||
buckets: Vec<Vec<Pair>>,
|
||||
}
|
||||
|
||||
@ -1046,7 +1046,7 @@ comments: true
|
||||
|
||||
/* 哈希函数 */
|
||||
fn hash_func(&self, key: i32) -> usize {
|
||||
key as usize % self.capacity as usize
|
||||
key as usize % self.capacity
|
||||
}
|
||||
|
||||
/* 负载因子 */
|
||||
@ -1057,12 +1057,11 @@ comments: true
|
||||
/* 删除操作 */
|
||||
fn remove(&mut self, key: i32) -> Option<String> {
|
||||
let index = self.hash_func(key);
|
||||
let bucket = &mut self.buckets[index];
|
||||
|
||||
// 遍历桶,从中删除键值对
|
||||
for i in 0..bucket.len() {
|
||||
if bucket[i].key == key {
|
||||
let pair = bucket.remove(i);
|
||||
for (i, p) in self.buckets[index].iter_mut().enumerate() {
|
||||
if p.key == key {
|
||||
let pair = self.buckets[index].remove(i);
|
||||
self.size -= 1;
|
||||
return Some(pair.val);
|
||||
}
|
||||
@ -1075,7 +1074,7 @@ comments: true
|
||||
/* 扩容哈希表 */
|
||||
fn extend(&mut self) {
|
||||
// 暂存原哈希表
|
||||
let buckets_tmp = std::mem::replace(&mut self.buckets, vec![]);
|
||||
let buckets_tmp = std::mem::take(&mut self.buckets);
|
||||
|
||||
// 初始化扩容后的新哈希表
|
||||
self.capacity *= self.extend_ratio;
|
||||
@ -1109,30 +1108,27 @@ comments: true
|
||||
}
|
||||
|
||||
let index = self.hash_func(key);
|
||||
let bucket = &mut self.buckets[index];
|
||||
|
||||
// 遍历桶,若遇到指定 key ,则更新对应 val 并返回
|
||||
for pair in bucket {
|
||||
for pair in self.buckets[index].iter_mut() {
|
||||
if pair.key == key {
|
||||
pair.val = val;
|
||||
return;
|
||||
}
|
||||
}
|
||||
let bucket = &mut self.buckets[index];
|
||||
|
||||
// 若无该 key ,则将键值对添加至尾部
|
||||
let pair = Pair { key, val };
|
||||
bucket.push(pair);
|
||||
self.buckets[index].push(pair);
|
||||
self.size += 1;
|
||||
}
|
||||
|
||||
/* 查询操作 */
|
||||
fn get(&self, key: i32) -> Option<&str> {
|
||||
let index = self.hash_func(key);
|
||||
let bucket = &self.buckets[index];
|
||||
|
||||
// 遍历桶,若找到 key ,则返回对应 val
|
||||
for pair in bucket {
|
||||
for pair in self.buckets[index].iter() {
|
||||
if pair.key == key {
|
||||
return Some(&pair.val);
|
||||
}
|
||||
|
@ -27,9 +27,7 @@ comments: true
|
||||
"""计数排序"""
|
||||
# 简单实现,无法用于排序对象
|
||||
# 1. 统计数组最大元素 m
|
||||
m = 0
|
||||
for num in nums:
|
||||
m = max(m, num)
|
||||
m = max(nums)
|
||||
# 2. 统计各数字的出现次数
|
||||
# counter[num] 代表 num 的出现次数
|
||||
counter = [0] * (m + 1)
|
||||
|
@ -1679,7 +1679,7 @@ comments: true
|
||||
}
|
||||
}
|
||||
self.que_size -= 1; // 更新队列长度
|
||||
Rc::try_unwrap(old_front).ok().unwrap().into_inner().val
|
||||
old_front.borrow().val
|
||||
})
|
||||
}
|
||||
// 队尾出队操作
|
||||
@ -1695,7 +1695,7 @@ comments: true
|
||||
}
|
||||
}
|
||||
self.que_size -= 1; // 更新队列长度
|
||||
Rc::try_unwrap(old_rear).ok().unwrap().into_inner().val
|
||||
old_rear.borrow().val
|
||||
})
|
||||
}
|
||||
}
|
||||
@ -1722,12 +1722,16 @@ comments: true
|
||||
|
||||
/* 返回数组用于打印 */
|
||||
pub fn to_array(&self, head: Option<&Rc<RefCell<ListNode<T>>>>) -> Vec<T> {
|
||||
if let Some(node) = head {
|
||||
let mut nums = self.to_array(node.borrow().next.as_ref());
|
||||
nums.insert(0, node.borrow().val);
|
||||
return nums;
|
||||
let mut res: Vec<T> = Vec::new();
|
||||
fn recur<T: Copy>(cur: Option<&Rc<RefCell<ListNode<T>>>>, res: &mut Vec<T>) {
|
||||
if let Some(cur) = cur {
|
||||
res.push(cur.borrow().val);
|
||||
recur(cur.borrow().next.as_ref(), res);
|
||||
}
|
||||
}
|
||||
return Vec::new();
|
||||
|
||||
recur(head, &mut res);
|
||||
res
|
||||
}
|
||||
}
|
||||
```
|
||||
|
@ -1070,7 +1070,7 @@ comments: true
|
||||
}
|
||||
}
|
||||
self.que_size -= 1;
|
||||
Rc::try_unwrap(old_front).ok().unwrap().into_inner().val
|
||||
old_front.borrow().val
|
||||
})
|
||||
}
|
||||
|
||||
@ -1081,12 +1081,18 @@ comments: true
|
||||
|
||||
/* 将链表转化为 Array 并返回 */
|
||||
pub fn to_array(&self, head: Option<&Rc<RefCell<ListNode<T>>>>) -> Vec<T> {
|
||||
if let Some(node) = head {
|
||||
let mut nums = self.to_array(node.borrow().next.as_ref());
|
||||
nums.insert(0, node.borrow().val);
|
||||
return nums;
|
||||
let mut res: Vec<T> = Vec::new();
|
||||
|
||||
fn recur<T: Copy>(cur: Option<&Rc<RefCell<ListNode<T>>>>, res: &mut Vec<T>) {
|
||||
if let Some(cur) = cur {
|
||||
res.push(cur.borrow().val);
|
||||
recur(cur.borrow().next.as_ref(), res);
|
||||
}
|
||||
}
|
||||
return Vec::new();
|
||||
|
||||
recur(head, &mut res);
|
||||
|
||||
res
|
||||
}
|
||||
}
|
||||
```
|
||||
|
@ -964,16 +964,10 @@ comments: true
|
||||
/* 出栈 */
|
||||
pub fn pop(&mut self) -> Option<T> {
|
||||
self.stack_peek.take().map(|old_head| {
|
||||
match old_head.borrow_mut().next.take() {
|
||||
Some(new_head) => {
|
||||
self.stack_peek = Some(new_head);
|
||||
}
|
||||
None => {
|
||||
self.stack_peek = None;
|
||||
}
|
||||
}
|
||||
self.stack_peek = old_head.borrow_mut().next.take();
|
||||
self.stk_size -= 1;
|
||||
Rc::try_unwrap(old_head).ok().unwrap().into_inner().val
|
||||
|
||||
old_head.borrow().val
|
||||
})
|
||||
}
|
||||
|
||||
|
@ -9,9 +9,9 @@ icon: material/map-marker-path
|
||||
|
||||
!!! abstract
|
||||
|
||||
Like explorers in a maze, we may encounter difficulties on our path forward.
|
||||
Like explorers in a maze, we may encounter obstacles on our path forward.
|
||||
|
||||
The power of backtracking allows us to start over, keep trying, and eventually find the exit to the light.
|
||||
The power of backtracking lets us begin anew, keep trying, and eventually find the exit leading to the light.
|
||||
|
||||
## Chapter contents
|
||||
|
||||
|
@ -11,7 +11,7 @@ icon: material/timer-sand
|
||||
|
||||
Complexity analysis is like a space-time navigator in the vast universe of algorithms.
|
||||
|
||||
It guides us in exploring deeper within the the dimensions of time and space, seeking more elegant solutions.
|
||||
It guides us in exploring deeper within the dimensions of time and space, seeking more elegant solutions.
|
||||
|
||||
## Chapter contents
|
||||
|
||||
|
@ -2,58 +2,58 @@
|
||||
comments: true
|
||||
---
|
||||
|
||||
# 12.3 Building binary tree problem
|
||||
# 12.3 Building a binary tree problem
|
||||
|
||||
!!! question
|
||||
|
||||
Given the pre-order traversal `preorder` and in-order traversal `inorder` of a binary tree, construct the binary tree and return the root node of the binary tree. Assume that there are no duplicate values in the nodes of the binary tree (as shown in Figure 12-5).
|
||||
Given the pre-order traversal `preorder` sequence and the in-order traversal `inorder` sequence of a binary tree, construct the binary tree and return its root node. Assume there are no duplicate node values in the binary tree (as shown in Figure 12-5).
|
||||
|
||||
{ class="animation-figure" }
|
||||
|
||||
<p align="center"> Figure 12-5 Example data for building a binary tree </p>
|
||||
|
||||
### 1. Determining if it is a divide and conquer problem
|
||||
### 1. Determining if it is a divide-and-conquer problem
|
||||
|
||||
The original problem of constructing a binary tree from `preorder` and `inorder` is a typical divide and conquer problem.
|
||||
The original problem of building a binary tree from the `preorder` and the `inorder` sequences is a typical divide-and-conquer problem.
|
||||
|
||||
- **The problem can be decomposed**: From the perspective of divide and conquer, we can divide the original problem into two subproblems: building the left subtree and building the right subtree, plus one operation: initializing the root node. For each subtree (subproblem), we can still use the above division method, dividing it into smaller subtrees (subproblems), until the smallest subproblem (empty subtree) is reached.
|
||||
- **The subproblems are independent**: The left and right subtrees are independent of each other, with no overlap. When building the left subtree, we only need to focus on the parts of the in-order and pre-order traversals that correspond to the left subtree. The same applies to the right subtree.
|
||||
- **Solutions to subproblems can be combined**: Once the solutions for the left and right subtrees (solutions to subproblems) are obtained, we can link them to the root node to obtain the solution to the original problem.
|
||||
- **The problem can be decomposed**: From the perspective of divide-and-conquer, we can divide the original problem into two subproblems—building the left subtree and building the right subtree—plus one operation of initializing the root node. For each subtree (subproblem), we continue applying the same approach, partitioning it into smaller subtrees (subproblems), until reaching the smallest subproblem (an empty subtree).
|
||||
- **The subproblems are independent**: The left and right subtrees do not overlap. When building the left subtree, we only need the segments of the in-order and pre-order traversals that correspond to the left subtree. The same approach applies to the right subtree.
|
||||
- **Solutions to subproblems can be combined**: Once we have constructed the left and right subtrees (the subproblem solutions), we can attach them to the root node to obtain the solution to the original problem.
|
||||
|
||||
### 2. How to divide the subtrees
|
||||
|
||||
Based on the above analysis, this problem can be solved using divide and conquer, **but how do we use the pre-order traversal `preorder` and in-order traversal `inorder` to divide the left and right subtrees?**
|
||||
Based on the above analysis, this problem can be solved using divide-and-conquer. **However, how do we use the pre-order traversal `preorder` sequence and the in-order traversal `inorder` sequence to divide the left and right subtrees?**
|
||||
|
||||
By definition, `preorder` and `inorder` can be divided into three parts.
|
||||
By definition, both the `preorder` and `inorder` sequences can be divided into three parts:
|
||||
|
||||
- Pre-order traversal: `[ Root | Left Subtree | Right Subtree ]`, for example, the tree in the figure corresponds to `[ 3 | 9 | 2 1 7 ]`.
|
||||
- In-order traversal: `[ Left Subtree | Root | Right Subtree ]`, for example, the tree in the figure corresponds to `[ 9 | 3 | 1 2 7 ]`.
|
||||
- Pre-order traversal: `[ Root | Left Subtree | Right Subtree ]`. For example, in the figure, the tree corresponds to `[ 3 | 9 | 2 1 7 ]`.
|
||||
- In-order traversal: `[ Left Subtree | Root | Right Subtree ]`. For example, in the figure, the tree corresponds to `[ 9 | 3 | 1 2 7 ]`.
|
||||
|
||||
Using the data in the figure above, we can obtain the division results as shown in Figure 12-6.
|
||||
Using the data from the preceding figure, we can follow the steps shown in the next figure to obtain the division results:
|
||||
|
||||
1. The first element 3 in the pre-order traversal is the value of the root node.
|
||||
2. Find the index of the root node 3 in `inorder`, and use this index to divide `inorder` into `[ 9 | 3 | 1 2 7 ]`.
|
||||
3. Based on the division results of `inorder`, it is easy to determine the number of nodes in the left and right subtrees as 1 and 3, respectively, thus dividing `preorder` into `[ 3 | 9 | 2 1 7 ]`.
|
||||
2. Find the index of the root node 3 in the `inorder` sequence, and use this index to split `inorder` into `[ 9 | 3 | 1 2 7 ]`.
|
||||
3. According to the split of the `inorder` sequence, it is straightforward to determine that the left and right subtrees contain 1 and 3 nodes, respectively, so we can split the `preorder` sequence into `[ 3 | 9 | 2 1 7 ]` accordingly.
|
||||
|
||||
{ class="animation-figure" }
|
||||
{ class="animation-figure" }
|
||||
|
||||
<p align="center"> Figure 12-6 Dividing the subtrees in pre-order and in-order traversals </p>
|
||||
|
||||
### 3. Describing subtree intervals based on variables
|
||||
### 3. Describing subtree ranges based on variables
|
||||
|
||||
Based on the above division method, **we have now obtained the index intervals of the root, left subtree, and right subtree in `preorder` and `inorder`**. To describe these index intervals, we need the help of several pointer variables.
|
||||
Based on the above division method, **we have now obtained the index ranges of the root, left subtree, and right subtree in the `preorder` and `inorder` sequences**. To describe these index ranges, we use several pointer variables.
|
||||
|
||||
- Let the index of the current tree's root node in `preorder` be denoted as $i$.
|
||||
- Let the index of the current tree's root node in `inorder` be denoted as $m$.
|
||||
- Let the index interval of the current tree in `inorder` be denoted as $[l, r]$.
|
||||
- Let the index of the current tree's root node in the `preorder` sequence be denoted as $i$.
|
||||
- Let the index of the current tree's root node in the `inorder` sequence be denoted as $m$.
|
||||
- Let the index range of the current tree in the `inorder` sequence be denoted as $[l, r]$.
|
||||
|
||||
As shown in Table 12-1, the above variables can represent the index of the root node in `preorder` as well as the index intervals of the subtrees in `inorder`.
|
||||
As shown in Table 12-1, these variables represent the root node’s index in the `preorder` sequence and the index ranges of the subtrees in the `inorder` sequence.
|
||||
|
||||
<p align="center"> Table 12-1 Indexes of the root node and subtrees in pre-order and in-order traversals </p>
|
||||
|
||||
<div class="center-table" markdown>
|
||||
|
||||
| | Root node index in `preorder` | Subtree index interval in `inorder` |
|
||||
| | Root node index in `preorder` | Subtree index range in `inorder` |
|
||||
| ------------- | ----------------------------- | ----------------------------------- |
|
||||
| Current tree | $i$ | $[l, r]$ |
|
||||
| Left subtree | $i + 1$ | $[l, m-1]$ |
|
||||
@ -61,7 +61,7 @@ As shown in Table 12-1, the above variables can represent the index of the root
|
||||
|
||||
</div>
|
||||
|
||||
Please note, the meaning of $(m-l)$ in the right subtree root index is "the number of nodes in the left subtree", which is suggested to be understood in conjunction with Figure 12-7.
|
||||
Please note that $(m-l)$ in the right subtree root index represents "the number of nodes in the left subtree." It may help to consult Figure 12-7 for a clearer understanding.
|
||||
|
||||
{ class="animation-figure" }
|
||||
|
||||
@ -69,7 +69,7 @@ Please note, the meaning of $(m-l)$ in the right subtree root index is "the numb
|
||||
|
||||
### 4. Code implementation
|
||||
|
||||
To improve the efficiency of querying $m$, we use a hash table `hmap` to store the mapping of elements in `inorder` to their indexes:
|
||||
To improve the efficiency of querying $m$, we use a hash table `hmap` to store the mapping from elements in the `inorder` sequence to their indexes:
|
||||
|
||||
=== "Python"
|
||||
|
||||
@ -256,7 +256,7 @@ To improve the efficiency of querying $m$, we use a hash table `hmap` to store t
|
||||
[class]{}-[func]{buildTree}
|
||||
```
|
||||
|
||||
Figure 12-8 shows the recursive process of building the binary tree, where each node is established during the "descending" process, and each edge (reference) is established during the "ascending" process.
|
||||
Figure 12-8 shows the recursive process of building the binary tree. Each node is created during the "descending" phase of the recursion, and each edge (reference) is formed during the "ascending" phase.
|
||||
|
||||
=== "<1>"
|
||||
{ class="animation-figure" }
|
||||
@ -287,12 +287,12 @@ Figure 12-8 shows the recursive process of building the binary tree, where each
|
||||
|
||||
<p align="center"> Figure 12-8 Recursive process of building a binary tree </p>
|
||||
|
||||
Each recursive function's division results of `preorder` and `inorder` are shown in Figure 12-9.
|
||||
Each recursive function's division of the `preorder` and `inorder` sequences is illustrated in Figure 12-9.
|
||||
|
||||
{ class="animation-figure" }
|
||||
{ class="animation-figure" }
|
||||
|
||||
<p align="center"> Figure 12-9 Division results in each recursive function </p>
|
||||
<p align="center"> Figure 12-9 Division in each recursive function </p>
|
||||
|
||||
Assuming the number of nodes in the tree is $n$, initializing each node (executing a recursive function `dfs()`) takes $O(1)$ time. **Thus, the overall time complexity is $O(n)$**.
|
||||
Assuming the binary tree has $n$ nodes, initializing each node (calling the recursive function `dfs()`) takes $O(1)$ time. **Therefore, the overall time complexity is $O(n)$**.
|
||||
|
||||
The hash table stores the mapping of `inorder` elements to their indexes, with a space complexity of $O(n)$. In the worst case, when the binary tree degenerates into a linked list, the recursive depth reaches $n$, using $O(n)$ stack frame space. **Therefore, the overall space complexity is $O(n)$**.
|
||||
Because the hash table stores the mapping from `inorder` elements to their indexes, it requires $O(n)$ space. In the worst case, if the binary tree degenerates into a linked list, the recursive depth can reach $n$, consuming $O(n)$ stack space. **Hence, the overall space complexity is $O(n)$**.
|
||||
|
@ -4,10 +4,10 @@ comments: true
|
||||
|
||||
# 12.1 Divide and conquer algorithms
|
||||
|
||||
<u>Divide and conquer</u>, fully referred to as "divide and rule", is an extremely important and common algorithm strategy. Divide and conquer is usually based on recursion and includes two steps: "divide" and "conquer".
|
||||
<u>Divide and conquer</u> is an important and popular algorithm strategy. As the name suggests, the algorithm is typically implemented recursively and consists of two steps: "divide" and "conquer".
|
||||
|
||||
1. **Divide (partition phase)**: Recursively decompose the original problem into two or more sub-problems until the smallest sub-problem is reached and the process terminates.
|
||||
2. **Conquer (merge phase)**: Starting from the smallest sub-problem with a known solution, merge the solutions of the sub-problems from bottom to top to construct the solution to the original problem.
|
||||
1. **Divide (partition phase)**: Recursively break down the original problem into two or more smaller sub-problems until the smallest sub-problem is reached.
|
||||
2. **Conquer (merge phase)**: Starting from the smallest sub-problem with known solution, we construct the solution to the original problem by merging the solutions of sub-problems in a bottom-up manner.
|
||||
|
||||
As shown in Figure 12-1, "merge sort" is one of the typical applications of the divide and conquer strategy.
|
||||
|
||||
@ -20,27 +20,27 @@ As shown in Figure 12-1, "merge sort" is one of the typical applications of the
|
||||
|
||||
## 12.1.1 How to identify divide and conquer problems
|
||||
|
||||
Whether a problem is suitable for a divide and conquer solution can usually be judged based on the following criteria.
|
||||
Whether a problem is suitable for a divide-and-conquer solution can usually be decided based on the following criteria.
|
||||
|
||||
1. **The problem can be decomposed**: The original problem can be decomposed into smaller, similar sub-problems and can be recursively divided in the same manner.
|
||||
1. **The problem can be broken down into smaller ones**: The original problem can be divided into smaller, similar sub-problems and such process can be recursively done in the same manner.
|
||||
2. **Sub-problems are independent**: There is no overlap between sub-problems, and they are independent and can be solved separately.
|
||||
3. **Solutions to sub-problems can be merged**: The solution to the original problem is obtained by merging the solutions of the sub-problems.
|
||||
3. **Solutions to sub-problems can be merged**: The solution to the original problem is derived by combining the solutions of the sub-problems.
|
||||
|
||||
Clearly, merge sort meets these three criteria.
|
||||
|
||||
1. **The problem can be decomposed**: Recursively divide the array (original problem) into two sub-arrays (sub-problems).
|
||||
1. **The problem can be broken down into smaller ones**: Recursively divide the array (original problem) into two sub-arrays (sub-problems).
|
||||
2. **Sub-problems are independent**: Each sub-array can be sorted independently (sub-problems can be solved independently).
|
||||
3. **Solutions to sub-problems can be merged**: Two ordered sub-arrays (solutions to the sub-problems) can be merged into one ordered array (solution to the original problem).
|
||||
|
||||
## 12.1.2 Improving efficiency through divide and conquer
|
||||
## 12.1.2 Improve efficiency through divide and conquer
|
||||
|
||||
**Divide and conquer can not only effectively solve algorithm problems but often also improve algorithm efficiency**. In sorting algorithms, quicksort, merge sort, and heap sort are faster than selection, bubble, and insertion sorts because they apply the divide and conquer strategy.
|
||||
The **divide-and-conquer strategy not only effectively solves algorithm problems but also often enhances efficiency**. In sorting algorithms, quick sort, merge sort, and heap sort are faster than selection sort, bubble sort, and insertion sort because they apply the divide-and-conquer strategy.
|
||||
|
||||
Then, we may ask: **Why can divide and conquer improve algorithm efficiency, and what is the underlying logic?** In other words, why are the steps of decomposing a large problem into multiple sub-problems, solving the sub-problems, and merging the solutions of the sub-problems into the solution of the original problem more efficient than directly solving the original problem? This question can be discussed from the aspects of the number of operations and parallel computation.
|
||||
We may have a question in mind: **Why can divide and conquer improve algorithm efficiency, and what is the underlying logic?** In other words, why is breaking a problem into sub-problems, solving them, and combining their solutions to address the original problem offer more efficiency than directly solving the original problem? This question can be analyzed from two aspects: operation count and parallel computation.
|
||||
|
||||
### 1. Optimization of operation count
|
||||
|
||||
Taking "bubble sort" as an example, it requires $O(n^2)$ time to process an array of length $n$. Suppose we divide the array from the midpoint into two sub-arrays as shown in Figure 12-2, then the division requires $O(n)$ time, sorting each sub-array requires $O((n / 2)^2)$ time, and merging the two sub-arrays requires $O(n)$ time, with the total time complexity being:
|
||||
Taking "bubble sort" as an example, it requires $O(n^2)$ time to process an array of length $n$. Suppose we divide the array from the midpoint into two sub-arrays as shown in Figure 12-2, such division requires $O(n)$ time. Sorting each sub-array requires $O((n / 2)^2)$ time. And merging the two sub-arrays requires $O(n)$ time. Thus, the overall time complexity is:
|
||||
|
||||
$$
|
||||
O(n + (\frac{n}{2})^2 \times 2 + n) = O(\frac{n^2}{2} + 2n)
|
||||
@ -50,7 +50,7 @@ $$
|
||||
|
||||
<p align="center"> Figure 12-2 Bubble sort before and after array partition </p>
|
||||
|
||||
Next, we calculate the following inequality, where the left and right sides are the total number of operations before and after the partition, respectively:
|
||||
Let's calculate the following inequality, where the left side represents the total number of operations before division and the right side represents the total number of operations after division, respectively:
|
||||
|
||||
$$
|
||||
\begin{aligned}
|
||||
@ -60,19 +60,19 @@ n(n - 4) & > 0
|
||||
\end{aligned}
|
||||
$$
|
||||
|
||||
**This means that when $n > 4$, the number of operations after partitioning is fewer, and the sorting efficiency should be higher**. Please note that the time complexity after partitioning is still quadratic $O(n^2)$, but the constant factor in the complexity has decreased.
|
||||
**This means that when $n > 4$, the number of operations after partitioning is fewer, leading to better performance**. Please note that the time complexity after partitioning is still quadratic $O(n^2)$, but the constant factor in the complexity has decreased.
|
||||
|
||||
Further, **what if we keep dividing the sub-arrays from their midpoints into two sub-arrays** until the sub-arrays have only one element left? This idea is actually "merge sort," with a time complexity of $O(n \log n)$.
|
||||
We can go even further. **How about keeping dividing the sub-arrays from their midpoints into two sub-arrays** until the sub-arrays have only one element left? This idea is actually "merge sort," with a time complexity of $O(n \log n)$.
|
||||
|
||||
Furthermore, **what if we set several more partition points** and evenly divide the original array into $k$ sub-arrays? This situation is very similar to "bucket sort," which is very suitable for sorting massive data, and theoretically, the time complexity can reach $O(n + k)$.
|
||||
Let's try something a bit different again. **How about splitting into more partitions instead of just two?** For example, we evenly divide the original array into $k$ sub-arrays? This approach is very similar to "bucket sort," which is very suitable for sorting massive data. Theoretically, the time complexity can reach $O(n + k)$.
|
||||
|
||||
### 2. Optimization through parallel computation
|
||||
|
||||
We know that the sub-problems generated by divide and conquer are independent of each other, **thus they can usually be solved in parallel**. This means that divide and conquer can not only reduce the algorithm's time complexity, **but also facilitate parallel optimization by the operating system**.
|
||||
We know that the sub-problems generated by divide and conquer are independent of each other, **which means that they can be solved in parallel.** As a result, divide and conquer not only reduces the algorithm's time complexity, **but also facilitates parallel optimization by modern operating systems.**
|
||||
|
||||
Parallel optimization is especially effective in environments with multiple cores or processors, as the system can process multiple sub-problems simultaneously, making fuller use of computing resources and significantly reducing the overall runtime.
|
||||
Parallel optimization is particularly effective in environments with multiple cores or processors. As the system can process multiple sub-problems simultaneously, fully utilizing computing resources, the overall runtime is significantly reduced.
|
||||
|
||||
For example, in the "bucket sort" shown in Figure 12-3, we distribute massive data evenly across various buckets, then the sorting tasks of all buckets can be distributed to different computing units, and the results are merged after completion.
|
||||
For example, in the "bucket sort" shown in Figure 12-3, we break massive data evenly into various buckets. The jobs of sorting each bucket can be allocated to available computing units. Once all jobs are done, all sorted buckets are merged to produce the final result.
|
||||
|
||||
{ class="animation-figure" }
|
||||
|
||||
@ -80,22 +80,22 @@ For example, in the "bucket sort" shown in Figure 12-3, we distribute massive da
|
||||
|
||||
## 12.1.3 Common applications of divide and conquer
|
||||
|
||||
On one hand, divide and conquer can be used to solve many classic algorithm problems.
|
||||
Divide and conquer can be used to solve many classic algorithm problems.
|
||||
|
||||
- **Finding the closest point pair**: This algorithm first divides the set of points into two parts, then finds the closest point pair in each part, and finally finds the closest point pair that spans the two parts.
|
||||
- **Large integer multiplication**: For example, the Karatsuba algorithm, which breaks down large integer multiplication into several smaller integer multiplications and additions.
|
||||
- **Matrix multiplication**: For example, the Strassen algorithm, which decomposes large matrix multiplication into multiple small matrix multiplications and additions.
|
||||
- **Tower of Hanoi problem**: The Tower of Hanoi problem can be solved recursively, a typical application of the divide and conquer strategy.
|
||||
- **Solving inverse pairs**: In a sequence, if a number in front is greater than a number behind, these two numbers form an inverse pair. Solving the inverse pair problem can utilize the idea of divide and conquer, with the aid of merge sort.
|
||||
- **Finding the closest pair of points**: This algorithm works by dividing the set of points into two halves. Then it recursively finds the closest pair in each half. Finally it considers pairs that span the two halves to find the overall closest pair.
|
||||
- **Large integer multiplication**: One algorithm is called Karatsuba. It breaks down large integer multiplication into several smaller integer multiplications and additions.
|
||||
- **Matrix multiplication**: One example is the Strassen algorithm. It breaks down a large matrix multiplication into multiple small matrix multiplications and additions.
|
||||
- **Tower of Hanoi problem**: The Tower of Hanoi problem can be solved recursively, a typical application of the divide-and-conquer strategy.
|
||||
- **Solving inversion pairs**: In a sequence, if a preceding number is greater than a following number, then these two numbers constitute an inversion pair. Solving inversion pair problem can utilize the idea of divide and conquer, with the aid of merge sort.
|
||||
|
||||
On the other hand, divide and conquer is very widely applied in the design of algorithms and data structures.
|
||||
Divide and conquer is also widely applied in the design of algorithms and data structures.
|
||||
|
||||
- **Binary search**: Binary search divides an ordered array from the midpoint index into two parts, then decides which half to exclude based on the comparison result between the target value and the middle element value, and performs the same binary operation in the remaining interval.
|
||||
- **Binary search**: Binary search divides a sorted array into two halves from the midpoint index. And then based on the comparison result between the target value and the middle element value, one half is discarded. The search continues on the remaining half with the same process until the target is found or there is no remaining element.
|
||||
- **Merge sort**: Already introduced at the beginning of this section, no further elaboration is needed.
|
||||
- **Quicksort**: Quicksort selects a pivot value, then divides the array into two sub-arrays, one with elements smaller than the pivot and the other with elements larger than the pivot, and then performs the same partitioning operation on these two parts until the sub-array has only one element.
|
||||
- **Bucket sort**: The basic idea of bucket sort is to distribute data to multiple buckets, then sort the elements within each bucket, and finally retrieve the elements from the buckets in order to obtain an ordered array.
|
||||
- **Trees**: For example, binary search trees, AVL trees, red-black trees, B-trees, B+ trees, etc., their operations such as search, insertion, and deletion can all be considered applications of the divide and conquer strategy.
|
||||
- **Heap**: A heap is a special type of complete binary tree, whose various operations, such as insertion, deletion, and heapification, actually imply the idea of divide and conquer.
|
||||
- **Hash table**: Although hash tables do not directly apply divide and conquer, some hash collision resolution solutions indirectly apply the divide and conquer strategy, for example, long lists in chained addressing being converted to red-black trees to improve query efficiency.
|
||||
- **Quicksort**: Quicksort picks a pivot value to divide the array into two sub-arrays, one with elements smaller than the pivot and the other with elements larger than the pivot. Such process goes on against each of these two sub-arrays until they hold only one element.
|
||||
- **Bucket sort**: The basic idea of bucket sort is to distribute data to multiple buckets. After sorting the elements within each bucket, retrieve the elements from the buckets in order to obtain an ordered array.
|
||||
- **Trees**: For example, binary search trees, AVL trees, red-black trees, B-trees, and B+ trees, etc. Their operations, such as search, insertion, and deletion, can all be regarded as applications of the divide-and-conquer strategy.
|
||||
- **Heap**: A heap is a special type of complete binary tree. Its various operations, such as insertion, deletion, and heapify, actually imply the idea of divide and conquer.
|
||||
- **Hash table**: Although hash tables do not directly apply divide and conquer, some hash collision resolution solutions indirectly apply the strategy. For example, long lists in chained addressing may be converted to red-black trees to improve query efficiency.
|
||||
|
||||
It can be seen that **divide and conquer is a subtly pervasive algorithmic idea**, embedded within various algorithms and data structures.
|
||||
|
@ -4,13 +4,13 @@ comments: true
|
||||
|
||||
# 12.4 Tower of Hanoi Problem
|
||||
|
||||
In both merge sorting and building binary trees, we decompose the original problem into two subproblems, each half the size of the original problem. However, for the Tower of Hanoi, we adopt a different decomposition strategy.
|
||||
In both merge sort and binary tree construction, we break the original problem into two subproblems, each half the size of the original problem. However, for the Tower of Hanoi, we adopt a different decomposition strategy.
|
||||
|
||||
!!! question
|
||||
|
||||
Given three pillars, denoted as `A`, `B`, and `C`. Initially, pillar `A` is stacked with $n$ discs, arranged in order from top to bottom from smallest to largest. Our task is to move these $n$ discs to pillar `C`, maintaining their original order (as shown in Figure 12-10). The following rules must be followed during the disc movement process:
|
||||
We are given three pillars, denoted as `A`, `B`, and `C`. Initially, pillar `A` has $n$ discs, arranged from top to bottom in ascending size. Our task is to move these $n$ discs to pillar `C`, maintaining their original order (as shown in Figure 12-10). The following rules apply during the movement:
|
||||
|
||||
1. A disc can only be picked up from the top of a pillar and placed on top of another pillar.
|
||||
1. A disc can be removed only from the top of a pillar and must be placed on the top of another pillar.
|
||||
2. Only one disc can be moved at a time.
|
||||
3. A smaller disc must always be on top of a larger disc.
|
||||
|
||||
@ -18,11 +18,11 @@ In both merge sorting and building binary trees, we decompose the original probl
|
||||
|
||||
<p align="center"> Figure 12-10 Example of the Tower of Hanoi </p>
|
||||
|
||||
**We denote the Tower of Hanoi of size $i$ as $f(i)$**. For example, $f(3)$ represents the Tower of Hanoi of moving $3$ discs from `A` to `C`.
|
||||
**We denote the Tower of Hanoi problem of size $i$ as $f(i)$**. For example, $f(3)$ represents moving $3$ discs from pillar `A` to pillar `C`.
|
||||
|
||||
### 1. Consider the base case
|
||||
### 1. Consider the base cases
|
||||
|
||||
As shown in Figure 12-11, for the problem $f(1)$, i.e., when there is only one disc, we can directly move it from `A` to `C`.
|
||||
As shown in Figure 12-11, for the problem $f(1)$—which has only one disc—we can directly move it from `A` to `C`.
|
||||
|
||||
=== "<1>"
|
||||
{ class="animation-figure" }
|
||||
@ -32,7 +32,7 @@ As shown in Figure 12-11, for the problem $f(1)$, i.e., when there is only one d
|
||||
|
||||
<p align="center"> Figure 12-11 Solution for a problem of size 1 </p>
|
||||
|
||||
As shown in Figure 12-12, for the problem $f(2)$, i.e., when there are two discs, **since the smaller disc must always be above the larger disc, `B` is needed to assist in the movement**.
|
||||
For $f(2)$—which has two discs—**we rely on pillar `B` to help keep the smaller disc above the larger disc**, as illustrated in the following figure:
|
||||
|
||||
1. First, move the smaller disc from `A` to `B`.
|
||||
2. Then move the larger disc from `A` to `C`.
|
||||
@ -52,17 +52,17 @@ As shown in Figure 12-12, for the problem $f(2)$, i.e., when there are two discs
|
||||
|
||||
<p align="center"> Figure 12-12 Solution for a problem of size 2 </p>
|
||||
|
||||
The process of solving the problem $f(2)$ can be summarized as: **moving two discs from `A` to `C` with the help of `B`**. Here, `C` is called the target pillar, and `B` is called the buffer pillar.
|
||||
The process of solving $f(2)$ can be summarized as: **moving two discs from `A` to `C` with the help of `B`**. Here, `C` is called the target pillar, and `B` is called the buffer pillar.
|
||||
|
||||
### 2. Decomposition of subproblems
|
||||
|
||||
For the problem $f(3)$, i.e., when there are three discs, the situation becomes slightly more complicated.
|
||||
For the problem $f(3)$—that is, when there are three discs—the situation becomes slightly more complicated.
|
||||
|
||||
Since we already know the solutions to $f(1)$ and $f(2)$, we can think from a divide-and-conquer perspective and **consider the two top discs on `A` as a unit**, performing the steps shown in Figure 12-13. This way, the three discs are successfully moved from `A` to `C`.
|
||||
Since we already know the solutions to $f(1)$ and $f(2)$, we can adopt a divide-and-conquer perspective and **treat the top two discs on `A` as a single unit**, performing the steps shown in Figure 12-13. This allows the three discs to be successfully moved from `A` to `C`.
|
||||
|
||||
1. Let `B` be the target pillar and `C` the buffer pillar, and move the two discs from `A` to `B`.
|
||||
1. Let `B` be the target pillar and `C` the buffer pillar, then move the two discs from `A` to `B`.
|
||||
2. Move the remaining disc from `A` directly to `C`.
|
||||
3. Let `C` be the target pillar and `A` the buffer pillar, and move the two discs from `B` to `C`.
|
||||
3. Let `C` be the target pillar and `A` the buffer pillar, then move the two discs from `B` to `C`.
|
||||
|
||||
=== "<1>"
|
||||
{ class="animation-figure" }
|
||||
@ -78,23 +78,23 @@ Since we already know the solutions to $f(1)$ and $f(2)$, we can think from a di
|
||||
|
||||
<p align="center"> Figure 12-13 Solution for a problem of size 3 </p>
|
||||
|
||||
Essentially, **we divide the problem $f(3)$ into two subproblems $f(2)$ and one subproblem $f(1)$**. By solving these three subproblems in order, the original problem is resolved. This indicates that the subproblems are independent, and their solutions can be merged.
|
||||
Essentially, **we decompose $f(3)$ into two $f(2)$ subproblems and one $f(1)$ subproblem**. By solving these three subproblems in sequence, the original problem is solved, indicating that the subproblems are independent and their solutions can be merged.
|
||||
|
||||
From this, we can summarize the divide-and-conquer strategy for solving the Tower of Hanoi shown in Figure 12-14: divide the original problem $f(n)$ into two subproblems $f(n-1)$ and one subproblem $f(1)$, and solve these three subproblems in the following order.
|
||||
From this, we can summarize the divide-and-conquer strategy for the Tower of Hanoi, illustrated in Figure 12-14. We divide the original problem $f(n)$ into two subproblems $f(n-1)$ and one subproblem $f(1)$, and solve these three subproblems in the following order:
|
||||
|
||||
1. Move $n-1$ discs with the help of `C` from `A` to `B`.
|
||||
2. Move the remaining one disc directly from `A` to `C`.
|
||||
3. Move $n-1$ discs with the help of `A` from `B` to `C`.
|
||||
1. Move $n-1$ discs from `A` to `B`, using `C` as a buffer.
|
||||
2. Move the remaining disc directly from `A` to `C`.
|
||||
3. Move $n-1$ discs from `B` to `C`, using `A` as a buffer.
|
||||
|
||||
For these two subproblems $f(n-1)$, **they can be recursively divided in the same manner** until the smallest subproblem $f(1)$ is reached. The solution to $f(1)$ is already known and requires only one move.
|
||||
For each $f(n-1)$ subproblem, **we can apply the same recursive partition** until we reach the smallest subproblem $f(1)$. Because $f(1)$ is already known to require just a single move, it is trivial to solve.
|
||||
|
||||
{ class="animation-figure" }
|
||||
{ class="animation-figure" }
|
||||
|
||||
<p align="center"> Figure 12-14 Divide and conquer strategy for solving the Tower of Hanoi </p>
|
||||
<p align="center"> Figure 12-14 Divide-and-conquer strategy for solving the Tower of Hanoi </p>
|
||||
|
||||
### 3. Code implementation
|
||||
|
||||
In the code, we declare a recursive function `dfs(i, src, buf, tar)` whose role is to move the $i$ discs on top of pillar `src` with the help of buffer pillar `buf` to the target pillar `tar`:
|
||||
In the code, we define a recursive function `dfs(i, src, buf, tar)` which moves the top $i$ discs from pillar `src` to pillar `tar`, using pillar `buf` as a buffer:
|
||||
|
||||
=== "Python"
|
||||
|
||||
@ -305,7 +305,7 @@ In the code, we declare a recursive function `dfs(i, src, buf, tar)` whose role
|
||||
[class]{}-[func]{solveHanota}
|
||||
```
|
||||
|
||||
As shown in Figure 12-15, the Tower of Hanoi forms a recursive tree with a height of $n$, each node representing a subproblem, corresponding to an open `dfs()` function, **thus the time complexity is $O(2^n)$, and the space complexity is $O(n)$**.
|
||||
As shown in Figure 12-15, the Tower of Hanoi problem can be visualized as a recursive tree of height $n$. Each node represents a subproblem, corresponding to a call to `dfs()`, **Hence, the time complexity is $O(2^n)$, and the space complexity is $O(n)$.**
|
||||
|
||||
{ class="animation-figure" }
|
||||
|
||||
@ -313,6 +313,6 @@ As shown in Figure 12-15, the Tower of Hanoi forms a recursive tree with a heigh
|
||||
|
||||
!!! quote
|
||||
|
||||
The Tower of Hanoi originates from an ancient legend. In a temple in ancient India, monks had three tall diamond pillars and $64$ differently sized golden discs. The monks continuously moved the discs, believing that when the last disc is correctly placed, the world would end.
|
||||
The Tower of Hanoi originates from an ancient legend. In a temple in ancient India, monks had three tall diamond pillars and $64$ differently sized golden discs. They believed that when the last disc was correctly placed, the world would end.
|
||||
|
||||
However, even if the monks moved a disc every second, it would take about $2^{64} \approx 1.84×10^{19}$ seconds, approximately 585 billion years, far exceeding current estimates of the age of the universe. Thus, if the legend is true, we probably do not need to worry about the world ending.
|
||||
However, even if the monks moved one disc every second, it would take about $2^{64} \approx 1.84×10^{19}$ —approximately 585 billion years—far exceeding current estimates of the age of the universe. Thus, if the legend is true, we probably do not need to worry about the world ending.
|
||||
|
@ -9,9 +9,9 @@ icon: material/set-split
|
||||
|
||||
!!! abstract
|
||||
|
||||
Difficult problems are decomposed layer by layer, each decomposition making them simpler.
|
||||
Difficult problems are decomposed layer by layer, with each decomposition making them simpler.
|
||||
|
||||
Divide and conquer reveals an important truth: start with simplicity, and nothing is complex anymore.
|
||||
Divide and conquer unveils a profound truth: begin with simplicity, and complexity dissolves.
|
||||
|
||||
## Chapter contents
|
||||
|
||||
|
@ -4,12 +4,12 @@ comments: true
|
||||
|
||||
# 12.5 Summary
|
||||
|
||||
- Divide and conquer is a common algorithm design strategy, which includes dividing (partitioning) and conquering (merging) two stages, usually implemented based on recursion.
|
||||
- The basis for judging whether it is a divide and conquer algorithm problem includes: whether the problem can be decomposed, whether the subproblems are independent, and whether the subproblems can be merged.
|
||||
- Merge sort is a typical application of the divide and conquer strategy, which recursively divides the array into two equal-length subarrays until only one element remains, and then starts merging layer by layer to complete the sorting.
|
||||
- Introducing the divide and conquer strategy can often improve algorithm efficiency. On one hand, the divide and conquer strategy reduces the number of operations; on the other hand, it is conducive to parallel optimization of the system after division.
|
||||
- Divide and conquer can solve many algorithm problems and is widely used in data structure and algorithm design, where its presence is ubiquitous.
|
||||
- Compared to brute force search, adaptive search is more efficient. Search algorithms with a time complexity of $O(\log n)$ are usually based on the divide and conquer strategy.
|
||||
- Binary search is another typical application of the divide and conquer strategy, which does not include the step of merging the solutions of subproblems. We can implement binary search through recursive divide and conquer.
|
||||
- In the problem of constructing binary trees, building the tree (original problem) can be divided into building the left and right subtree (subproblems), which can be achieved by partitioning the index intervals of the pre-order and in-order traversals.
|
||||
- In the Tower of Hanoi problem, a problem of size $n$ can be divided into two subproblems of size $n-1$ and one subproblem of size $1$. By solving these three subproblems in sequence, the original problem is consequently resolved.
|
||||
- Divide and conquer is a common algorithm design strategy that consists of two stages—divide (partition) and conquer (merge)—and is generally implemented using recursion.
|
||||
- To determine whether a problem is suited for a divide and conquer approach, we check if the problem can be decomposed, whether the subproblems are independent, and whether the subproblems can be merged.
|
||||
- Merge sort is a typical example of the divide and conquer strategy. It recursively splits an array into two equal-length subarrays until only one element remains, and then merges these subarrays layer by layer to complete the sorting.
|
||||
- Introducing the divide and conquer strategy often improves algorithm efficiency. On one hand, it reduces the number of operations; on the other hand, it facilitates parallel optimization of the system after division.
|
||||
- Divide and conquer can be applied to numerous algorithmic problems and is widely used in data structures and algorithm design, appearing in many scenarios.
|
||||
- Compared to brute force search, adaptive search is more efficient. Search algorithms with a time complexity of $O(\log n)$ are typically based on the divide and conquer strategy.
|
||||
- Binary search is another classic application of the divide-and-conquer strategy. It does not involve merging subproblem solutions and can be implemented via a recursive divide-and-conquer approach.
|
||||
- In the problem of constructing binary trees, building the tree (the original problem) can be divided into building the left subtree and right subtree (the subproblems). This can be achieved by partitioning the index ranges of the preorder and inorder traversals.
|
||||
- In the Tower of Hanoi problem, a problem of size $n$ can be broken down into two subproblems of size $n-1$ and one subproblem of size $1$. By solving these three subproblems in sequence, the original problem is resolved.
|
||||
|
@ -11,7 +11,7 @@ icon: material/table-pivot
|
||||
|
||||
Streams merge into rivers, and rivers merge into the sea.
|
||||
|
||||
Dynamic programming combines the solutions of small problems to solve bigger problems, step by step leading us to the solution.
|
||||
Dynamic programming weaves smaller problems’ solutions into larger ones, guiding us step by step toward the far shore—where the ultimate answer awaits.
|
||||
|
||||
## Chapter contents
|
||||
|
||||
|
@ -4,28 +4,28 @@ comments: true
|
||||
|
||||
# 8.1 Heap
|
||||
|
||||
A <u>heap</u> is a complete binary tree that satisfies specific conditions and can be mainly divided into two types, as shown in Figure 8-1.
|
||||
A <u>heap</u> is a complete binary tree that satisfies specific conditions and can be mainly categorized into two types, as shown in Figure 8-1.
|
||||
|
||||
- <u>Min heap</u>: The value of any node $\leq$ the values of its child nodes.
|
||||
- <u>Max heap</u>: The value of any node $\geq$ the values of its child nodes.
|
||||
- <u>min heap</u>: The value of any node $\leq$ the values of its child nodes.
|
||||
- <u>max heap</u>: The value of any node $\geq$ the values of its child nodes.
|
||||
|
||||
{ class="animation-figure" }
|
||||
|
||||
<p align="center"> Figure 8-1 Min heap and max heap </p>
|
||||
|
||||
As a special case of a complete binary tree, heaps have the following characteristics:
|
||||
As a special case of a complete binary tree, a heap has the following characteristics:
|
||||
|
||||
- The bottom layer nodes are filled from left to right, and nodes in other layers are fully filled.
|
||||
- The root node of the binary tree is called the "heap top," and the bottom-rightmost node is called the "heap bottom."
|
||||
- For max heaps (min heaps), the value of the heap top element (root node) is the largest (smallest).
|
||||
- The root node of the binary tree is called the "top" of the heap, and the bottom-rightmost node is called the "bottom" of the heap.
|
||||
- For max heaps (min heaps), the value of the top element (root) is the largest (smallest) among all elements.
|
||||
|
||||
## 8.1.1 Common operations on heaps
|
||||
## 8.1.1 Common heap operations
|
||||
|
||||
It should be noted that many programming languages provide a <u>priority queue</u>, which is an abstract data structure defined as a queue with priority sorting.
|
||||
|
||||
In fact, **heaps are often used to implement priority queues, with max heaps equivalent to priority queues where elements are dequeued in descending order**. From a usage perspective, we can consider "priority queue" and "heap" as equivalent data structures. Therefore, this book does not make a special distinction between the two, uniformly referring to them as "heap."
|
||||
In practice, **heaps are often used to implement priority queues. A max heap corresponds to a priority queue where elements are dequeued in descending order**. From a usage perspective, we can consider "priority queue" and "heap" as equivalent data structures. Therefore, this book does not make a special distinction between the two, uniformly referring to them as "heap."
|
||||
|
||||
Common operations on heaps are shown in Table 8-1, and the method names depend on the programming language.
|
||||
Common operations on heaps are shown in Table 8-1, and the method names may vary based on the programming language.
|
||||
|
||||
<p align="center"> Table 8-1 Efficiency of Heap Operations </p>
|
||||
|
||||
@ -43,45 +43,45 @@ Common operations on heaps are shown in Table 8-1, and the method names depend o
|
||||
|
||||
In practice, we can directly use the heap class (or priority queue class) provided by programming languages.
|
||||
|
||||
Similar to sorting algorithms where we have "ascending order" and "descending order," we can switch between "min heap" and "max heap" by setting a `flag` or modifying the `Comparator`. The code is as follows:
|
||||
Similar to sorting algorithms where we have "ascending order" and "descending order", we can switch between "min heap" and "max heap" by setting a `flag` or modifying the `Comparator`. The code is as follows:
|
||||
|
||||
=== "Python"
|
||||
|
||||
```python title="heap.py"
|
||||
# 初始化小顶堆
|
||||
# Initialize a min heap
|
||||
min_heap, flag = [], 1
|
||||
# 初始化大顶堆
|
||||
# Initialize a max heap
|
||||
max_heap, flag = [], -1
|
||||
|
||||
# Python 的 heapq 模块默认实现小顶堆
|
||||
# 考虑将“元素取负”后再入堆,这样就可以将大小关系颠倒,从而实现大顶堆
|
||||
# 在本示例中,flag = 1 时对应小顶堆,flag = -1 时对应大顶堆
|
||||
# Python's heapq module implements a min heap by default
|
||||
# By negating the elements before pushing them to the heap, we invert the order and thus implement a max heap
|
||||
# In this example, flag = 1 corresponds to a min heap, while flag = -1 corresponds to a max heap
|
||||
|
||||
# 元素入堆
|
||||
# Push elements into the heap
|
||||
heapq.heappush(max_heap, flag * 1)
|
||||
heapq.heappush(max_heap, flag * 3)
|
||||
heapq.heappush(max_heap, flag * 2)
|
||||
heapq.heappush(max_heap, flag * 5)
|
||||
heapq.heappush(max_heap, flag * 4)
|
||||
|
||||
# 获取堆顶元素
|
||||
# Retrieve the top element of the heap
|
||||
peek: int = flag * max_heap[0] # 5
|
||||
|
||||
# 堆顶元素出堆
|
||||
# 出堆元素会形成一个从大到小的序列
|
||||
# Pop the top element of the heap
|
||||
# The popped elements will form a sequence in descending order
|
||||
val = flag * heapq.heappop(max_heap) # 5
|
||||
val = flag * heapq.heappop(max_heap) # 4
|
||||
val = flag * heapq.heappop(max_heap) # 3
|
||||
val = flag * heapq.heappop(max_heap) # 2
|
||||
val = flag * heapq.heappop(max_heap) # 1
|
||||
|
||||
# 获取堆大小
|
||||
# Get the size of the heap
|
||||
size: int = len(max_heap)
|
||||
|
||||
# 判断堆是否为空
|
||||
# Check if the heap is empty
|
||||
is_empty: bool = not max_heap
|
||||
|
||||
# 输入列表并建堆
|
||||
# Create a heap from a list
|
||||
min_heap: list[int] = [1, 3, 2, 5, 4]
|
||||
heapq.heapify(min_heap)
|
||||
```
|
||||
@ -89,37 +89,37 @@ Similar to sorting algorithms where we have "ascending order" and "descending or
|
||||
=== "C++"
|
||||
|
||||
```cpp title="heap.cpp"
|
||||
/* 初始化堆 */
|
||||
// 初始化小顶堆
|
||||
/* Initialize a heap */
|
||||
// Initialize a min heap
|
||||
priority_queue<int, vector<int>, greater<int>> minHeap;
|
||||
// 初始化大顶堆
|
||||
// Initialize a max heap
|
||||
priority_queue<int, vector<int>, less<int>> maxHeap;
|
||||
|
||||
/* 元素入堆 */
|
||||
/* Push elements into the heap */
|
||||
maxHeap.push(1);
|
||||
maxHeap.push(3);
|
||||
maxHeap.push(2);
|
||||
maxHeap.push(5);
|
||||
maxHeap.push(4);
|
||||
|
||||
/* 获取堆顶元素 */
|
||||
/* Retrieve the top element of the heap */
|
||||
int peek = maxHeap.top(); // 5
|
||||
|
||||
/* 堆顶元素出堆 */
|
||||
// 出堆元素会形成一个从大到小的序列
|
||||
/* Pop the top element of the heap */
|
||||
// The popped elements will form a sequence in descending order
|
||||
maxHeap.pop(); // 5
|
||||
maxHeap.pop(); // 4
|
||||
maxHeap.pop(); // 3
|
||||
maxHeap.pop(); // 2
|
||||
maxHeap.pop(); // 1
|
||||
|
||||
/* 获取堆大小 */
|
||||
/* Get the size of the heap */
|
||||
int size = maxHeap.size();
|
||||
|
||||
/* 判断堆是否为空 */
|
||||
/* Check if the heap is empty */
|
||||
bool isEmpty = maxHeap.empty();
|
||||
|
||||
/* 输入列表并建堆 */
|
||||
/* Create a heap from a list */
|
||||
vector<int> input{1, 3, 2, 5, 4};
|
||||
priority_queue<int, vector<int>, greater<int>> minHeap(input.begin(), input.end());
|
||||
```
|
||||
@ -127,206 +127,206 @@ Similar to sorting algorithms where we have "ascending order" and "descending or
|
||||
=== "Java"
|
||||
|
||||
```java title="heap.java"
|
||||
/* 初始化堆 */
|
||||
// 初始化小顶堆
|
||||
/* Initialize a heap */
|
||||
// Initialize a min heap
|
||||
Queue<Integer> minHeap = new PriorityQueue<>();
|
||||
// 初始化大顶堆(使用 lambda 表达式修改 Comparator 即可)
|
||||
// Initialize a max heap (Simply modify the Comparator using a lambda expression)
|
||||
Queue<Integer> maxHeap = new PriorityQueue<>((a, b) -> b - a);
|
||||
|
||||
/* 元素入堆 */
|
||||
/* Push elements into the heap */
|
||||
maxHeap.offer(1);
|
||||
maxHeap.offer(3);
|
||||
maxHeap.offer(2);
|
||||
maxHeap.offer(5);
|
||||
maxHeap.offer(4);
|
||||
|
||||
/* 获取堆顶元素 */
|
||||
/* Retrieve the top element of the heap */
|
||||
int peek = maxHeap.peek(); // 5
|
||||
|
||||
/* 堆顶元素出堆 */
|
||||
// 出堆元素会形成一个从大到小的序列
|
||||
/* Pop the top element of the heap */
|
||||
// The popped elements will form a sequence in descending order
|
||||
peek = maxHeap.poll(); // 5
|
||||
peek = maxHeap.poll(); // 4
|
||||
peek = maxHeap.poll(); // 3
|
||||
peek = maxHeap.poll(); // 2
|
||||
peek = maxHeap.poll(); // 1
|
||||
|
||||
/* 获取堆大小 */
|
||||
/* Get the size of the heap */
|
||||
int size = maxHeap.size();
|
||||
|
||||
/* 判断堆是否为空 */
|
||||
/* Check if the heap is empty */
|
||||
boolean isEmpty = maxHeap.isEmpty();
|
||||
|
||||
/* 输入列表并建堆 */
|
||||
/* Create a heap from a list */
|
||||
minHeap = new PriorityQueue<>(Arrays.asList(1, 3, 2, 5, 4));
|
||||
```
|
||||
|
||||
=== "C#"
|
||||
|
||||
```csharp title="heap.cs"
|
||||
/* 初始化堆 */
|
||||
// 初始化小顶堆
|
||||
/* Initialize a heap */
|
||||
// Initialize a min heap
|
||||
PriorityQueue<int, int> minHeap = new();
|
||||
// 初始化大顶堆(使用 lambda 表达式修改 Comparator 即可)
|
||||
// Initialize a max heap (Simply modify the Comparator using a lambda expression)
|
||||
PriorityQueue<int, int> maxHeap = new(Comparer<int>.Create((x, y) => y - x));
|
||||
|
||||
/* 元素入堆 */
|
||||
/* Push elements into the heap */
|
||||
maxHeap.Enqueue(1, 1);
|
||||
maxHeap.Enqueue(3, 3);
|
||||
maxHeap.Enqueue(2, 2);
|
||||
maxHeap.Enqueue(5, 5);
|
||||
maxHeap.Enqueue(4, 4);
|
||||
|
||||
/* 获取堆顶元素 */
|
||||
/* Retrieve the top element of the heap */
|
||||
int peek = maxHeap.Peek();//5
|
||||
|
||||
/* 堆顶元素出堆 */
|
||||
// 出堆元素会形成一个从大到小的序列
|
||||
/* Pop the top element of the heap */
|
||||
// The popped elements will form a sequence in descending order
|
||||
peek = maxHeap.Dequeue(); // 5
|
||||
peek = maxHeap.Dequeue(); // 4
|
||||
peek = maxHeap.Dequeue(); // 3
|
||||
peek = maxHeap.Dequeue(); // 2
|
||||
peek = maxHeap.Dequeue(); // 1
|
||||
|
||||
/* 获取堆大小 */
|
||||
/* Get the size of the heap */
|
||||
int size = maxHeap.Count;
|
||||
|
||||
/* 判断堆是否为空 */
|
||||
/* Check if the heap is empty */
|
||||
bool isEmpty = maxHeap.Count == 0;
|
||||
|
||||
/* 输入列表并建堆 */
|
||||
/* Create a heap from a list */
|
||||
minHeap = new PriorityQueue<int, int>([(1, 1), (3, 3), (2, 2), (5, 5), (4, 4)]);
|
||||
```
|
||||
|
||||
=== "Go"
|
||||
|
||||
```go title="heap.go"
|
||||
// Go 语言中可以通过实现 heap.Interface 来构建整数大顶堆
|
||||
// 实现 heap.Interface 需要同时实现 sort.Interface
|
||||
// In Go, we can construct a max heap of integers by implementing heap.Interface
|
||||
// Note that implementing heap.Interface requires also implementing sort.Interface
|
||||
type intHeap []any
|
||||
|
||||
// Push heap.Interface 的方法,实现推入元素到堆
|
||||
// Push method of heap.Interface, which pushes an element into the heap
|
||||
func (h *intHeap) Push(x any) {
|
||||
// Push 和 Pop 使用 pointer receiver 作为参数
|
||||
// 因为它们不仅会对切片的内容进行调整,还会修改切片的长度。
|
||||
// Both Push and Pop use a pointer receiver
|
||||
// because they not only adjust the elements of the slice but also change its length
|
||||
*h = append(*h, x.(int))
|
||||
}
|
||||
|
||||
// Pop heap.Interface 的方法,实现弹出堆顶元素
|
||||
// Pop method of heap.Interface, which removes the top element of the heap
|
||||
func (h *intHeap) Pop() any {
|
||||
// 待出堆元素存放在最后
|
||||
// The element to pop from the heap is stored at the end
|
||||
last := (*h)[len(*h)-1]
|
||||
*h = (*h)[:len(*h)-1]
|
||||
return last
|
||||
}
|
||||
|
||||
// Len sort.Interface 的方法
|
||||
// Len method of sort.Interface
|
||||
func (h *intHeap) Len() int {
|
||||
return len(*h)
|
||||
}
|
||||
|
||||
// Less sort.Interface 的方法
|
||||
// Less method of sort.Interface
|
||||
func (h *intHeap) Less(i, j int) bool {
|
||||
// 如果实现小顶堆,则需要调整为小于号
|
||||
// If you want to implement a min heap, you would change this to a less-than comparison
|
||||
return (*h)[i].(int) > (*h)[j].(int)
|
||||
}
|
||||
|
||||
// Swap sort.Interface 的方法
|
||||
// Swap method of sort.Interface
|
||||
func (h *intHeap) Swap(i, j int) {
|
||||
(*h)[i], (*h)[j] = (*h)[j], (*h)[i]
|
||||
}
|
||||
|
||||
// Top 获取堆顶元素
|
||||
// Top Retrieve the top element of the heap
|
||||
func (h *intHeap) Top() any {
|
||||
return (*h)[0]
|
||||
}
|
||||
|
||||
/* Driver Code */
|
||||
func TestHeap(t *testing.T) {
|
||||
/* 初始化堆 */
|
||||
// 初始化大顶堆
|
||||
/* Initialize a heap */
|
||||
// Initialize a max heap
|
||||
maxHeap := &intHeap{}
|
||||
heap.Init(maxHeap)
|
||||
/* 元素入堆 */
|
||||
// 调用 heap.Interface 的方法,来添加元素
|
||||
/* Push elements into the heap */
|
||||
// Call the methods of heap.Interface to add elements
|
||||
heap.Push(maxHeap, 1)
|
||||
heap.Push(maxHeap, 3)
|
||||
heap.Push(maxHeap, 2)
|
||||
heap.Push(maxHeap, 4)
|
||||
heap.Push(maxHeap, 5)
|
||||
|
||||
/* 获取堆顶元素 */
|
||||
/* Retrieve the top element of the heap */
|
||||
top := maxHeap.Top()
|
||||
fmt.Printf("堆顶元素为 %d\n", top)
|
||||
fmt.Printf("The top element of the heap is %d\n", top)
|
||||
|
||||
/* 堆顶元素出堆 */
|
||||
// 调用 heap.Interface 的方法,来移除元素
|
||||
/* Pop the top element of the heap */
|
||||
// Call the methods of heap.Interface to remove elements
|
||||
heap.Pop(maxHeap) // 5
|
||||
heap.Pop(maxHeap) // 4
|
||||
heap.Pop(maxHeap) // 3
|
||||
heap.Pop(maxHeap) // 2
|
||||
heap.Pop(maxHeap) // 1
|
||||
|
||||
/* 获取堆大小 */
|
||||
/* Get the size of the heap */
|
||||
size := len(*maxHeap)
|
||||
fmt.Printf("堆元素数量为 %d\n", size)
|
||||
fmt.Printf("The number of elements in the heap is %d\n", size)
|
||||
|
||||
/* 判断堆是否为空 */
|
||||
/* Check if the heap is empty */
|
||||
isEmpty := len(*maxHeap) == 0
|
||||
fmt.Printf("堆是否为空 %t\n", isEmpty)
|
||||
fmt.Printf("Is the heap empty? %t\n", isEmpty)
|
||||
}
|
||||
```
|
||||
|
||||
=== "Swift"
|
||||
|
||||
```swift title="heap.swift"
|
||||
/* 初始化堆 */
|
||||
// Swift 的 Heap 类型同时支持最大堆和最小堆,且需要引入 swift-collections
|
||||
/* Initialize a heap */
|
||||
// Swift’s Heap type supports both max heaps and min heaps, and need the swift-collections library
|
||||
var heap = Heap<Int>()
|
||||
|
||||
/* 元素入堆 */
|
||||
/* Push elements into the heap */
|
||||
heap.insert(1)
|
||||
heap.insert(3)
|
||||
heap.insert(2)
|
||||
heap.insert(5)
|
||||
heap.insert(4)
|
||||
|
||||
/* 获取堆顶元素 */
|
||||
/* Retrieve the top element of the heap */
|
||||
var peek = heap.max()!
|
||||
|
||||
/* 堆顶元素出堆 */
|
||||
/* Pop the top element of the heap */
|
||||
peek = heap.removeMax() // 5
|
||||
peek = heap.removeMax() // 4
|
||||
peek = heap.removeMax() // 3
|
||||
peek = heap.removeMax() // 2
|
||||
peek = heap.removeMax() // 1
|
||||
|
||||
/* 获取堆大小 */
|
||||
/* Get the size of the heap */
|
||||
let size = heap.count
|
||||
|
||||
/* 判断堆是否为空 */
|
||||
/* Check if the heap is empty */
|
||||
let isEmpty = heap.isEmpty
|
||||
|
||||
/* 输入列表并建堆 */
|
||||
/* Create a heap from a list */
|
||||
let heap2 = Heap([1, 3, 2, 5, 4])
|
||||
```
|
||||
|
||||
=== "JS"
|
||||
|
||||
```javascript title="heap.js"
|
||||
// JavaScript 未提供内置 Heap 类
|
||||
// JavaScript does not provide a built-in Heap class
|
||||
```
|
||||
|
||||
=== "TS"
|
||||
|
||||
```typescript title="heap.ts"
|
||||
// TypeScript 未提供内置 Heap 类
|
||||
// TypeScript does not provide a built-in Heap class
|
||||
```
|
||||
|
||||
=== "Dart"
|
||||
|
||||
```dart title="heap.dart"
|
||||
// Dart 未提供内置 Heap 类
|
||||
// Dart does not provide a built-in Heap class
|
||||
```
|
||||
|
||||
=== "Rust"
|
||||
@ -335,80 +335,80 @@ Similar to sorting algorithms where we have "ascending order" and "descending or
|
||||
use std::collections::BinaryHeap;
|
||||
use std::cmp::Reverse;
|
||||
|
||||
/* 初始化堆 */
|
||||
// 初始化小顶堆
|
||||
/* Initialize a heap */
|
||||
// Initialize a min heap
|
||||
let mut min_heap = BinaryHeap::<Reverse<i32>>::new();
|
||||
// 初始化大顶堆
|
||||
// Initialize a max heap
|
||||
let mut max_heap = BinaryHeap::new();
|
||||
|
||||
/* 元素入堆 */
|
||||
/* Push elements into the heap */
|
||||
max_heap.push(1);
|
||||
max_heap.push(3);
|
||||
max_heap.push(2);
|
||||
max_heap.push(5);
|
||||
max_heap.push(4);
|
||||
|
||||
/* 获取堆顶元素 */
|
||||
/* Retrieve the top element of the heap */
|
||||
let peek = max_heap.peek().unwrap(); // 5
|
||||
|
||||
/* 堆顶元素出堆 */
|
||||
// 出堆元素会形成一个从大到小的序列
|
||||
/* Pop the top element of the heap */
|
||||
// The popped elements will form a sequence in descending order
|
||||
let peek = max_heap.pop().unwrap(); // 5
|
||||
let peek = max_heap.pop().unwrap(); // 4
|
||||
let peek = max_heap.pop().unwrap(); // 3
|
||||
let peek = max_heap.pop().unwrap(); // 2
|
||||
let peek = max_heap.pop().unwrap(); // 1
|
||||
|
||||
/* 获取堆大小 */
|
||||
/* Get the size of the heap */
|
||||
let size = max_heap.len();
|
||||
|
||||
/* 判断堆是否为空 */
|
||||
/* Check if the heap is empty */
|
||||
let is_empty = max_heap.is_empty();
|
||||
|
||||
/* 输入列表并建堆 */
|
||||
/* Create a heap from a list */
|
||||
let min_heap = BinaryHeap::from(vec![Reverse(1), Reverse(3), Reverse(2), Reverse(5), Reverse(4)]);
|
||||
```
|
||||
|
||||
=== "C"
|
||||
|
||||
```c title="heap.c"
|
||||
// C 未提供内置 Heap 类
|
||||
// C does not provide a built-in Heap class
|
||||
```
|
||||
|
||||
=== "Kotlin"
|
||||
|
||||
```kotlin title="heap.kt"
|
||||
/* 初始化堆 */
|
||||
// 初始化小顶堆
|
||||
/* Initialize a heap */
|
||||
// Initialize a min heap
|
||||
var minHeap = PriorityQueue<Int>()
|
||||
// 初始化大顶堆(使用 lambda 表达式修改 Comparator 即可)
|
||||
// Initialize a max heap (Simply modify the Comparator using a lambda expression)
|
||||
val maxHeap = PriorityQueue { a: Int, b: Int -> b - a }
|
||||
|
||||
/* 元素入堆 */
|
||||
/* Push elements into the heap */
|
||||
maxHeap.offer(1)
|
||||
maxHeap.offer(3)
|
||||
maxHeap.offer(2)
|
||||
maxHeap.offer(5)
|
||||
maxHeap.offer(4)
|
||||
|
||||
/* 获取堆顶元素 */
|
||||
/* Retrieve the top element of the heap */
|
||||
var peek = maxHeap.peek() // 5
|
||||
|
||||
/* 堆顶元素出堆 */
|
||||
// 出堆元素会形成一个从大到小的序列
|
||||
/* Pop the top element of the heap */
|
||||
// The popped elements will form a sequence in descending order
|
||||
peek = maxHeap.poll() // 5
|
||||
peek = maxHeap.poll() // 4
|
||||
peek = maxHeap.poll() // 3
|
||||
peek = maxHeap.poll() // 2
|
||||
peek = maxHeap.poll() // 1
|
||||
|
||||
/* 获取堆大小 */
|
||||
/* Get the size of the heap */
|
||||
val size = maxHeap.size
|
||||
|
||||
/* 判断堆是否为空 */
|
||||
/* Check if the heap is empty */
|
||||
val isEmpty = maxHeap.isEmpty()
|
||||
|
||||
/* 输入列表并建堆 */
|
||||
/* Create a heap from a list */
|
||||
minHeap = PriorityQueue(mutableListOf(1, 3, 2, 5, 4))
|
||||
```
|
||||
|
||||
@ -424,17 +424,17 @@ Similar to sorting algorithms where we have "ascending order" and "descending or
|
||||
|
||||
```
|
||||
|
||||
??? pythontutor "可视化运行"
|
||||
??? pythontutor "Code visualization"
|
||||
|
||||
https://pythontutor.com/render.html#code=import%20heapq%0A%0A%22%22%22Driver%20Code%22%22%22%0Aif%20__name__%20%3D%3D%20%22__main__%22%3A%0A%20%20%20%20%23%20%E5%88%9D%E5%A7%8B%E5%8C%96%E5%B0%8F%E9%A1%B6%E5%A0%86%0A%20%20%20%20min_heap,%20flag%20%3D%20%5B%5D,%201%0A%20%20%20%20%23%20%E5%88%9D%E5%A7%8B%E5%8C%96%E5%A4%A7%E9%A1%B6%E5%A0%86%0A%20%20%20%20max_heap,%20flag%20%3D%20%5B%5D,%20-1%0A%20%20%20%20%0A%20%20%20%20%23%20Python%20%E7%9A%84%20heapq%20%E6%A8%A1%E5%9D%97%E9%BB%98%E8%AE%A4%E5%AE%9E%E7%8E%B0%E5%B0%8F%E9%A1%B6%E5%A0%86%0A%20%20%20%20%23%20%E8%80%83%E8%99%91%E5%B0%86%E2%80%9C%E5%85%83%E7%B4%A0%E5%8F%96%E8%B4%9F%E2%80%9D%E5%90%8E%E5%86%8D%E5%85%A5%E5%A0%86%EF%BC%8C%E8%BF%99%E6%A0%B7%E5%B0%B1%E5%8F%AF%E4%BB%A5%E5%B0%86%E5%A4%A7%E5%B0%8F%E5%85%B3%E7%B3%BB%E9%A2%A0%E5%80%92%EF%BC%8C%E4%BB%8E%E8%80%8C%E5%AE%9E%E7%8E%B0%E5%A4%A7%E9%A1%B6%E5%A0%86%0A%20%20%20%20%23%20%E5%9C%A8%E6%9C%AC%E7%A4%BA%E4%BE%8B%E4%B8%AD%EF%BC%8Cflag%20%3D%201%20%E6%97%B6%E5%AF%B9%E5%BA%94%E5%B0%8F%E9%A1%B6%E5%A0%86%EF%BC%8Cflag%20%3D%20-1%20%E6%97%B6%E5%AF%B9%E5%BA%94%E5%A4%A7%E9%A1%B6%E5%A0%86%0A%20%20%20%20%0A%20%20%20%20%23%20%E5%85%83%E7%B4%A0%E5%85%A5%E5%A0%86%0A%20%20%20%20heapq.heappush%28max_heap,%20flag%20*%201%29%0A%20%20%20%20heapq.heappush%28max_heap,%20flag%20*%203%29%0A%20%20%20%20heapq.heappush%28max_heap,%20flag%20*%202%29%0A%20%20%20%20heapq.heappush%28max_heap,%20flag%20*%205%29%0A%20%20%20%20heapq.heappush%28max_heap,%20flag%20*%204%29%0A%20%20%20%20%0A%20%20%20%20%23%20%E8%8E%B7%E5%8F%96%E5%A0%86%E9%A1%B6%E5%85%83%E7%B4%A0%0A%20%20%20%20peek%20%3D%20flag%20*%20max_heap%5B0%5D%20%23%205%0A%20%20%20%20%0A%20%20%20%20%23%20%E5%A0%86%E9%A1%B6%E5%85%83%E7%B4%A0%E5%87%BA%E5%A0%86%0A%20%20%20%20%23%20%E5%87%BA%E5%A0%86%E5%85%83%E7%B4%A0%E4%BC%9A%E5%BD%A2%E6%88%90%E4%B8%80%E4%B8%AA%E4%BB%8E%E5%A4%A7%E5%88%B0%E5%B0%8F%E7%9A%84%E5%BA%8F%E5%88%97%0A%20%20%20%20val%20%3D%20flag%20*%20heapq.heappop%28max_heap%29%20%23%205%0A%20%20%20%20val%20%3D%20flag%20*%20heapq.heappop%28max_heap%29%20%23%204%0A%20%20%20%20val%20%3D%20flag%20*%20heapq.heappop%28max_heap%29%20%23%203%0A%20%20%20%20val%20%3D%20flag%20*%20heapq.heappop%28max_heap%29%20%23%202%0A%20%20%20%20val%20%3D%20flag%20*%20heapq.heappop%28max_heap%29%20%23%201%0A%20%20%20%20%0A%20%20%20%20%23%20%E8%8E%B7%E5%8F%96%E5%A0%86%E5%A4%A7%E5%B0%8F%0A%20%20%20%20size%20%3D%20len%28max_heap%29%0A%20%20%20%20%0A%20%20%20%20%23%20%E5%88%A4%E6%96%AD%E5%A0%86%E6%98%AF%E5%90%A6%E4%B8%BA%E7%A9%BA%0A%20%20%20%20is_empty%20%3D%20not%20max_heap%0A%20%20%20%20%0A%20%20%20%20%23%20%E8%BE%93%E5%85%A5%E5%88%97%E8%A1%A8%E5%B9%B6%E5%BB%BA%E5%A0%86%0A%20%20%20%20min_heap%20%3D%20%5B1,%203,%202,%205,%204%5D%0A%20%20%20%20heapq.heapify%28min_heap%29&cumulative=false&curInstr=3&heapPrimitives=nevernest&mode=display&origin=opt-frontend.js&py=311&rawInputLstJSON=%5B%5D&textReferences=false
|
||||
|
||||
## 8.1.2 Implementation of heaps
|
||||
## 8.1.2 Implementation of the heap
|
||||
|
||||
The following implementation is of a max heap. To convert it into a min heap, simply invert all size logic comparisons (for example, replace $\geq$ with $\leq$). Interested readers are encouraged to implement it on their own.
|
||||
|
||||
### 1. Storage and representation of heaps
|
||||
### 1. Heap storage and representation
|
||||
|
||||
As mentioned in the "Binary Trees" section, complete binary trees are well-suited for array representation. Since heaps are a type of complete binary tree, **we will use arrays to store heaps**.
|
||||
As mentioned in the "Binary Trees" section, complete binary trees are highly suitable for array representation. Since heaps are a type of complete binary tree, **we will use arrays to store heaps**.
|
||||
|
||||
When using an array to represent a binary tree, elements represent node values, and indexes represent node positions in the binary tree. **Node pointers are implemented through an index mapping formula**.
|
||||
|
||||
@ -708,9 +708,9 @@ The top element of the heap is the root node of the binary tree, which is also t
|
||||
|
||||
### 3. Inserting an element into the heap
|
||||
|
||||
Given an element `val`, we first add it to the bottom of the heap. After addition, since `val` may be larger than other elements in the heap, the heap's integrity might be compromised, **thus it's necessary to repair the path from the inserted node to the root node**. This operation is called <u>heapifying</u>.
|
||||
Given an element `val`, we first add it to the bottom of the heap. After addition, since `val` may be larger than other elements in the heap, the heap's integrity might be compromised, **thus it's necessary to repair the path from the inserted node to the root node**. This operation is called <u>heapify</u>.
|
||||
|
||||
Considering starting from the node inserted, **perform heapify from bottom to top**. As shown in Figure 8-3, we compare the value of the inserted node with its parent node, and if the inserted node is larger, we swap them. Then continue this operation, repairing each node in the heap from bottom to top until passing the root node or encountering a node that does not need to be swapped.
|
||||
Considering starting from the node inserted, **perform heapify from bottom to top**. As shown in Figure 8-3, we compare the value of the inserted node with its parent node, and if the inserted node is larger, we swap them. Then continue this operation, repairing each node in the heap from bottom to top until reaching the root or a node that does not need swapping.
|
||||
|
||||
=== "<1>"
|
||||
{ class="animation-figure" }
|
||||
@ -911,13 +911,13 @@ Given a total of $n$ nodes, the height of the tree is $O(\log n)$. Hence, the lo
|
||||
|
||||
### 4. Removing the top element from the heap
|
||||
|
||||
The top element of the heap is the root node of the binary tree, that is, the first element of the list. If we directly remove the first element from the list, all node indexes in the binary tree would change, making it difficult to use heapify for repairs subsequently. To minimize changes in element indexes, we use the following steps.
|
||||
The top element of the heap is the root node of the binary tree, that is, the first element of the list. If we directly remove the first element from the list, all node indexes in the binary tree will change, making it difficult to use heapify for subsequent repairs. To minimize changes in element indexes, we use the following steps.
|
||||
|
||||
1. Swap the top element with the bottom element of the heap (swap the root node with the rightmost leaf node).
|
||||
2. After swapping, remove the bottom of the heap from the list (note, since it has been swapped, what is actually being removed is the original top element).
|
||||
2. After swapping, remove the bottom of the heap from the list (note that since it has been swapped, the original top element is actually being removed).
|
||||
3. Starting from the root node, **perform heapify from top to bottom**.
|
||||
|
||||
As shown in Figure 8-4, **the direction of "heapify from top to bottom" is opposite to "heapify from bottom to top"**. We compare the value of the root node with its two children and swap it with the largest child. Then repeat this operation until passing the leaf node or encountering a node that does not need to be swapped.
|
||||
As shown in Figure 8-4, **the direction of "heapify from top to bottom" is opposite to "heapify from bottom to top"**. We compare the value of the root node with its two children and swap it with the largest child. Then, repeat this operation until reaching the leaf node or encountering a node that does not need swapping.
|
||||
|
||||
=== "<1>"
|
||||
{ class="animation-figure" }
|
||||
@ -1153,5 +1153,5 @@ Similar to the element insertion operation, the time complexity of the top eleme
|
||||
## 8.1.3 Common applications of heaps
|
||||
|
||||
- **Priority Queue**: Heaps are often the preferred data structure for implementing priority queues, with both enqueue and dequeue operations having a time complexity of $O(\log n)$, and building a queue having a time complexity of $O(n)$, all of which are very efficient.
|
||||
- **Heap Sort**: Given a set of data, we can create a heap from them and then continually perform element removal operations to obtain ordered data. However, we usually use a more elegant method to implement heap sort, as detailed in the "Heap Sort" section.
|
||||
- **Finding the Largest $k$ Elements**: This is a classic algorithm problem and also a typical application, such as selecting the top 10 hot news for Weibo hot search, picking the top 10 selling products, etc.
|
||||
- **Heap Sort**: Given a set of data, we can create a heap from them and then continually perform element removal operations to obtain ordered data. However, there is a more elegant way to implement heap sort, as explained in the "Heap Sort" chapter.
|
||||
- **Finding the Largest $k$ Elements**: This is a classic algorithm problem and also a common use case, such as selecting the top 10 hot news for Weibo hot search, picking the top 10 selling products, etc.
|
||||
|
@ -9,9 +9,9 @@ icon: material/family-tree
|
||||
|
||||
!!! abstract
|
||||
|
||||
The heap is like mountain peaks, stacked and undulating, each with its unique shape.
|
||||
Heaps resemble mountains and their jagged peaks, layered and undulating, each with its unique form.
|
||||
|
||||
Among these peaks, the highest one always catches the eye first.
|
||||
Each mountain peak rises and falls in scattered heights, yet the tallest always captures attention first.
|
||||
|
||||
## Chapter contents
|
||||
|
||||
|
@ -10,9 +10,9 @@ comments: true
|
||||
|
||||
Given a sorted array `nums` of length $n$, which may contain duplicate elements, return the index of the leftmost element `target`. If the element is not present in the array, return $-1$.
|
||||
|
||||
Recall the method of binary search for an insertion point, after the search is completed, $i$ points to the leftmost `target`, **thus searching for the insertion point is essentially searching for the index of the leftmost `target`**.
|
||||
Recalling the method of binary search for an insertion point, after the search is completed, the index $i$ will point to the leftmost occurrence of `target`. Therefore, **searching for the insertion point is essentially the same as finding the index of the leftmost `target`**.
|
||||
|
||||
Consider implementing the search for the left boundary using the function for finding an insertion point. Note that the array might not contain `target`, which could lead to the following two results:
|
||||
We can use the function for finding an insertion point to find the left boundary of `target`. Note that the array might not contain `target`, which could lead to the following two results:
|
||||
|
||||
- The index $i$ of the insertion point is out of bounds.
|
||||
- The element `nums[i]` is not equal to `target`.
|
||||
@ -133,21 +133,21 @@ In these cases, simply return $-1$. The code is as follows:
|
||||
|
||||
## 10.3.2 Find the right boundary
|
||||
|
||||
So how do we find the rightmost `target`? The most straightforward way is to modify the code, replacing the pointer contraction operation in the case of `nums[m] == target`. The code is omitted here, but interested readers can implement it on their own.
|
||||
How do we find the rightmost occurrence of `target`? The most straightforward way is to modify the traditional binary search logic by changing how we adjust the search boundaries in the case of `nums[m] == target`. The code is omitted here. If you are interested, try to implement the code on your own.
|
||||
|
||||
Below we introduce two more cunning methods.
|
||||
Below we are going to introduce two more ingenious methods.
|
||||
|
||||
### 1. Reusing the search for the left boundary
|
||||
### 1. Reuse the left boundary search
|
||||
|
||||
In fact, we can use the function for finding the leftmost element to find the rightmost element, specifically by **transforming the search for the rightmost `target` into a search for the leftmost `target + 1`**.
|
||||
To find the rightmost occurrence of `target`, we can reuse the function used for locating the leftmost `target`. Specifically, we transform the search for the rightmost target into a search for the leftmost target + 1.
|
||||
|
||||
As shown in Figure 10-7, after the search is completed, the pointer $i$ points to the leftmost `target + 1` (if it exists), while $j$ points to the rightmost `target`, **thus returning $j$ is sufficient**.
|
||||
As shown in Figure 10-7, after the search is complete, pointer $i$ will point to the leftmost `target + 1` (if exists), while pointer $j$ will point to the rightmost occurrence of `target`. Therefore, returning $j$ will give us the right boundary.
|
||||
|
||||
{ class="animation-figure" }
|
||||
|
||||
<p align="center"> Figure 10-7 Transforming the search for the right boundary into the search for the left boundary </p>
|
||||
|
||||
Please note, the insertion point returned is $i$, therefore, it should be subtracted by $1$ to obtain $j$:
|
||||
Note that the insertion point returned is $i$, therefore, it should be subtracted by $1$ to obtain $j$:
|
||||
|
||||
=== "Python"
|
||||
|
||||
@ -267,9 +267,9 @@ Please note, the insertion point returned is $i$, therefore, it should be subtra
|
||||
[class]{}-[func]{binarySearchRightEdge}
|
||||
```
|
||||
|
||||
### 2. Transforming into an element search
|
||||
### 2. Transform into an element search
|
||||
|
||||
We know that when the array does not contain `target`, $i$ and $j$ will eventually point to the first element greater and smaller than `target` respectively.
|
||||
When the array does not contain `target`, $i$ and $j$ will eventually point to the first element greater and smaller than `target` respectively.
|
||||
|
||||
Thus, as shown in Figure 10-8, we can construct an element that does not exist in the array, to search for the left and right boundaries.
|
||||
|
||||
@ -280,7 +280,7 @@ Thus, as shown in Figure 10-8, we can construct an element that does not exist i
|
||||
|
||||
<p align="center"> Figure 10-8 Transforming the search for boundaries into the search for an element </p>
|
||||
|
||||
The code is omitted here, but two points are worth noting.
|
||||
The code is omitted here, but here are two important points to note about this approach.
|
||||
|
||||
- The given array does not contain decimals, meaning we do not need to worry about how to handle equal situations.
|
||||
- Since this method introduces decimals, the variable `target` in the function needs to be changed to a floating point type (no change needed in Python).
|
||||
- The given array `nums` does not contain decimal, so handling equal cases is not a concern.
|
||||
- However, introducing decimals in this approach requires modifying the `target` variable to a floating-point type (no change needed in Python).
|
||||
|
@ -10,7 +10,7 @@ Binary search is not only used to search for target elements but also to solve m
|
||||
|
||||
!!! question
|
||||
|
||||
Given an ordered array `nums` of length $n$ and an element `target`, where the array has no duplicate elements. Now insert `target` into the array `nums` while maintaining its order. If the element `target` already exists in the array, insert it to its left side. Please return the index of `target` in the array after insertion. See the example shown in Figure 10-4.
|
||||
Given a sorted array `nums` of length $n$ with unique elements and an element `target`, insert `target` into `nums` while maintaining its sorted order. If `target` already exists in the array, insert it to the left of the existing element. Return the index of `target` in the array after insertion. See the example shown in Figure 10-4.
|
||||
|
||||
{ class="animation-figure" }
|
||||
|
||||
@ -18,15 +18,15 @@ Binary search is not only used to search for target elements but also to solve m
|
||||
|
||||
If you want to reuse the binary search code from the previous section, you need to answer the following two questions.
|
||||
|
||||
**Question one**: When the array contains `target`, is the insertion point index the index of that element?
|
||||
**Question one**: If the array already contains `target`, would the insertion point be the index of existing element?
|
||||
|
||||
The requirement to insert `target` to the left of equal elements means that the newly inserted `target` replaces the original `target` position. Thus, **when the array contains `target`, the insertion point index is the index of that `target`**.
|
||||
The requirement to insert `target` to the left of equal elements means that the newly inserted `target` will replace the original `target` position. In other words, **when the array contains `target`, the insertion point is indeed the index of that `target`**.
|
||||
|
||||
**Question two**: When the array does not contain `target`, what is the index of the insertion point?
|
||||
**Question two**: When the array does not contain `target`, at which index would it be inserted?
|
||||
|
||||
Further consider the binary search process: when `nums[m] < target`, pointer $i$ moves, meaning that pointer $i$ is approaching an element greater than or equal to `target`. Similarly, pointer $j$ is always approaching an element less than or equal to `target`.
|
||||
Let's further consider the binary search process: when `nums[m] < target`, pointer $i$ moves, meaning that pointer $i$ is approaching an element greater than or equal to `target`. Similarly, pointer $j$ is always approaching an element less than or equal to `target`.
|
||||
|
||||
Therefore, at the end of the binary, it is certain that: $i$ points to the first element greater than `target`, and $j$ points to the first element less than `target`. **It is easy to see that when the array does not contain `target`, the insertion index is $i$**. The code is as follows:
|
||||
Therefore, at the end of the binary, it is certain that: $i$ points to the first element greater than `target`, and $j$ points to the first element less than `target`. **It is easy to see that when the array does not contain `target`, the insertion point is $i$**. The code is as follows:
|
||||
|
||||
=== "Python"
|
||||
|
||||
@ -160,12 +160,12 @@ Therefore, at the end of the binary, it is certain that: $i$ points to the first
|
||||
|
||||
Based on the previous question, assume the array may contain duplicate elements, all else remains the same.
|
||||
|
||||
Suppose there are multiple `target`s in the array, ordinary binary search can only return the index of one of the `target`s, **and it cannot determine how many `target`s are to the left and right of that element**.
|
||||
When there are multiple occurrences of `target` in the array, a regular binary search can only return the index of one occurrence of `target`, **and it cannot determine how many occurrences of `target` are to the left and right of that position**.
|
||||
|
||||
The task requires inserting the target element to the very left, **so we need to find the index of the leftmost `target` in the array**. Initially consider implementing this through the steps shown in Figure 10-5.
|
||||
The problem requires inserting the target element at the leftmost position, **so we need to find the index of the leftmost `target` in the array**. Initially consider implementing this through the steps shown in Figure 10-5.
|
||||
|
||||
1. Perform a binary search, get an arbitrary index of `target`, denoted as $k$.
|
||||
2. Start from index $k$, and perform a linear search to the left until the leftmost `target` is found and return.
|
||||
1. Perform a binary search to find any index of `target`, say $k$.
|
||||
2. Starting from index $k$, conduct a linear search to the left until the leftmost occurrence of `target` is found, then return this index.
|
||||
|
||||
{ class="animation-figure" }
|
||||
|
||||
@ -173,10 +173,10 @@ The task requires inserting the target element to the very left, **so we need to
|
||||
|
||||
Although this method is feasible, it includes linear search, so its time complexity is $O(n)$. This method is inefficient when the array contains many duplicate `target`s.
|
||||
|
||||
Now consider extending the binary search code. As shown in Figure 10-6, the overall process remains the same, each round first calculates the midpoint index $m$, then judges the size relationship between `target` and `nums[m]`, divided into the following cases.
|
||||
Now consider extending the binary search code. As shown in Figure 10-6, the overall process remains the same. In each round, we first calculate the middle index $m$, then compare the value of `target` with `nums[m]`, leading to the following cases.
|
||||
|
||||
- When `nums[m] < target` or `nums[m] > target`, it means `target` has not been found yet, thus use the normal binary search interval reduction operation, **thus making pointers $i$ and $j$ approach `target`**.
|
||||
- When `nums[m] == target`, it indicates that the elements less than `target` are in the interval $[i, m - 1]$, therefore use $j = m - 1$ to narrow the interval, **thus making pointer $j$ approach elements less than `target`**.
|
||||
- When `nums[m] < target` or `nums[m] > target`, it means `target` has not been found yet, thus use the normal binary search to narrow the search range, **bringing pointers $i$ and $j$ closer to `target`**.
|
||||
- When `nums[m] == target`, it indicates that the elements less than `target` are in the range $[i, m - 1]$, therefore use $j = m - 1$ to narrow the range, **thus bringing pointer $j$ closer to the elements less than `target`**.
|
||||
|
||||
After the loop, $i$ points to the leftmost `target`, and $j$ points to the first element less than `target`, **therefore index $i$ is the insertion point**.
|
||||
|
||||
@ -206,9 +206,9 @@ After the loop, $i$ points to the leftmost `target`, and $j$ points to the first
|
||||
|
||||
<p align="center"> Figure 10-6 Steps for binary search insertion point of duplicate elements </p>
|
||||
|
||||
Observe the code, the operations of the branch `nums[m] > target` and `nums[m] == target` are the same, so the two can be combined.
|
||||
Observe the following code. The operations in the branches `nums[m] > target` and `nums[m] == target` are the same, so these two branches can be merged.
|
||||
|
||||
Even so, we can still keep the conditions expanded, as their logic is clearer and more readable.
|
||||
Even so, we can still keep the conditions expanded, as it makes the logic clearer and improves readability.
|
||||
|
||||
=== "Python"
|
||||
|
||||
@ -338,8 +338,8 @@ Even so, we can still keep the conditions expanded, as their logic is clearer an
|
||||
|
||||
!!! tip
|
||||
|
||||
The code in this section uses "closed intervals". Readers interested can implement the "left-closed right-open" method themselves.
|
||||
The code in this section uses "closed interval". If you are interested in "left-closed, right-open", try to implement the code on your own.
|
||||
|
||||
In summary, binary search is merely about setting search targets for pointers $i$ and $j$, which might be a specific element (like `target`) or a range of elements (like elements less than `target`).
|
||||
In summary, binary search essentially involves setting search targets for pointers $i$ and $j$. These targets could be a specific element (like `target`) or a range of elements (such as those smaller than `target`).
|
||||
|
||||
In the continuous loop of binary search, pointers $i$ and $j$ gradually approach the predefined target. Ultimately, they either find the answer or stop after crossing the boundary.
|
||||
|
@ -4,9 +4,9 @@ comments: true
|
||||
|
||||
# 11.3 Bubble sort
|
||||
|
||||
<u>Bubble sort</u> achieves sorting by continuously comparing and swapping adjacent elements. This process resembles bubbles rising from the bottom to the top, hence the name bubble sort.
|
||||
<u>Bubble sort</u> works by continuously comparing and swapping adjacent elements. This process is like bubbles rising from the bottom to the top, hence the name "bubble sort."
|
||||
|
||||
As shown in Figure 11-4, the bubbling process can be simulated using element swap operations: starting from the leftmost end of the array and moving right, sequentially compare the size of adjacent elements. If "left element > right element," then swap them. After the traversal, the largest element will be moved to the far right end of the array.
|
||||
As shown in Figure 11-4, the bubbling process can be simulated using element swaps: start from the leftmost end of the array and move right, comparing each pair of adjacent elements. If the left element is greater than the right element, swap them. After the traversal, the largest element will have bubbled up to the rightmost end of the array.
|
||||
|
||||
=== "<1>"
|
||||
{ class="animation-figure" }
|
||||
@ -33,12 +33,12 @@ As shown in Figure 11-4, the bubbling process can be simulated using element swa
|
||||
|
||||
## 11.3.1 Algorithm process
|
||||
|
||||
Assuming the length of the array is $n$, the steps of bubble sort are shown in Figure 11-5.
|
||||
Assume the array has length $n$. The steps of bubble sort are shown in Figure 11-5:
|
||||
|
||||
1. First, perform a "bubble" on $n$ elements, **swapping the largest element to its correct position**.
|
||||
2. Next, perform a "bubble" on the remaining $n - 1$ elements, **swapping the second largest element to its correct position**.
|
||||
3. Similarly, after $n - 1$ rounds of "bubbling," **the top $n - 1$ largest elements will be swapped to their correct positions**.
|
||||
4. The only remaining element is necessarily the smallest and does not require sorting, thus the array sorting is complete.
|
||||
1. First, perform one "bubble" pass on $n$ elements, **swapping the largest element to its correct position**.
|
||||
2. Next, perform a "bubble" pass on the remaining $n - 1$ elements, **swapping the second largest element to its correct position**.
|
||||
3. Continue in this manner; after $n - 1$ such passes, **the largest $n - 1$ elements will have been moved to their correct positions**.
|
||||
4. The only remaining element **must** be the smallest, so **no** further sorting is required. At this point, the array is sorted.
|
||||
|
||||
{ class="animation-figure" }
|
||||
|
||||
@ -168,9 +168,9 @@ Example code is as follows:
|
||||
|
||||
## 11.3.2 Efficiency optimization
|
||||
|
||||
We find that if no swaps are performed in a round of "bubbling," the array is already sorted, and we can return the result immediately. Thus, we can add a flag `flag` to monitor this situation and return immediately when it occurs.
|
||||
If no swaps occur during a round of "bubbling," the array is already sorted, so we can return immediately. To detect this, we can add a `flag` variable; whenever no swaps are made in a pass, we set the flag and return early.
|
||||
|
||||
Even after optimization, the worst-case time complexity and average time complexity of bubble sort remain at $O(n^2)$; however, when the input array is completely ordered, it can achieve the best time complexity of $O(n)$.
|
||||
Even with this optimization, the worst time complexity and average time complexity of bubble sort remains $O(n^2)$. However, if the input array is already sorted, the best-case time complexity can be as low as $O(n)$.
|
||||
|
||||
=== "Python"
|
||||
|
||||
@ -306,6 +306,6 @@ Even after optimization, the worst-case time complexity and average time complex
|
||||
|
||||
## 11.3.3 Algorithm characteristics
|
||||
|
||||
- **Time complexity of $O(n^2)$, adaptive sorting**: The length of the array traversed in each round of "bubbling" decreases sequentially from $n - 1$, $n - 2$, $\dots$, $2$, $1$, totaling $(n - 1) n / 2$. With the introduction of `flag` optimization, the best time complexity can reach $O(n)$.
|
||||
- **Space complexity of $O(1)$, in-place sorting**: Only a constant amount of extra space is used by pointers $i$ and $j$.
|
||||
- **Stable sorting**: As equal elements are not swapped during the "bubbling".
|
||||
- **Time complexity of $O(n^2)$, adaptive sorting.** Each round of "bubbling" traverses array segments of length $n - 1$, $n - 2$, $\dots$, $2$, $1$, which sums to $(n - 1) n / 2$. With a `flag` optimization, the best-case time complexity can reach $O(n)$ when the array is already sorted.
|
||||
- **Space complexity of $O(1)$, in-place sorting.** Only a constant amount of extra space is used by pointers $i$ and $j$.
|
||||
- **Stable sorting.** Because equal elements are not swapped during "bubbling," their original order is preserved, making this a stable sort.
|
||||
|
@ -4,13 +4,13 @@ comments: true
|
||||
|
||||
# 11.8 Bucket sort
|
||||
|
||||
The previously mentioned sorting algorithms are all "comparison-based sorting algorithms," which sort by comparing the size of elements. Such sorting algorithms cannot surpass a time complexity of $O(n \log n)$. Next, we will discuss several "non-comparison sorting algorithms" that can achieve linear time complexity.
|
||||
The previously mentioned sorting algorithms are all "comparison-based sorting algorithms," which sort elements by comparing their values. Such sorting algorithms cannot have better time complexity of $O(n \log n)$. Next, we will discuss several "non-comparison sorting algorithms" that could achieve linear time complexity.
|
||||
|
||||
<u>Bucket sort</u> is a typical application of the divide-and-conquer strategy. It involves setting up a series of ordered buckets, each corresponding to a range of data, and then distributing the data evenly among these buckets; each bucket is then sorted individually; finally, all the data are merged in the order of the buckets.
|
||||
<u>Bucket sort</u> is a typical application of the divide-and-conquer strategy. It works by setting up a series of ordered buckets, each containing a range of data, and distributing the input data evenly across these buckets. And then, the data in each bucket is sorted individually. Finally, the sorted data from all the buckets is merged in sequence to produce the final result.
|
||||
|
||||
## 11.8.1 Algorithm process
|
||||
|
||||
Consider an array of length $n$, with elements in the range $[0, 1)$. The bucket sort process is illustrated in Figure 11-13.
|
||||
Consider an array of length $n$, with float numbers in the range $[0, 1)$. The bucket sort process is illustrated in Figure 11-13.
|
||||
|
||||
1. Initialize $k$ buckets and distribute $n$ elements into these $k$ buckets.
|
||||
2. Sort each bucket individually (using the built-in sorting function of the programming language).
|
||||
@ -179,27 +179,27 @@ The code is shown as follows:
|
||||
|
||||
## 11.8.2 Algorithm characteristics
|
||||
|
||||
Bucket sort is suitable for handling very large data sets. For example, if the input data includes 1 million elements, and system memory limitations prevent loading all the data at once, you can divide the data into 1,000 buckets and sort each bucket separately before merging the results.
|
||||
Bucket sort is suitable for handling very large data sets. For example, if the input data includes 1 million elements, and system memory limitations prevent loading all the data at the same time, you can divide the data into 1,000 buckets and sort each bucket separately before merging the results.
|
||||
|
||||
- **Time complexity is $O(n + k)$**: Assuming the elements are evenly distributed across the buckets, the number of elements in each bucket is $n/k$. Assuming sorting a single bucket takes $O(n/k \log(n/k))$ time, sorting all buckets takes $O(n \log(n/k))$ time. **When the number of buckets $k$ is relatively large, the time complexity tends towards $O(n)$**. Merging the results requires traversing all buckets and elements, taking $O(n + k)$ time. In the worst case, all data is distributed into a single bucket, and sorting that bucket takes $O(n^2)$ time.
|
||||
- **Time complexity is $O(n + k)$**: Assuming the elements are evenly distributed across the buckets, the number of elements in each bucket is $n/k$. Assuming sorting a single bucket takes $O(n/k \log(n/k))$ time, sorting all buckets takes $O(n \log(n/k))$ time. **When the number of buckets $k$ is relatively large, the time complexity approaches $O(n)$**. Merging the results requires traversing all buckets and elements, taking $O(n + k)$ time. In the worst case, all data is distributed into a single bucket, and sorting that bucket takes $O(n^2)$ time.
|
||||
- **Space complexity is $O(n + k)$, non-in-place sorting**: It requires additional space for $k$ buckets and a total of $n$ elements.
|
||||
- Whether bucket sort is stable depends on whether the algorithm used to sort elements within the buckets is stable.
|
||||
- Whether bucket sort is stable depends on whether the sorting algorithm used within each bucket is stable.
|
||||
|
||||
## 11.8.3 How to achieve even distribution
|
||||
|
||||
The theoretical time complexity of bucket sort can reach $O(n)$, **the key is to evenly distribute the elements across all buckets**, as real data is often not uniformly distributed. For example, if we want to evenly distribute all products on Taobao by price range into 10 buckets, but the distribution of product prices is uneven, with many under 100 yuan and few over 1000 yuan. If the price range is evenly divided into 10, the difference in the number of products in each bucket will be very large.
|
||||
The theoretical time complexity of bucket sort can reach $O(n)$. **The key is to evenly distribute the elements across all buckets** as real-world data is often not uniformly distributed. For example, we may want to evenly distribute all products on eBay by price range into 10 buckets. However, the distribution of product prices may not be even, with many under $100 and few over $500. If the price range is evenly divided into 10, the difference in the number of products in each bucket will be significant.
|
||||
|
||||
To achieve even distribution, we can initially set a rough dividing line, roughly dividing the data into 3 buckets. **After the distribution is complete, the buckets with more products can be further divided into 3 buckets, until the number of elements in all buckets is roughly equal**.
|
||||
To achieve even distribution, we can initially set an approximate boundary to roughly divide the data into 3 buckets. **After the distribution is complete, the buckets with more items can be further divided into 3 buckets, until the number of elements in all buckets is roughly equal**.
|
||||
|
||||
As shown in Figure 11-14, this method essentially creates a recursive tree, aiming to make the leaf node values as even as possible. Of course, you don't have to divide the data into 3 buckets each round; the specific division method can be flexibly chosen based on data characteristics.
|
||||
As shown in Figure 11-14, this method essentially constructs a recursive tree, aiming to ensure the element counts in leaf nodes are as even as possible. Of course, you don't have to divide the data into 3 buckets each round - the partitioning strategy can be adaptively tailored to the data's unique characteristics.
|
||||
|
||||
{ class="animation-figure" }
|
||||
|
||||
<p align="center"> Figure 11-14 Recursive division of buckets </p>
|
||||
|
||||
If we know the probability distribution of product prices in advance, **we can set the price dividing line for each bucket based on the data probability distribution**. It is worth noting that it is not necessarily required to specifically calculate the data distribution; it can also be approximated based on data characteristics using some probability model.
|
||||
If we know the probability distribution of product prices in advance, **we can set the price boundaries for each bucket based on the data probability distribution**. It is worth noting that it is not necessarily required to specifically calculate the data distribution; instead, it can be approximated based on data characteristics using a probability model.
|
||||
|
||||
As shown in Figure 11-15, we assume that product prices follow a normal distribution, allowing us to reasonably set the price intervals, thereby evenly distributing the products into the respective buckets.
|
||||
As shown in Figure 11-15, assuming that product prices follow a normal distribution, we can define reasonable price intervals to balance the distribution of items across the buckets.
|
||||
|
||||
{ class="animation-figure" }
|
||||
|
||||
|
@ -4,15 +4,15 @@ comments: true
|
||||
|
||||
# 11.9 Counting sort
|
||||
|
||||
<u>Counting sort</u> achieves sorting by counting the number of elements, typically applied to arrays of integers.
|
||||
<u>Counting sort</u> achieves sorting by counting the number of elements, usually applied to integer arrays.
|
||||
|
||||
## 11.9.1 Simple implementation
|
||||
|
||||
Let's start with a simple example. Given an array `nums` of length $n$, where all elements are "non-negative integers", the overall process of counting sort is illustrated in Figure 11-16.
|
||||
Let's start with a simple example. Given an array `nums` of length $n$, where all elements are "non-negative integers", the overall process of counting sort is shown in Figure 11-16.
|
||||
|
||||
1. Traverse the array to find the maximum number, denoted as $m$, then create an auxiliary array `counter` of length $m + 1$.
|
||||
2. **Use `counter` to count the occurrence of each number in `nums`**, where `counter[num]` corresponds to the occurrence of the number `num`. The counting method is simple, just traverse `nums` (suppose the current number is `num`), and increase `counter[num]` by $1$ each round.
|
||||
3. **Since the indices of `counter` are naturally ordered, all numbers are essentially sorted already**. Next, we traverse `counter`, filling `nums` in ascending order of occurrence.
|
||||
3. **Since the indices of `counter` are naturally ordered, all numbers are essentially sorted already**. Next, we traverse `counter`, and fill in `nums` in ascending order of occurrence.
|
||||
|
||||
{ class="animation-figure" }
|
||||
|
||||
@ -169,7 +169,7 @@ The code is shown below:
|
||||
|
||||
## 11.9.2 Complete implementation
|
||||
|
||||
Astute readers might have noticed, **if the input data is an object, the above step `3.` becomes ineffective**. Suppose the input data is a product object, we want to sort the products by their price (a class member variable), but the above algorithm can only provide the sorting result for the price.
|
||||
Observant readers might notice, **if the input data is an object, the above step `3.` is invalid**. Suppose the input data is a product object, we want to sort the products by the price (a class member variable), but the above algorithm can only give the sorted price as the result.
|
||||
|
||||
So how can we get the sorting result for the original data? First, we calculate the "prefix sum" of `counter`. As the name suggests, the prefix sum at index `i`, `prefix[i]`, equals the sum of the first `i` elements of the array:
|
||||
|
||||
@ -177,10 +177,10 @@ $$
|
||||
\text{prefix}[i] = \sum_{j=0}^i \text{counter[j]}
|
||||
$$
|
||||
|
||||
**The prefix sum has a clear meaning, `prefix[num] - 1` represents the last occurrence index of element `num` in the result array `res`**. This information is crucial, as it tells us where each element should appear in the result array. Next, we traverse the original array `nums` for each element `num` in reverse order, performing the following two steps in each iteration.
|
||||
**The prefix sum has a clear meaning, `prefix[num] - 1` represents the index of the last occurrence of element `num` in the result array `res`**. This information is crucial, as it tells us where each element should appear in the result array. Next, we traverse each element `num` of the original array `nums` in reverse order, performing the following two steps in each iteration.
|
||||
|
||||
1. Fill `num` into the array `res` at the index `prefix[num] - 1`.
|
||||
2. Reduce the prefix sum `prefix[num]` by $1$, thus obtaining the next index to place `num`.
|
||||
2. Decrease the prefix sum `prefix[num]` by $1$ to obtain the next index to place `num`.
|
||||
|
||||
After the traversal, the array `res` contains the sorted result, and finally, `res` replaces the original array `nums`. The complete counting sort process is shown in Figure 11-17.
|
||||
|
||||
@ -384,14 +384,14 @@ The implementation code of counting sort is shown below:
|
||||
|
||||
## 11.9.3 Algorithm characteristics
|
||||
|
||||
- **Time complexity is $O(n + m)$, non-adaptive sort**: Involves traversing `nums` and `counter`, both using linear time. Generally, $n \gg m$, and the time complexity tends towards $O(n)$.
|
||||
- **Space complexity is $O(n + m)$, non-in-place sort**: Utilizes arrays `res` and `counter` of lengths $n$ and $m$ respectively.
|
||||
- **Time complexity is $O(n + m)$, non-adaptive sort**: It involves traversing `nums` and `counter`, both using linear time. Generally, $n \gg m$, and the time complexity tends towards $O(n)$.
|
||||
- **Space complexity is $O(n + m)$, non-in-place sort**: It uses array `res` of lengths $n$ and array `counter` of length $m$ respectively.
|
||||
- **Stable sort**: Since elements are filled into `res` in a "right-to-left" order, reversing the traversal of `nums` can prevent changing the relative position between equal elements, thereby achieving a stable sort. Actually, traversing `nums` in order can also produce the correct sorting result, but the outcome is unstable.
|
||||
|
||||
## 11.9.4 Limitations
|
||||
|
||||
By now, you might find counting sort very clever, as it can achieve efficient sorting merely by counting quantities. However, the prerequisites for using counting sort are relatively strict.
|
||||
|
||||
**Counting sort is only suitable for non-negative integers**. If you want to apply it to other types of data, you need to ensure that these data can be converted to non-negative integers without changing the relative sizes of the elements. For example, for an array containing negative integers, you can first add a constant to all numbers, converting them all to positive numbers, and then convert them back after sorting is complete.
|
||||
**Counting sort is only suitable for non-negative integers**. If you want to apply it to other types of data, you need to ensure that these data can be converted to non-negative integers without changing the original order of the elements. For example, for an array containing negative integers, you can first add a constant to all numbers, converting them all to positive numbers, and then convert them back after sorting is complete.
|
||||
|
||||
**Counting sort is suitable for large data volumes but small data ranges**. For example, in the above example, $m$ should not be too large, otherwise, it will occupy too much space. And when $n \ll m$, counting sort uses $O(m)$ time, which may be slower than $O(n \log n)$ sorting algorithms.
|
||||
**Counting sort is suitable for large datasets with a small range of values**. For example, in the above example, $m$ should not be too large, otherwise, it will occupy too much space. And when $n \ll m$, counting sort uses $O(m)$ time, which may be slower than $O(n \log n)$ sorting algorithms.
|
||||
|
@ -6,27 +6,27 @@ comments: true
|
||||
|
||||
!!! tip
|
||||
|
||||
Before reading this section, please make sure you have completed the "Heap" chapter.
|
||||
Before reading this section, please ensure you have completed the "Heap" chapter.
|
||||
|
||||
<u>Heap sort</u> is an efficient sorting algorithm based on the heap data structure. We can implement heap sort using the "heap creation" and "element extraction" operations we have already learned.
|
||||
|
||||
1. Input the array and establish a min-heap, where the smallest element is at the heap's top.
|
||||
2. Continuously perform the extraction operation, recording the extracted elements in sequence to obtain a sorted list from smallest to largest.
|
||||
1. Input the array and construct a min-heap, where the smallest element is at the top of the heap.
|
||||
2. Continuously perform the extraction operation, record the extracted elements sequentially to obtain a sorted list from smallest to largest.
|
||||
|
||||
Although the above method is feasible, it requires an additional array to save the popped elements, which is somewhat space-consuming. In practice, we usually use a more elegant implementation.
|
||||
Although the above method is feasible, it requires an additional array to store the popped elements, which is somewhat space-consuming. In practice, we usually use a more elegant implementation.
|
||||
|
||||
## 11.7.1 Algorithm flow
|
||||
|
||||
Suppose the array length is $n$, the heap sort process is as follows.
|
||||
|
||||
1. Input the array and establish a max-heap. After completion, the largest element is at the heap's top.
|
||||
2. Swap the top element of the heap (the first element) with the heap's bottom element (the last element). After the swap, reduce the heap's length by $1$ and increase the sorted elements count by $1$.
|
||||
1. Input the array and establish a max-heap. After this step, the largest element is positioned at the top of the heap.
|
||||
2. Swap the top element of the heap (the first element) with the heap's bottom element (the last element). Following this swap, reduce the heap's length by $1$ and increase the sorted elements count by $1$.
|
||||
3. Starting from the heap top, perform the sift-down operation from top to bottom. After the sift-down, the heap's property is restored.
|
||||
4. Repeat steps `2.` and `3.` Loop for $n - 1$ rounds to complete the sorting of the array.
|
||||
|
||||
!!! tip
|
||||
|
||||
In fact, the element extraction operation also includes steps `2.` and `3.`, with the addition of a popping element step.
|
||||
In fact, the element extraction operation also includes steps `2.` and `3.`, with an additional step to pop (remove) the extracted element from the heap.
|
||||
|
||||
=== "<1>"
|
||||
{ class="animation-figure" }
|
||||
|
@ -9,7 +9,7 @@ icon: material/sort-ascending
|
||||
|
||||
!!! abstract
|
||||
|
||||
Sorting is like a magical key that turns chaos into order, enabling us to understand and handle data in a more efficient manner.
|
||||
Sorting is like a magical key that turns chaos into order, enabling us to understand and handle data more efficiently.
|
||||
|
||||
Whether it's simple ascending order or complex categorical arrangements, sorting reveals the harmonious beauty of data.
|
||||
|
||||
|
@ -6,9 +6,9 @@ comments: true
|
||||
|
||||
<u>Insertion sort</u> is a simple sorting algorithm that works very much like the process of manually sorting a deck of cards.
|
||||
|
||||
Specifically, we select a pivot element from the unsorted interval, compare it with the elements in the sorted interval to its left, and insert the element into the correct position.
|
||||
Specifically, we select a base element from the unsorted interval, compare it with the elements in the sorted interval to its left, and insert the element into the correct position.
|
||||
|
||||
Figure 11-6 shows the process of inserting an element into an array. Assuming the pivot element is `base`, we need to move all elements between the target index and `base` one position to the right, then assign `base` to the target index.
|
||||
Figure 11-6 illustrates how an element is inserted into the array. Assuming the base element is `base`, we need to shift all elements from the target index up to `base` one position to the right, then assign `base` to the target index.
|
||||
|
||||
{ class="animation-figure" }
|
||||
|
||||
@ -18,10 +18,10 @@ Figure 11-6 shows the process of inserting an element into an array. Assuming th
|
||||
|
||||
The overall process of insertion sort is shown in Figure 11-7.
|
||||
|
||||
1. Initially, the first element of the array is sorted.
|
||||
2. The second element of the array is taken as `base`, and after inserting it into the correct position, **the first two elements of the array are sorted**.
|
||||
3. The third element is taken as `base`, and after inserting it into the correct position, **the first three elements of the array are sorted**.
|
||||
4. And so on, in the last round, the last element is taken as `base`, and after inserting it into the correct position, **all elements are sorted**.
|
||||
1. Consider the first element of the array as sorted.
|
||||
2. Select the second element as `base`, insert it into its correct position, **leaving the first two elements sorted**.
|
||||
3. Select the third element as `base`, insert it into its correct position, **leaving the first three elements sorted**.
|
||||
4. Continuing in this manner, in the final iteration, the last element is taken as `base`, and after inserting it into the correct position, **all elements are sorted**.
|
||||
|
||||
{ class="animation-figure" }
|
||||
|
||||
@ -155,13 +155,13 @@ Example code is as follows:
|
||||
|
||||
## 11.4.3 Advantages of insertion sort
|
||||
|
||||
The time complexity of insertion sort is $O(n^2)$, while the time complexity of quicksort, which we will study next, is $O(n \log n)$. Although insertion sort has a higher time complexity, **it is usually faster in cases of small data volumes**.
|
||||
The time complexity of insertion sort is $O(n^2)$, while the time complexity of quicksort, which we will study next, is $O(n \log n)$. Although insertion sort has a higher time complexity, **it is usually faster in small input sizes**.
|
||||
|
||||
This conclusion is similar to that for linear and binary search. Algorithms like quicksort that have a time complexity of $O(n \log n)$ and are based on the divide-and-conquer strategy often involve more unit operations. In cases of small data volumes, the numerical values of $n^2$ and $n \log n$ are close, and complexity does not dominate, with the number of unit operations per round playing a decisive role.
|
||||
This conclusion is similar to that for linear and binary search. Algorithms like quicksort that have a time complexity of $O(n \log n)$ and are based on the divide-and-conquer strategy often involve more unit operations. For small input sizes, the numerical values of $n^2$ and $n \log n$ are close, and complexity does not dominate, with the number of unit operations per round playing a decisive role.
|
||||
|
||||
In fact, many programming languages (such as Java) use insertion sort in their built-in sorting functions. The general approach is: for long arrays, use sorting algorithms based on divide-and-conquer strategies, such as quicksort; for short arrays, use insertion sort directly.
|
||||
In fact, many programming languages (such as Java) use insertion sort within their built-in sorting functions. The general approach is: for long arrays, use sorting algorithms based on divide-and-conquer strategies, such as quicksort; for short arrays, use insertion sort directly.
|
||||
|
||||
Although bubble sort, selection sort, and insertion sort all have a time complexity of $O(n^2)$, in practice, **insertion sort is used significantly more frequently than bubble sort and selection sort**, mainly for the following reasons.
|
||||
Although bubble sort, selection sort, and insertion sort all have a time complexity of $O(n^2)$, in practice, **insertion sort is commonly used than bubble sort and selection sort**, mainly for the following reasons.
|
||||
|
||||
- Bubble sort is based on element swapping, which requires the use of a temporary variable, involving 3 unit operations; insertion sort is based on element assignment, requiring only 1 unit operation. Therefore, **the computational overhead of bubble sort is generally higher than that of insertion sort**.
|
||||
- The time complexity of selection sort is always $O(n^2)$. **Given a set of partially ordered data, insertion sort is usually more efficient than selection sort**.
|
||||
|
@ -6,8 +6,8 @@ comments: true
|
||||
|
||||
<u>Merge sort</u> is a sorting algorithm based on the divide-and-conquer strategy, involving the "divide" and "merge" phases shown in Figure 11-10.
|
||||
|
||||
1. **Divide phase**: Recursively split the array from the midpoint, transforming the sorting problem of a long array into that of shorter arrays.
|
||||
2. **Merge phase**: Stop dividing when the length of the sub-array is 1, start merging, and continuously combine two shorter ordered arrays into one longer ordered array until the process is complete.
|
||||
1. **Divide phase**: Recursively split the array from the midpoint, transforming the sorting problem of a long array into shorter arrays.
|
||||
2. **Merge phase**: Stop dividing when the length of the sub-array is 1, and then begin merging. The two shorter sorted arrays are continuously merged into a longer sorted array until the process is complete.
|
||||
|
||||
{ class="animation-figure" }
|
||||
|
||||
@ -18,9 +18,9 @@ comments: true
|
||||
As shown in Figure 11-11, the "divide phase" recursively splits the array from the midpoint into two sub-arrays from top to bottom.
|
||||
|
||||
1. Calculate the midpoint `mid`, recursively divide the left sub-array (interval `[left, mid]`) and the right sub-array (interval `[mid + 1, right]`).
|
||||
2. Continue with step `1.` recursively until the sub-array interval length is 1 to stop.
|
||||
2. Continue with step `1.` recursively until sub-array length becomes 1, then stops.
|
||||
|
||||
The "merge phase" combines the left and right sub-arrays into a single ordered array from bottom to top. Note that merging starts with sub-arrays of length 1, and each sub-array is ordered during the merge phase.
|
||||
The "merge phase" combines the left and right sub-arrays into a sorted array from bottom to top. It is important to note that, merging starts with sub-arrays of length 1, and each sub-array is sorted during the merge phase.
|
||||
|
||||
=== "<1>"
|
||||
{ class="animation-figure" }
|
||||
@ -54,10 +54,10 @@ The "merge phase" combines the left and right sub-arrays into a single ordered a
|
||||
|
||||
<p align="center"> Figure 11-11 Merge sort process </p>
|
||||
|
||||
It is observed that the order of recursion in merge sort is consistent with the post-order traversal of a binary tree.
|
||||
It can be observed that the order of recursion in merge sort is consistent with the post-order traversal of a binary tree.
|
||||
|
||||
- **Post-order traversal**: First recursively traverse the left subtree, then the right subtree, and finally handle the root node.
|
||||
- **Merge sort**: First recursively handle the left sub-array, then the right sub-array, and finally perform the merge.
|
||||
- **Post-order traversal**: First recursively traverse the left subtree, then the right subtree, and finally process the root node.
|
||||
- **Merge sort**: First recursively process the left sub-array, then the right sub-array, and finally perform the merge.
|
||||
|
||||
The implementation of merge sort is shown in the following code. Note that the interval to be merged in `nums` is `[left, right]`, while the corresponding interval in `tmp` is `[0, right - left]`.
|
||||
|
||||
@ -290,9 +290,9 @@ The implementation of merge sort is shown in the following code. Note that the i
|
||||
|
||||
## 11.6.3 Linked List sorting
|
||||
|
||||
For linked lists, merge sort has significant advantages over other sorting algorithms, **optimizing the space complexity of the linked list sorting task to $O(1)$**.
|
||||
For linked lists, merge sort has significant advantages over other sorting algorithms. **It can optimize the space complexity of the linked list sorting task to $O(1)$**.
|
||||
|
||||
- **Divide phase**: "Iteration" can be used instead of "recursion" to perform the linked list division work, thus saving the stack frame space used by recursion.
|
||||
- **Merge phase**: In linked lists, node addition and deletion operations can be achieved by changing references (pointers), so no extra lists need to be created during the merge phase (combining two short ordered lists into one long ordered list).
|
||||
- **Merge phase**: In linked lists, node insertion and deletion operations can be achieved by changing references (pointers), so no extra lists need to be created during the merge phase (combining two short ordered lists into one long ordered list).
|
||||
|
||||
Detailed implementation details are complex, and interested readers can consult related materials for learning.
|
||||
The implementation details are relatively complex, and interested readers can consult related materials for learning.
|
||||
|
@ -4,12 +4,12 @@ comments: true
|
||||
|
||||
# 11.5 Quick sort
|
||||
|
||||
<u>Quick sort</u> is a sorting algorithm based on the divide and conquer strategy, known for its efficiency and wide application.
|
||||
<u>Quick sort</u> is a sorting algorithm based on the divide-and-conquer strategy, known for its efficiency and wide application.
|
||||
|
||||
The core operation of quick sort is "pivot partitioning," aiming to: select an element from the array as the "pivot," move all elements smaller than the pivot to its left, and move elements greater than the pivot to its right. Specifically, the pivot partitioning process is illustrated in Figure 11-8.
|
||||
The core operation of quick sort is "pivot partitioning," which aims to select an element from the array as the "pivot" and move all elements less than the pivot to its left side, while moving all elements greater than the pivot to its right side. Specifically, the process of pivot partitioning is illustrated in Figure 11-8.
|
||||
|
||||
1. Select the leftmost element of the array as the pivot, and initialize two pointers `i` and `j` at both ends of the array.
|
||||
2. Set up a loop where each round uses `i` (`j`) to find the first element larger (smaller) than the pivot, then swap these two elements.
|
||||
1. Select the leftmost element of the array as the pivot, and initialize two pointers `i` and `j` to point to the two ends of the array respectively.
|
||||
2. Set up a loop where each round uses `i` (`j`) to search for the first element larger (smaller) than the pivot, then swap these two elements.
|
||||
3. Repeat step `2.` until `i` and `j` meet, finally swap the pivot to the boundary between the two sub-arrays.
|
||||
|
||||
=== "<1>"
|
||||
@ -41,11 +41,11 @@ The core operation of quick sort is "pivot partitioning," aiming to: select an e
|
||||
|
||||
<p align="center"> Figure 11-8 Pivot division process </p>
|
||||
|
||||
After the pivot partitioning, the original array is divided into three parts: left sub-array, pivot, and right sub-array, satisfying "any element in the left sub-array $\leq$ pivot $\leq$ any element in the right sub-array." Therefore, we only need to sort these two sub-arrays next.
|
||||
After the pivot partitioning, the original array is divided into three parts: left sub-array, pivot, and right sub-array, satisfying "any element in the left sub-array $\leq$ pivot $\leq$ any element in the right sub-array." Therefore, we then only need to sort these two sub-arrays.
|
||||
|
||||
!!! note "Quick sort's divide and conquer strategy"
|
||||
!!! note "Divide-and-conquer strategy for quick sort"
|
||||
|
||||
The essence of pivot partitioning is to simplify a longer array's sorting problem into two shorter arrays' sorting problems.
|
||||
The essence of pivot partitioning is to simplify the sorting problem of a longer array into two shorter arrays.
|
||||
|
||||
=== "Python"
|
||||
|
||||
@ -196,8 +196,8 @@ After the pivot partitioning, the original array is divided into three parts: le
|
||||
The overall process of quick sort is shown in Figure 11-9.
|
||||
|
||||
1. First, perform a "pivot partitioning" on the original array to obtain the unsorted left and right sub-arrays.
|
||||
2. Then, recursively perform "pivot partitioning" on both the left and right sub-arrays.
|
||||
3. Continue recursively until the sub-array length reaches 1, thus completing the sorting of the entire array.
|
||||
2. Then, recursively perform "pivot partitioning" on the left and right sub-arrays separately.
|
||||
3. Continue recursively until the length of sub-array is 1, thus completing the sorting of the entire array.
|
||||
|
||||
{ class="animation-figure" }
|
||||
|
||||
@ -318,27 +318,27 @@ The overall process of quick sort is shown in Figure 11-9.
|
||||
|
||||
## 11.5.2 Algorithm features
|
||||
|
||||
- **Time complexity of $O(n \log n)$, non-adaptive sorting**: In average cases, the recursive levels of pivot partitioning are $\log n$, and the total number of loops per level is $n$, using $O(n \log n)$ time overall. In the worst case, each round of pivot partitioning divides an array of length $n$ into two sub-arrays of lengths $0$ and $n - 1$, reaching $n$ recursive levels, and using $O(n^2)$ time overall.
|
||||
- **Space complexity of $O(n)$, in-place sorting**: In completely reversed input arrays, reaching the worst recursion depth of $n$, using $O(n)$ stack frame space. The sorting operation is performed on the original array without the aid of additional arrays.
|
||||
- **Time complexity of $O(n \log n)$, non-adaptive sorting**: In average cases, the recursive levels of pivot partitioning are $\log n$, and the total number of loops per level is $n$, using $O(n \log n)$ time overall. In the worst case, each round of pivot partitioning divides an array of length $n$ into two sub-arrays of lengths $0$ and $n - 1$, when the number of recursive levels reaches $n$, the number of loops in each level is $n$, and the total time used is $O(n^2)$.
|
||||
- **Space complexity of $O(n)$, in-place sorting**: In the case where the input array is completely reversed, the worst recursive depth reaches $n$, using $O(n)$ stack frame space. The sorting operation is performed on the original array without the aid of additional arrays.
|
||||
- **Non-stable sorting**: In the final step of pivot partitioning, the pivot may be swapped to the right of equal elements.
|
||||
|
||||
## 11.5.3 Why is quick sort fast
|
||||
|
||||
From its name, it is apparent that quick sort should have certain efficiency advantages. Although the average time complexity of quick sort is the same as "merge sort" and "heap sort," quick sort is generally more efficient, mainly for the following reasons.
|
||||
As the name suggests, quick sort should have certain advantages in terms of efficiency. Although the average time complexity of quick sort is the same as that of "merge sort" and "heap sort," it is generally more efficient for the following reasons.
|
||||
|
||||
- **Low probability of worst-case scenarios**: Although the worst time complexity of quick sort is $O(n^2)$, less stable than merge sort, in most cases, quick sort can operate under a time complexity of $O(n \log n)$.
|
||||
- **High cache usage efficiency**: During the pivot partitioning operation, the system can load the entire sub-array into the cache, thus accessing elements more efficiently. In contrast, algorithms like "heap sort" need to access elements in a jumping manner, lacking this feature.
|
||||
- **Small constant coefficient of complexity**: Among the mentioned algorithms, quick sort has the fewest total number of comparisons, assignments, and swaps. This is similar to why "insertion sort" is faster than "bubble sort."
|
||||
- **High cache utilization**: During the pivot partitioning operation, the system can load the entire sub-array into the cache, thus accessing elements more efficiently. In contrast, algorithms like "heap sort" need to access elements in a jumping manner, lacking this feature.
|
||||
- **Small constant coefficient of complexity**: Among the three algorithms mentioned above, quick sort has the least total number of operations such as comparisons, assignments, and swaps. This is similar to why "insertion sort" is faster than "bubble sort."
|
||||
|
||||
## 11.5.4 Pivot optimization
|
||||
|
||||
**Quick sort's time efficiency may decrease under certain inputs**. For example, if the input array is completely reversed, since we select the leftmost element as the pivot, after the pivot partitioning, the pivot is swapped to the array's right end, causing the left sub-array length to be $n - 1$ and the right sub-array length to be $0$. If this recursion continues, each round of pivot partitioning will have a sub-array length of $0$, and the divide and conquer strategy fails, degrading quick sort to a form similar to "bubble sort."
|
||||
**Quick sort's time efficiency may degrade under certain inputs**. For example, if the input array is completely reversed, since we select the leftmost element as the pivot, after the pivot partitioning, the pivot is swapped to the array's right end, causing the left sub-array length to be $n - 1$ and the right sub-array length to be $0$. Continuing this way, each round of pivot partitioning will have a sub-array length of $0$, and the divide-and-conquer strategy fails, degrading quick sort to a form similar to "bubble sort."
|
||||
|
||||
To avoid this situation, **we can optimize the strategy for selecting the pivot in the pivot partitioning**. For instance, we can randomly select an element as the pivot. However, if luck is not on our side, and we keep selecting suboptimal pivots, the efficiency is still not satisfactory.
|
||||
To avoid this situation, **we can optimize the pivot selection strategy in the pivot partitioning**. For instance, we can randomly select an element as the pivot. However, if luck is not on our side, and we consistently select suboptimal pivots, the efficiency is still not satisfactory.
|
||||
|
||||
It's important to note that programming languages usually generate "pseudo-random numbers". If we construct a specific test case for a pseudo-random number sequence, the efficiency of quick sort may still degrade.
|
||||
|
||||
For further improvement, we can select three candidate elements (usually the first, last, and midpoint elements of the array), **and use the median of these three candidate elements as the pivot**. This significantly increases the probability that the pivot is "neither too small nor too large". Of course, we can also select more candidate elements to further enhance the algorithm's robustness. Using this method significantly reduces the probability of time complexity degradation to $O(n^2)$.
|
||||
For further improvement, we can select three candidate elements (usually the first, last, and midpoint elements of the array), **and use the median of these three candidate elements as the pivot**. This way, the probability that the pivot is "neither too small nor too large" will be greatly increased. Of course, we can also select more candidate elements to further enhance robustness of the algorithm. With this method, the probability of the time complexity degrading to $O(n^2)$ is greatly reduced.
|
||||
|
||||
Sample code is as follows:
|
||||
|
||||
@ -530,7 +530,8 @@ Sample code is as follows:
|
||||
|
||||
## 11.5.5 Tail recursion optimization
|
||||
|
||||
**Under certain inputs, quick sort may occupy more space**. For a completely ordered input array, assume the sub-array length in recursion is $m$, each round of pivot partitioning produces a left sub-array of length $0$ and a right sub-array of length $m - 1$, meaning the problem size reduced per recursive call is very small (only one element), and the height of the recursion tree can reach $n - 1$, requiring $O(n)$ stack frame space.
|
||||
**Under certain inputs, quick sort may occupy more space**. For example, consider a completely ordered input array. Let the length of the sub-array in the recursion be $m$. In each round of pivot partitioning, a left sub-array of length $0$ and a right sub-array of length $m - 1$ are produced. This means that the problem size is reduced by only one element per recursive call, resulting in a very small reduction at each level of recursion.
|
||||
As a result, the height of the recursion tree can reach $n − 1$ , which requires $O(n)$ of stack frame space.
|
||||
|
||||
To prevent the accumulation of stack frame space, we can compare the lengths of the two sub-arrays after each round of pivot sorting, **and only recursively sort the shorter sub-array**. Since the length of the shorter sub-array will not exceed $n / 2$, this method ensures that the recursion depth does not exceed $\log n$, thus optimizing the worst space complexity to $O(\log n)$. The code is as follows:
|
||||
|
||||
|
@ -4,17 +4,17 @@ comments: true
|
||||
|
||||
# 11.10 Radix sort
|
||||
|
||||
The previous section introduced counting sort, which is suitable for scenarios where the data volume $n$ is large but the data range $m$ is small. Suppose we need to sort $n = 10^6$ student IDs, where each ID is an $8$-digit number. This means the data range $m = 10^8$ is very large, requiring a significant amount of memory space for counting sort, while radix sort can avoid this situation.
|
||||
The previous section introduced counting sort, which is suitable for scenarios where the data size $n$ is large but the data range $m$ is small. Suppose we need to sort $n = 10^6$ student IDs, where each ID is an $8$-digit number. This means the data range $m = 10^8$ is very large. Using counting sort in this case would require significant memory space. Radix sort can avoid this situation.
|
||||
|
||||
<u>Radix sort</u> shares the core idea with counting sort, which also sorts by counting the frequency of elements. Building on this, radix sort utilizes the progressive relationship between the digits of numbers, sorting each digit in turn to achieve the final sorted order.
|
||||
<u>Radix sort</u> shares the same core concept as counting sort, which also sorts by counting the frequency of elements. Meanwhile, radix sort builds upon this by utilizing the progressive relationship between the digits of numbers. It processes and sorts the digits one at a time, achieving the final sorted order.
|
||||
|
||||
## 11.10.1 Algorithm process
|
||||
|
||||
Taking the student ID data as an example, assuming the least significant digit is the $1^{st}$ and the most significant is the $8^{th}$, the radix sort process is illustrated in Figure 11-18.
|
||||
Taking the student ID data as an example, assume the least significant digit is the $1^{st}$ and the most significant is the $8^{th}$, the radix sort process is illustrated in Figure 11-18.
|
||||
|
||||
1. Initialize digit $k = 1$.
|
||||
2. Perform "counting sort" on the $k^{th}$ digit of the student IDs. After completion, the data will be sorted from smallest to largest based on the $k^{th}$ digit.
|
||||
3. Increment $k$ by $1$, then return to step `2.` and continue iterating until all digits have been sorted, then the process ends.
|
||||
3. Increment $k$ by $1$, then return to step `2.` and continue iterating until all digits have been sorted, at which point the process ends.
|
||||
|
||||
{ class="animation-figure" }
|
||||
|
||||
@ -292,12 +292,12 @@ Additionally, we need to slightly modify the counting sort code to allow sorting
|
||||
|
||||
!!! question "Why start sorting from the least significant digit?"
|
||||
|
||||
In consecutive sorting rounds, the result of a later round will override the result of an earlier round. For example, if the result of the first round is $a < b$ and the result of the second round is $a > b$, the result of the second round will replace the first round's result. Since the significance of higher digits is greater than that of lower digits, it makes sense to sort lower digits before higher digits.
|
||||
In consecutive sorting rounds, the result of a later round will override the result of an earlier round. For example, if the result of the first round is $a < b$ and the second round is $a > b$, the second round's result will replace the first round's result. Since higher-order digits take precedence over lower-order digits, it makes sense to sort the lower digits before the higher digits.
|
||||
|
||||
## 11.10.2 Algorithm characteristics
|
||||
|
||||
Compared to counting sort, radix sort is suitable for larger numerical ranges, **but it assumes that the data can be represented in a fixed number of digits, and the number of digits should not be too large**. For example, floating-point numbers are not suitable for radix sort, as their digit count $k$ may be large, potentially leading to a time complexity $O(nk) \gg O(n^2)$.
|
||||
Compared to counting sort, radix sort is suitable for larger numerical ranges, **but it assumes that the data can be represented in a fixed number of digits, and the number of digits should not be too large**. For example, floating-point numbers are unsuitable for radix sort, as their digit count $k$ may be large, potentially leading to a time complexity $O(nk) \gg O(n^2)$.
|
||||
|
||||
- **Time complexity is $O(nk)$, non-adaptive sorting**: Assuming the data size is $n$, the data is in base $d$, and the maximum number of digits is $k$, then sorting a single digit takes $O(n + d)$ time, and sorting all $k$ digits takes $O((n + d)k)$ time. Generally, both $d$ and $k$ are relatively small, leading to a time complexity approaching $O(n)$.
|
||||
- **Space complexity is $O(n + d)$, non-in-place sorting**: Like counting sort, radix sort relies on arrays `res` and `counter` of lengths $n$ and $d$ respectively.
|
||||
- **Stable sorting**: When counting sort is stable, radix sort is also stable; if counting sort is unstable, radix sort cannot guarantee a correct sorting outcome.
|
||||
- **Stable sorting**: When counting sort is stable, radix sort is also stable; if counting sort is unstable, radix sort cannot ensure a correct sorting order.
|
||||
|
@ -4,15 +4,15 @@ comments: true
|
||||
|
||||
# 11.2 Selection sort
|
||||
|
||||
<u>Selection sort</u> works on a very simple principle: it starts a loop where each iteration selects the smallest element from the unsorted interval and moves it to the end of the sorted interval.
|
||||
<u>Selection sort</u> works on a very simple principle: it uses a loop where each iteration selects the smallest element from the unsorted interval and moves it to the end of the sorted section.
|
||||
|
||||
Suppose the length of the array is $n$, the algorithm flow of selection sort is as shown in Figure 11-2.
|
||||
Suppose the length of the array is $n$, the steps of selection sort is shown in Figure 11-2.
|
||||
|
||||
1. Initially, all elements are unsorted, i.e., the unsorted (index) interval is $[0, n-1]$.
|
||||
2. Select the smallest element in the interval $[0, n-1]$ and swap it with the element at index $0$. After this, the first element of the array is sorted.
|
||||
3. Select the smallest element in the interval $[1, n-1]$ and swap it with the element at index $1$. After this, the first two elements of the array are sorted.
|
||||
4. Continue in this manner. After $n - 1$ rounds of selection and swapping, the first $n - 1$ elements are sorted.
|
||||
5. The only remaining element is necessarily the largest element and does not need sorting, thus the array is sorted.
|
||||
5. The only remaining element is subsequently the largest element and does not need sorting, thus the array is sorted.
|
||||
|
||||
=== "<1>"
|
||||
{ class="animation-figure" }
|
||||
@ -178,7 +178,7 @@ In the code, we use $k$ to record the smallest element within the unsorted inter
|
||||
|
||||
## 11.2.1 Algorithm characteristics
|
||||
|
||||
- **Time complexity of $O(n^2)$, non-adaptive sort**: There are $n - 1$ rounds in the outer loop, with the unsorted interval length starting at $n$ in the first round and decreasing to $2$ in the last round, i.e., the outer loops contain $n$, $n - 1$, $\dots$, $3$, $2$ inner loops respectively, summing up to $\frac{(n - 1)(n + 2)}{2}$.
|
||||
- **Time complexity of $O(n^2)$, non-adaptive sort**: There are $n - 1$ iterations in the outer loop, with the length of the unsorted section starting at $n$ in the first iteration and decreasing to $2$ in the last iteration, i.e., each outer loop iterations contain $n$, $n - 1$, $\dots$, $3$, $2$ inner loop iterations respectively, summing up to $\frac{(n - 1)(n + 2)}{2}$.
|
||||
- **Space complexity of $O(1)$, in-place sort**: Uses constant extra space with pointers $i$ and $j$.
|
||||
- **Non-stable sort**: As shown in Figure 11-3, an element `nums[i]` may be swapped to the right of an equal element, causing their relative order to change.
|
||||
|
||||
|
@ -8,12 +8,12 @@ comments: true
|
||||
|
||||
- Bubble sort works by swapping adjacent elements. By adding a flag to enable early return, we can optimize the best-case time complexity of bubble sort to $O(n)$.
|
||||
- Insertion sort sorts each round by inserting elements from the unsorted interval into the correct position in the sorted interval. Although the time complexity of insertion sort is $O(n^2)$, it is very popular in sorting small amounts of data due to relatively fewer operations per unit.
|
||||
- Quick sort is based on sentinel partitioning operations. In sentinel partitioning, it's possible to always pick the worst pivot, leading to a time complexity degradation to $O(n^2)$. Introducing median or random pivots can reduce the probability of such degradation. Tail recursion can effectively reduce the recursion depth, optimizing the space complexity to $O(\log n)$.
|
||||
- Quick sort is based on sentinel partitioning operations. In sentinel partitioning, it's possible to always pick the worst pivot, leading to a time complexity degradation to $O(n^2)$. Introducing median or random pivots can reduce the probability of such degradation. Tail recursion effectively reduce the recursion depth, optimizing the space complexity to $O(\log n)$.
|
||||
- Merge sort includes dividing and merging two phases, typically embodying the divide-and-conquer strategy. In merge sort, sorting an array requires creating auxiliary arrays, resulting in a space complexity of $O(n)$; however, the space complexity for sorting a list can be optimized to $O(1)$.
|
||||
- Bucket sort consists of three steps: data bucketing, sorting within buckets, and merging results. It also embodies the divide-and-conquer strategy, suitable for very large datasets. The key to bucket sort is the even distribution of data.
|
||||
- Counting sort is a special case of bucket sort, which sorts by counting the occurrences of each data point. Counting sort is suitable for large datasets with a limited range of data and requires that data can be converted to positive integers.
|
||||
- Radix sort sorts data by sorting digit by digit, requiring data to be represented as fixed-length numbers.
|
||||
- Overall, we hope to find a sorting algorithm that has high efficiency, stability, in-place operation, and adaptability. However, like other data structures and algorithms, no sorting algorithm can meet all these conditions simultaneously. In practical applications, we need to choose the appropriate sorting algorithm based on the characteristics of the data.
|
||||
- Bucket sort consists of three steps: distributing data into buckets, sorting within each bucket, and merging results in bucket order. It also embodies the divide-and-conquer strategy, suitable for very large datasets. The key to bucket sort is the even distribution of data.
|
||||
- Counting sort is a variant of bucket sort, which sorts by counting the occurrences of each data point. Counting sort is suitable for large datasets with a limited range of data and requires data conversion to positive integers.
|
||||
- Radix sort processes data by sorting it digit by digit, requiring data to be represented as fixed-length numbers.
|
||||
- Overall, we seek sorting algorithm that has high efficiency, stability, in-place operation, and adaptability. However, like other data structures and algorithms, no sorting algorithm can meet all these conditions simultaneously. In practical applications, we need to choose the appropriate sorting algorithm based on the characteristics of the data.
|
||||
- Figure 11-19 compares mainstream sorting algorithms in terms of efficiency, stability, in-place nature, and adaptability.
|
||||
|
||||
{ class="animation-figure" }
|
||||
@ -32,7 +32,7 @@ It can be seen that the positions of students D and C have been swapped, disrupt
|
||||
|
||||
No, when using the leftmost element as the pivot, we must first "search from right to left" then "search from left to right". This conclusion is somewhat counterintuitive, so let's analyze the reason.
|
||||
|
||||
The last step of the sentinel partition `partition()` is to swap `nums[left]` and `nums[i]`. After the swap, the elements to the left of the pivot are all `<=` the pivot, **which requires that `nums[left] >= nums[i]` must hold before the last swap**. Suppose we "search from left to right" first, then if no element larger than the pivot is found, **we will exit the loop when `i == j`, possibly with `nums[j] == nums[i] > nums[left]`**. In other words, the final swap operation will exchange an element larger than the pivot to the left end of the array, causing the sentinel partition to fail.
|
||||
The last step of the sentinel partition `partition()` is to swap `nums[left]` and `nums[i]`. After the swap, the elements to the left of the pivot are all `<=` the pivot, **which requires that `nums[left] >= nums[i]` must hold before the last swap**. Suppose we "search from left to right" first, and if no element larger than the pivot is found, **we will exit the loop when `i == j`, possibly with `nums[j] == nums[i] > nums[left]`**. In other words, the final swap operation will exchange an element larger than the pivot to the left end of the array, causing the sentinel partition to fail.
|
||||
|
||||
For example, given the array `[0, 0, 0, 0, 1]`, if we first "search from left to right", the array after the sentinel partition is `[1, 0, 0, 0, 0]`, which is incorrect.
|
||||
|
||||
|
Reference in New Issue
Block a user