diff --git a/codes/c/chapter_sorting/insertion_sort.c b/codes/c/chapter_sorting/insertion_sort.c new file mode 100644 index 00000000..ae1f6e7d --- /dev/null +++ b/codes/c/chapter_sorting/insertion_sort.c @@ -0,0 +1,41 @@ +/* + * File: insertion_sort.c + * Created Time: 2022-12-29 + * Author: Listening (https://github.com/L-Super) + */ + +#include "../include/include.h" + +/* 插入排序 */ +void insertionSort(int nums[], int size) +{ + // 外循环:base = nums[1], nums[2], ..., nums[n-1] + for (int i = 1; i < size; i++) + { + int base = nums[i], j = i - 1; + // 内循环:将 base 插入到左边的正确位置 + while (j >= 0 && nums[j] > base) + { + // 1. 将 nums[j] 向右移动一位 + nums[j + 1] = nums[j]; + j--; + } + // 2. 将 base 赋值到正确位置 + nums[j + 1] = base; + } +} + +/* Driver Code */ +int main() +{ + int nums[] = {4, 1, 3, 1, 5, 2}; + insertionSort(nums, 6); + printf("插入排序完成后 nums = \n"); + for (int i = 0; i < 6; i++) + { + printf("%d ", nums[i]); + } + printf("\n"); + + return 0; +} diff --git a/codes/cpp/chapter_sorting/merge_sort.cpp b/codes/cpp/chapter_sorting/merge_sort.cpp index 7c9a41c8..b406529a 100644 --- a/codes/cpp/chapter_sorting/merge_sort.cpp +++ b/codes/cpp/chapter_sorting/merge_sort.cpp @@ -25,10 +25,10 @@ void merge(vector& nums, int left, int mid, int right) { // 若“左子数组已全部合并完”,则选取右子数组元素,并且 j++ if (i > leftEnd) nums[k] = tmp[j++]; - // 否则,若“右子数组已全部合并完”或“左子数组元素 < 右子数组元素”,则选取左子数组元素,并且 i++ + // 否则,若“右子数组已全部合并完”或“左子数组元素 <= 右子数组元素”,则选取左子数组元素,并且 i++ else if (j > rightEnd || tmp[i] <= tmp[j]) nums[k] = tmp[i++]; - // 否则,若“左子数组元素 > 右子数组元素”,则选取右子数组元素,并且 j++ + // 否则,若“左右子数组都未全部合并完”且“左子数组元素 > 右子数组元素”,则选取右子数组元素,并且 j++ else nums[k] = tmp[j++]; } diff --git a/codes/csharp/chapter_sorting/merge_sort.cs b/codes/csharp/chapter_sorting/merge_sort.cs index 04ac8acd..e66ae5e2 100644 --- a/codes/csharp/chapter_sorting/merge_sort.cs +++ b/codes/csharp/chapter_sorting/merge_sort.cs @@ -31,10 +31,10 @@ namespace hello_algo.chapter_sorting // 若“左子数组已全部合并完”,则选取右子数组元素,并且 j++ if (i > leftEnd) nums[k] = tmp[j++]; - // 否则,若“右子数组已全部合并完”或“左子数组元素 < 右子数组元素”,则选取左子数组元素,并且 i++ + // 否则,若“右子数组已全部合并完”或“左子数组元素 <= 右子数组元素”,则选取左子数组元素,并且 i++ else if (j > rightEnd || tmp[i] <= tmp[j]) nums[k] = tmp[i++]; - // 否则,若“左子数组元素 > 右子数组元素”,则选取右子数组元素,并且 j++ + // 否则,若“左右子数组都未全部合并完”且“左子数组元素 > 右子数组元素”,则选取右子数组元素,并且 j++ else nums[k] = tmp[j++]; } diff --git a/codes/csharp/chapter_stack_and_queue/deque.cs b/codes/csharp/chapter_stack_and_queue/deque.cs new file mode 100644 index 00000000..d9f7a509 --- /dev/null +++ b/codes/csharp/chapter_stack_and_queue/deque.cs @@ -0,0 +1,49 @@ +/** + * File: deque.cs + * Created Time: 2022-12-30 + * Author: moonache (microin1301@outlook.com) + */ + +using NUnit.Framework; + +namespace hello_algo.chapter_stack_and_queue +{ + public class deque + { + [Test] + public void Test() + { + /* 初始化双向队列 */ + // 在 C# 中,将链表 LinkedList 看作双向队列来使用 + LinkedList deque = new LinkedList(); + + /* 元素入队 */ + deque.AddLast(2); // 添加至队尾 + deque.AddLast(5); + deque.AddLast(4); + deque.AddFirst(3); // 添加至队首 + deque.AddFirst(1); + Console.WriteLine("双向队列 deque = " + String.Join(",", deque.ToArray())); + + /* 访问元素 */ + int peekFirst = deque.First.Value; // 队首元素 + Console.WriteLine("队首元素 peekFirst = " + peekFirst); + int peekLast = deque.Last.Value; // 队尾元素 + Console.WriteLine("队尾元素 peekLast = " + peekLast); + + /* 元素出队 */ + deque.RemoveFirst(); // 队首元素出队 + Console.WriteLine("队首元素出队后 deque = " + String.Join(",", deque.ToArray())); + deque.RemoveLast(); // 队尾元素出队 + Console.WriteLine("队尾元素出队后 deque = " + String.Join(",", deque.ToArray())); + + /* 获取双向队列的长度 */ + int size = deque.Count; + Console.WriteLine("双向队列长度 size = " + size); + + /* 判断双向队列是否为空 */ + bool isEmpty = deque.Count == 0; + Console.WriteLine("双向队列是否为空 = " + isEmpty); + } + } +} diff --git a/codes/go/chapter_array_and_linkedlist/array.go b/codes/go/chapter_array_and_linkedlist/array.go index 1a02bea4..1f1a78be 100644 --- a/codes/go/chapter_array_and_linkedlist/array.go +++ b/codes/go/chapter_array_and_linkedlist/array.go @@ -1,6 +1,6 @@ // File: array.go // Created Time: 2022-12-29 -// Author: cathay (cathaycchen@gmail.com) +// Author: GuoWei (gongguowei01@gmail.com), cathay (cathaycchen@gmail.com) package chapter_array_and_linkedlist @@ -9,11 +9,18 @@ import ( "math/rand" ) +/** +我们将 Go 中的 Slice 切片看作 Array 数组,降低理解成本, +有利于我们将关注点放在数据结构与算法上。 +*/ + /* 随机返回一个数组元素 */ -func randomAccess(nums []int) int { +func randomAccess(nums []int) (randomNum int) { + // 在区间 [0, nums.length) 中随机抽取一个数字 randomIndex := rand.Intn(len(nums)) - randomNum := nums[randomIndex] - return randomNum + // 获取并返回随机元素 + randomNum = nums[randomIndex] + return } /* 扩展数组长度 */ @@ -21,8 +28,8 @@ func extend(nums []int, enlarge int) []int { // 初始化一个扩展长度后的数组 res := make([]int, len(nums)+enlarge) // 将原数组中的所有元素复制到新数组 - for i, num := range nums { - res[i] = num + for i := 0; i < len(nums); i++ { + res[i] = nums[i] } // 返回扩展后的新数组 return res @@ -53,22 +60,20 @@ func traverse(nums []int) { for i := 0; i < len(nums); i++ { count++ } - fmt.Println(count) - - count = 0 // 直接遍历数组 - for range nums { - count++ + for index, val := range nums { + fmt.Printf("index:%v value:%v\n", index, val) } - fmt.Println(count) } -/* 在数组中查找指定元素,返回第一个索引位置,未查找到则返回 -1 */ -func find(nums []int, target int) int { +/* 在数组中查找指定元素 */ +func find(nums []int, target int) (index int) { + index = -1 for i := 0; i < len(nums); i++ { if nums[i] == target { - return i + index = i + break } } - return -1 + return } diff --git a/codes/go/chapter_array_and_linkedlist/array_test.go b/codes/go/chapter_array_and_linkedlist/array_test.go index 4938ac50..7dff6a03 100644 --- a/codes/go/chapter_array_and_linkedlist/array_test.go +++ b/codes/go/chapter_array_and_linkedlist/array_test.go @@ -1,9 +1,14 @@ // File: array_test.go // Created Time: 2022-12-29 -// Author: cathay (cathaycchen@gmail.com) +// Author: GuoWei (gongguowei01@gmail.com), cathay (cathaycchen@gmail.com) package chapter_array_and_linkedlist +/** +我们将 Go 中的 Slice 切片看作 Array 数组。因为这样可以 +降低理解成本,利于我们将关注点放在数据结构与算法上。 +*/ + import ( "fmt" "testing" @@ -12,10 +17,9 @@ import ( /* Driver Code */ func TestArray(t *testing.T) { /* 初始化数组 */ - var arr = [5]int{} + var arr []int fmt.Println("数组 arr =", arr) - - var nums = []int{1, 3, 2, 5, 4} + nums := []int{1, 3, 2, 5, 4} fmt.Println("数组 nums =", nums) /* 随机访问 */ @@ -32,7 +36,7 @@ func TestArray(t *testing.T) { /* 删除元素 */ remove(nums, 2) - fmt.Println("删除索引 2 处的元素,得到 nums = ", nums) + fmt.Println("删除索引 2 处的元素,得到 nums =", nums) /* 遍历数组 */ traverse(nums) diff --git a/codes/go/chapter_sorting/merge_sort/merge_sort.go b/codes/go/chapter_sorting/merge_sort/merge_sort.go index 830e2dfa..f2910600 100644 --- a/codes/go/chapter_sorting/merge_sort/merge_sort.go +++ b/codes/go/chapter_sorting/merge_sort/merge_sort.go @@ -25,11 +25,11 @@ func merge(nums []int, left, mid, right int) { if i > left_end { nums[k] = tmp[j] j++ - // 否则,若“右子数组已全部合并完”或“左子数组元素 < 右子数组元素”,则选取左子数组元素,并且 i++ + // 否则,若“右子数组已全部合并完”或“左子数组元素 <= 右子数组元素”,则选取左子数组元素,并且 i++ } else if j > right_end || tmp[i] <= tmp[j] { nums[k] = tmp[i] i++ - // 否则,若“左子数组元素 > 右子数组元素”,则选取右子数组元素,并且 j++ + // 否则,若“左右子数组都未全部合并完”且“左子数组元素 > 右子数组元素”,则选取右子数组元素,并且 j++ } else { nums[k] = tmp[j] j++ diff --git a/codes/java/chapter_sorting/merge_sort.java b/codes/java/chapter_sorting/merge_sort.java index 498a6fd8..fe9f183a 100644 --- a/codes/java/chapter_sorting/merge_sort.java +++ b/codes/java/chapter_sorting/merge_sort.java @@ -28,10 +28,10 @@ public class merge_sort { // 若“左子数组已全部合并完”,则选取右子数组元素,并且 j++ if (i > leftEnd) nums[k] = tmp[j++]; - // 否则,若“右子数组已全部合并完”或“左子数组元素 < 右子数组元素”,则选取左子数组元素,并且 i++ + // 否则,若“右子数组已全部合并完”或“左子数组元素 <= 右子数组元素”,则选取左子数组元素,并且 i++ else if (j > rightEnd || tmp[i] <= tmp[j]) nums[k] = tmp[i++]; - // 否则,若“左子数组元素 > 右子数组元素”,则选取右子数组元素,并且 j++ + // 否则,若“左右子数组都未全部合并完”且“左子数组元素 > 右子数组元素”,则选取右子数组元素,并且 j++ else nums[k] = tmp[j++]; } diff --git a/codes/javascript/chapter_sorting/merge_sort.js b/codes/javascript/chapter_sorting/merge_sort.js index 31860ce1..b00c17bc 100644 --- a/codes/javascript/chapter_sorting/merge_sort.js +++ b/codes/javascript/chapter_sorting/merge_sort.js @@ -23,10 +23,10 @@ function merge(nums, left, mid, right) { // 若“左子数组已全部合并完”,则选取右子数组元素,并且 j++ if (i > leftEnd) { nums[k] = tmp[j++]; - // 否则,若“右子数组已全部合并完”或“左子数组元素 < 右子数组元素”,则选取左子数组元素,并且 i++ + // 否则,若“右子数组已全部合并完”或“左子数组元素 <= 右子数组元素”,则选取左子数组元素,并且 i++ } else if (j > rightEnd || tmp[i] <= tmp[j]) { nums[k] = tmp[i++]; - // 否则,若“左子数组元素 > 右子数组元素”,则选取右子数组元素,并且 j++ + // 否则,若“左右子数组都未全部合并完”且“左子数组元素 > 右子数组元素”,则选取右子数组元素,并且 j++ } else { nums[k] = tmp[j++]; } diff --git a/codes/python/chapter_sorting/merge_sort.py b/codes/python/chapter_sorting/merge_sort.py index f3fb538a..5fa7d83a 100644 --- a/codes/python/chapter_sorting/merge_sort.py +++ b/codes/python/chapter_sorting/merge_sort.py @@ -28,11 +28,11 @@ def merge(nums, left, mid, right): if i > left_end: nums[k] = tmp[j] j += 1 - # 否则,若“右子数组已全部合并完”或“左子数组元素 < 右子数组元素”,则选取左子数组元素,并且 i++ + # 否则,若“右子数组已全部合并完”或“左子数组元素 <= 右子数组元素”,则选取左子数组元素,并且 i++ elif j > right_end or tmp[i] <= tmp[j]: nums[k] = tmp[i] i += 1 - # 否则,若“左子数组元素 > 右子数组元素”,则选取右子数组元素,并且 j++ + # 否则,若“左右子数组都未全部合并完”且“左子数组元素 > 右子数组元素”,则选取右子数组元素,并且 j++ else: nums[k] = tmp[j] j += 1 diff --git a/codes/typescript/chapter_sorting/merge_sort.ts b/codes/typescript/chapter_sorting/merge_sort.ts index 44fb4b8d..a4cb5e1c 100644 --- a/codes/typescript/chapter_sorting/merge_sort.ts +++ b/codes/typescript/chapter_sorting/merge_sort.ts @@ -23,10 +23,10 @@ function merge(nums: number[], left: number, mid: number, right: number): void { // 若“左子数组已全部合并完”,则选取右子数组元素,并且 j++ if (i > leftEnd) { nums[k] = tmp[j++]; - // 否则,若“右子数组已全部合并完”或“左子数组元素 < 右子数组元素”,则选取左子数组元素,并且 i++ + // 否则,若“右子数组已全部合并完”或“左子数组元素 <= 右子数组元素”,则选取左子数组元素,并且 i++ } else if (j > rightEnd || tmp[i] <= tmp[j]) { nums[k] = tmp[i++]; - // 否则,若“左子数组元素 > 右子数组元素”,则选取右子数组元素,并且 j++ + // 否则,若“左右子数组都未全部合并完”且“左子数组元素 > 右子数组元素”,则选取右子数组元素,并且 j++ } else { nums[k] = tmp[j++]; } diff --git a/docs/chapter_array_and_linkedlist/array.md b/docs/chapter_array_and_linkedlist/array.md index bd98a914..5168a5d5 100644 --- a/docs/chapter_array_and_linkedlist/array.md +++ b/docs/chapter_array_and_linkedlist/array.md @@ -43,8 +43,9 @@ comments: true === "Go" ```go title="array.go" - var arr = [5]int{} - var nums = [5]int{1, 3, 2, 5, 4} + /* 初始化数组 */ + var arr []int + nums := []int{1, 3, 2, 5, 4} ``` === "JavaScript" @@ -135,10 +136,12 @@ elementAddr = firtstElementAddr + elementLength * elementIndex ```go title="array.go" /* 随机返回一个数组元素 */ - func randomAccess(nums []int) int { + func randomAccess(nums [5]int) (randomNum int) { + // 在区间 [0, nums.length) 中随机抽取一个数字 randomIndex := rand.Intn(len(nums)) - randomNum := nums[randomIndex] - return randomNum + // 获取并返回随机元素 + randomNum = nums[randomIndex] + return } ``` @@ -387,9 +390,10 @@ elementAddr = firtstElementAddr + elementLength * elementIndex ```go title="array.go" /* 在数组的索引 index 处插入元素 num */ - func insert(nums []int, size int, num int, index int) { + func insert(nums []int, num int, index int) { // 把索引 index 以及之后的所有元素向后移动一位 - for i := size - 1; i > index; i-- { + // 如果超出了数组长度,会被直接舍弃 + for i := len(nums) - 1; i > index; i-- { nums[i] = nums[i-1] } // 将 num 赋给 index 处元素 @@ -397,9 +401,9 @@ elementAddr = firtstElementAddr + elementLength * elementIndex } /* 删除索引 index 处元素 */ - func remove(nums []int, size int, index int) { + func remove(nums []int, index int) { // 把索引 index 之后的所有元素向前移动一位 - for i := index; i < size-1; i++ { + for i := index; i < len(nums) - 1; i++ { nums[i] = nums[i+1] } } @@ -647,14 +651,16 @@ elementAddr = firtstElementAddr + elementLength * elementIndex === "Go" ```go title="array.go" - /* 在数组中查找指定元素,返回第一个索引位置,未查找到则返回 -1 */ - func find(nums []int, target int) int { + /* 在数组中查找指定元素 */ + func find(nums []int, target int) (index int) { + index = -1 for i := 0; i < len(nums); i++ { if nums[i] == target { - return i + index = i + break } } - return -1 + return } ``` diff --git a/docs/chapter_computational_complexity/space_complexity.md b/docs/chapter_computational_complexity/space_complexity.md index df5d98f3..4c0d2d1b 100644 --- a/docs/chapter_computational_complexity/space_complexity.md +++ b/docs/chapter_computational_complexity/space_complexity.md @@ -219,11 +219,11 @@ comments: true ```go title="" func algorithm(n int) { - a := 0 // O(1) - b := make([]int, 10000) // O(1) + a := 0 // O(1) + b := make([]int, 10000) // O(1) var nums []int if n > 10 { - nums = make([]int, 10000) // O(n) + nums := make([]int, n) // O(n) } fmt.Println(a, b, nums) } @@ -838,7 +838,7 @@ $$ ``` -在以下递归函数中,同时存在 $n$ 个未返回的 `algorihtm()` ,并且每个函数中都初始化了一个数组,长度分别为 $n, n-1, n-2, ..., 2, 1$ ,平均长度为 $\frac{n}{2}$ ,因此总体使用 $O(n^2)$ 空间。 +在以下递归函数中,同时存在 $n$ 个未返回的 `algorithm()` ,并且每个函数中都初始化了一个数组,长度分别为 $n, n-1, n-2, ..., 2, 1$ ,平均长度为 $\frac{n}{2}$ ,因此总体使用 $O(n^2)$ 空间。 === "Java" diff --git a/docs/chapter_computational_complexity/time_complexity.md b/docs/chapter_computational_complexity/time_complexity.md index aafb0d89..539c9b5c 100644 --- a/docs/chapter_computational_complexity/time_complexity.md +++ b/docs/chapter_computational_complexity/time_complexity.md @@ -1442,7 +1442,7 @@ $$ ### 对数阶 $O(\log n)$ -对数阶与指数阶正好相反,后者反映“每轮增加到两倍的情况”,而前者反映“每轮缩减到一半的情况”。对数阶仅次于常数阶,时间增长的很慢,是理想的时间复杂度。 +对数阶与指数阶正好相反,后者反映“每轮增加到两倍的情况”,而前者反映“每轮缩减到一半的情况”。对数阶仅次于常数阶,时间增长得很慢,是理想的时间复杂度。 对数阶常出现于「二分查找」和「分治算法」中,体现“一分为多”、“化繁为简”的算法思想。 diff --git a/docs/chapter_data_structure/classification_of_data_strcuture.assets/classification_logic_structure.png b/docs/chapter_data_structure/classification_of_data_structure.assets/classification_logic_structure.png similarity index 100% rename from docs/chapter_data_structure/classification_of_data_strcuture.assets/classification_logic_structure.png rename to docs/chapter_data_structure/classification_of_data_structure.assets/classification_logic_structure.png diff --git a/docs/chapter_data_structure/classification_of_data_strcuture.assets/classification_phisical_structure.png b/docs/chapter_data_structure/classification_of_data_structure.assets/classification_phisical_structure.png similarity index 100% rename from docs/chapter_data_structure/classification_of_data_strcuture.assets/classification_phisical_structure.png rename to docs/chapter_data_structure/classification_of_data_structure.assets/classification_phisical_structure.png diff --git a/docs/chapter_data_structure/classification_of_data_strcuture.md b/docs/chapter_data_structure/classification_of_data_structure.md similarity index 93% rename from docs/chapter_data_structure/classification_of_data_strcuture.md rename to docs/chapter_data_structure/classification_of_data_structure.md index b41266bb..d4397c5d 100644 --- a/docs/chapter_data_structure/classification_of_data_strcuture.md +++ b/docs/chapter_data_structure/classification_of_data_structure.md @@ -15,7 +15,7 @@ comments: true - **线性数据结构:** 数组、链表、栈、队列、哈希表; - **非线性数据结构:** 树、图、堆、哈希表; -![classification_logic_structure](classification_of_data_strcuture.assets/classification_logic_structure.png) +![classification_logic_structure](classification_of_data_structure.assets/classification_logic_structure.png)

Fig. 线性与非线性数据结构

@@ -27,7 +27,7 @@ comments: true **「物理结构」反映了数据在计算机内存中的存储方式。** 从本质上看,分别是 **数组的连续空间存储** 和 **链表的离散空间存储** 。物理结构从底层上决定了数据的访问、更新、增删等操作方法,在时间效率和空间效率方面呈现出此消彼长的特性。 -![classification_phisical_structure](classification_of_data_strcuture.assets/classification_phisical_structure.png) +![classification_phisical_structure](classification_of_data_structure.assets/classification_phisical_structure.png)

Fig. 连续空间存储与离散空间存储

diff --git a/docs/chapter_sorting/insertion_sort.md b/docs/chapter_sorting/insertion_sort.md index 1614b5c3..faaf3835 100644 --- a/docs/chapter_sorting/insertion_sort.md +++ b/docs/chapter_sorting/insertion_sort.md @@ -135,7 +135,24 @@ comments: true === "C" ```c title="insertion_sort.c" - + /* 插入排序 */ + void insertionSort(int nums[], int size) + { + // 外循环:base = nums[1], nums[2], ..., nums[n-1] + for (int i = 1; i < size; i++) + { + int base = nums[i], j = i - 1; + // 内循环:将 base 插入到左边的正确位置 + while (j >= 0 && nums[j] > base) + { + // 1. 将 nums[j] 向右移动一位 + nums[j + 1] = nums[j]; + j--; + } + // 2. 将 base 赋值到正确位置 + nums[j + 1] = base; + } + } ``` === "C#" diff --git a/docs/chapter_sorting/merge_sort.md b/docs/chapter_sorting/merge_sort.md index 9ee98427..48a5046c 100644 --- a/docs/chapter_sorting/merge_sort.md +++ b/docs/chapter_sorting/merge_sort.md @@ -81,10 +81,10 @@ comments: true // 若“左子数组已全部合并完”,则选取右子数组元素,并且 j++ if (i > leftEnd) nums[k] = tmp[j++]; - // 否则,若“右子数组已全部合并完”或“左子数组元素 < 右子数组元素”,则选取左子数组元素,并且 i++ + // 否则,若“右子数组已全部合并完”或“左子数组元素 <= 右子数组元素”,则选取左子数组元素,并且 i++ else if (j > rightEnd || tmp[i] <= tmp[j]) nums[k] = tmp[i++]; - // 否则,若“左子数组元素 > 右子数组元素”,则选取右子数组元素,并且 j++ + // 否则,若“左右子数组都未全部合并完”且“左子数组元素 > 右子数组元素”,则选取右子数组元素,并且 j++ else nums[k] = tmp[j++]; } @@ -125,10 +125,10 @@ comments: true // 若“左子数组已全部合并完”,则选取右子数组元素,并且 j++ if (i > leftEnd) nums[k] = tmp[j++]; - // 否则,若“右子数组已全部合并完”或“左子数组元素 < 右子数组元素”,则选取左子数组元素,并且 i++ + // 否则,若“右子数组已全部合并完”或“左子数组元素 <= 右子数组元素”,则选取左子数组元素,并且 i++ else if (j > rightEnd || tmp[i] <= tmp[j]) nums[k] = tmp[i++]; - // 否则,若“左子数组元素 > 右子数组元素”,则选取右子数组元素,并且 j++ + // 否则,若“左右子数组都未全部合并完”且“左子数组元素 > 右子数组元素”,则选取右子数组元素,并且 j++ else nums[k] = tmp[j++]; } @@ -170,11 +170,11 @@ comments: true if i > left_end: nums[k] = tmp[j] j += 1 - # 否则,若“右子数组已全部合并完”或“左子数组元素 < 右子数组元素”,则选取左子数组元素,并且 i++ + # 否则,若“右子数组已全部合并完”或“左子数组元素 <= 右子数组元素”,则选取左子数组元素,并且 i++ elif j > right_end or tmp[i] <= tmp[j]: nums[k] = tmp[i] i += 1 - # 否则,若“左子数组元素 > 右子数组元素”,则选取右子数组元素,并且 j++ + # 否则,若“左右子数组都未全部合并完”且“左子数组元素 > 右子数组元素”,则选取右子数组元素,并且 j++ else: nums[k] = tmp[j] j += 1 @@ -218,11 +218,11 @@ comments: true if i > left_end { nums[k] = tmp[j] j++ - // 否则,若“右子数组已全部合并完”或“左子数组元素 < 右子数组元素”,则选取左子数组元素,并且 i++ + // 否则,若“右子数组已全部合并完”或“左子数组元素 <= 右子数组元素”,则选取左子数组元素,并且 i++ } else if j > right_end || tmp[i] <= tmp[j] { nums[k] = tmp[i] i++ - // 否则,若“左子数组元素 > 右子数组元素”,则选取右子数组元素,并且 j++ + // 否则,若“左右子数组都未全部合并完”且“左子数组元素 > 右子数组元素”,则选取右子数组元素,并且 j++ } else { nums[k] = tmp[j] j++ @@ -267,10 +267,10 @@ comments: true // 若“左子数组已全部合并完”,则选取右子数组元素,并且 j++ if (i > leftEnd) { nums[k] = tmp[j++]; - // 否则,若“右子数组已全部合并完”或“左子数组元素 < 右子数组元素”,则选取左子数组元素,并且 i++ + // 否则,若“右子数组已全部合并完”或“左子数组元素 <= 右子数组元素”,则选取左子数组元素,并且 i++ } else if (j > rightEnd || tmp[i] <= tmp[j]) { nums[k] = tmp[i++]; - // 否则,若“左子数组元素 > 右子数组元素”,则选取右子数组元素,并且 j++ + // 否则,若“左右子数组都未全部合并完”且“左子数组元素 > 右子数组元素”,则选取右子数组元素,并且 j++ } else { nums[k] = tmp[j++]; } @@ -312,10 +312,10 @@ comments: true // 若“左子数组已全部合并完”,则选取右子数组元素,并且 j++ if (i > leftEnd) { nums[k] = tmp[j++]; - // 否则,若“右子数组已全部合并完”或“左子数组元素 < 右子数组元素”,则选取左子数组元素,并且 i++ + // 否则,若“右子数组已全部合并完”或“左子数组元素 <= 右子数组元素”,则选取左子数组元素,并且 i++ } else if (j > rightEnd || tmp[i] <= tmp[j]) { nums[k] = tmp[i++]; - // 否则,若“左子数组元素 > 右子数组元素”,则选取右子数组元素,并且 j++ + // 否则,若“左右子数组都未全部合并完”且“左子数组元素 > 右子数组元素”,则选取右子数组元素,并且 j++ } else { nums[k] = tmp[j++]; } @@ -365,10 +365,10 @@ comments: true // 若“左子数组已全部合并完”,则选取右子数组元素,并且 j++ if (i > leftEnd) nums[k] = tmp[j++]; - // 否则,若“右子数组已全部合并完”或“左子数组元素 < 右子数组元素”,则选取左子数组元素,并且 i++ + // 否则,若“右子数组已全部合并完”或“左子数组元素 <= 右子数组元素”,则选取左子数组元素,并且 i++ else if (j > rightEnd || tmp[i] <= tmp[j]) nums[k] = tmp[i++]; - // 否则,若“左子数组元素 > 右子数组元素”,则选取右子数组元素,并且 j++ + // 否则,若“左右子数组都未全部合并完”且“左子数组元素 > 右子数组元素”,则选取右子数组元素,并且 j++ else nums[k] = tmp[j++]; } diff --git a/docs/chapter_stack_and_queue/deque.md b/docs/chapter_stack_and_queue/deque.md index ca55f822..72106360 100644 --- a/docs/chapter_stack_and_queue/deque.md +++ b/docs/chapter_stack_and_queue/deque.md @@ -167,5 +167,28 @@ comments: true === "C#" ```csharp title="deque.cs" + /* 初始化双向队列 */ + // 在 C# 中,将链表 LinkedList 看作双向队列来使用 + LinkedList deque = new LinkedList(); + /* 元素入队 */ + deque.AddLast(2); // 添加至队尾 + deque.AddLast(5); + deque.AddLast(4); + deque.AddFirst(3); // 添加至队首 + deque.AddFirst(1); + + /* 访问元素 */ + int peekFirst = deque.First.Value; // 队首元素 + int peekLast = deque.Last.Value; // 队尾元素 + + /* 元素出队 */ + deque.RemoveFirst(); // 队首元素出队 + deque.RemoveLast(); // 队尾元素出队 + + /* 获取双向队列的长度 */ + int size = deque.Count; + + /* 判断双向队列是否为空 */ + bool isEmpty = deque.Count == 0; ``` diff --git a/mkdocs.yml b/mkdocs.yml index 4334382f..4aca74c2 100644 --- a/mkdocs.yml +++ b/mkdocs.yml @@ -139,7 +139,7 @@ nav: - 小结: chapter_computational_complexity/summary.md - 数据结构简介: - 数据与内存: chapter_data_structure/data_and_memory.md - - 数据结构分类: chapter_data_structure/classification_of_data_strcuture.md + - 数据结构分类: chapter_data_structure/classification_of_data_structure.md - 小结: chapter_data_structure/summary.md - 数组与链表: - 数组(Array): chapter_array_and_linkedlist/array.md