From 874e75d92de7b96406939dc0378950a388334ba7 Mon Sep 17 00:00:00 2001 From: krahets Date: Fri, 2 Jun 2023 01:35:02 +0800 Subject: [PATCH] build --- chapter_array_and_linkedlist/index.md | 11 +++++++ chapter_array_and_linkedlist/summary.md | 17 +++-------- chapter_backtracking/index.md | 11 +++++++ chapter_computational_complexity/index.md | 11 +++++++ chapter_data_structure/index.md | 11 +++++++ chapter_graph/index.md | 11 +++++++ chapter_hashing/index.md | 11 +++++++ chapter_heap/index.md | 11 +++++++ chapter_introduction/index.md | 11 +++++++ chapter_preface/index.md | 11 +++++++ chapter_searching/binary_search_edge.md | 34 +++++++++++++++++++-- chapter_searching/index.md | 11 +++++++ chapter_sorting/heap_sort.md | 36 +++++++++++++++++++++-- chapter_sorting/index.md | 11 +++++++ chapter_sorting/selection_sort.md | 16 +++++++++- chapter_stack_and_queue/index.md | 11 +++++++ chapter_tree/index.md | 11 +++++++ 17 files changed, 228 insertions(+), 18 deletions(-) create mode 100644 chapter_array_and_linkedlist/index.md create mode 100644 chapter_backtracking/index.md create mode 100644 chapter_computational_complexity/index.md create mode 100644 chapter_data_structure/index.md create mode 100644 chapter_graph/index.md create mode 100644 chapter_hashing/index.md create mode 100644 chapter_heap/index.md create mode 100644 chapter_introduction/index.md create mode 100644 chapter_preface/index.md create mode 100644 chapter_searching/index.md create mode 100644 chapter_sorting/index.md create mode 100644 chapter_stack_and_queue/index.md create mode 100644 chapter_tree/index.md diff --git a/chapter_array_and_linkedlist/index.md b/chapter_array_and_linkedlist/index.md new file mode 100644 index 000000000..0cde65045 --- /dev/null +++ b/chapter_array_and_linkedlist/index.md @@ -0,0 +1,11 @@ +--- +comments: true +--- + +# 4.   数组与链表 + +
+ +![数组与链表](../assets/covers/chapter_array_and_linkedlist.jpg){ width="70%" } + +
diff --git a/chapter_array_and_linkedlist/summary.md b/chapter_array_and_linkedlist/summary.md index 46614d4c4..c147e1964 100644 --- a/chapter_array_and_linkedlist/summary.md +++ b/chapter_array_and_linkedlist/summary.md @@ -8,7 +8,7 @@ comments: true - 数组支持随机访问、占用内存较少;但插入和删除元素效率低,且初始化后长度不可变。 - 链表通过更改指针实现高效的节点插入与删除,且可以灵活调整长度;但节点访问效率低、占用内存较多。常见的链表类型包括单向链表、循环链表、双向链表。 - 动态数组,又称列表,是基于数组实现的一种数据结构。它保留了数组的优势,同时可以灵活调整长度。列表的出现极大地提高了数组的易用性,但可能导致部分内存空间浪费。 -- 下表总结并对比了数组与链表的各项特性。 +- 下表总结并对比了数组与链表的各项特性与操作效率。
@@ -18,6 +18,9 @@ comments: true | 数据结构长度 | 长度不可变 | 长度可变 | | 内存使用率 | 占用内存少、缓存局部性好 | 占用内存多 | | 优势操作 | 随机访问 | 插入、删除 | +| 访问元素 | $O(1)$ | $O(N)$ | +| 添加元素 | $O(N)$ | $O(1)$ | +| 删除元素 | $O(N)$ | $O(1)$ |
@@ -25,18 +28,6 @@ comments: true 在计算机中,数据读写速度排序是“硬盘 < 内存 < CPU 缓存”。当我们访问数组元素时,计算机不仅会加载它,还会缓存其周围的其他数据,从而借助高速缓存来提升后续操作的执行速度。链表则不然,计算机只能挨个地缓存各个节点,这样的多次“搬运”降低了整体效率。 -- 下表对比了数组与链表在各种操作上的效率。 - -
- -| 操作 | 数组 | 链表 | -| ------- | ------ | ------ | -| 访问元素 | $O(1)$ | $O(N)$ | -| 添加元素 | $O(N)$ | $O(1)$ | -| 删除元素 | $O(N)$ | $O(1)$ | - -
- ## 4.4.1.   Q & A !!! question "数组存储在栈上和存储在堆上,对时间效率和空间效率是否有影响?" diff --git a/chapter_backtracking/index.md b/chapter_backtracking/index.md new file mode 100644 index 000000000..c1c8be927 --- /dev/null +++ b/chapter_backtracking/index.md @@ -0,0 +1,11 @@ +--- +comments: true +--- + +# 12.   回溯 + +
+ +![回溯](../assets/covers/chapter_backtracking.jpg){ width="70%" } + +
diff --git a/chapter_computational_complexity/index.md b/chapter_computational_complexity/index.md new file mode 100644 index 000000000..bb7b1dda2 --- /dev/null +++ b/chapter_computational_complexity/index.md @@ -0,0 +1,11 @@ +--- +comments: true +--- + +# 2.   复杂度分析 + +
+ +![复杂度分析](../assets/covers/chapter_complexity_analysis.jpg){ width="70%" } + +
diff --git a/chapter_data_structure/index.md b/chapter_data_structure/index.md new file mode 100644 index 000000000..1f0ef845a --- /dev/null +++ b/chapter_data_structure/index.md @@ -0,0 +1,11 @@ +--- +comments: true +--- + +# 3.   数据结构简介 + +
+ +![数据结构](../assets/covers/chapter_data_structure.jpg){ width="70%" } + +
diff --git a/chapter_graph/index.md b/chapter_graph/index.md new file mode 100644 index 000000000..8791c09f3 --- /dev/null +++ b/chapter_graph/index.md @@ -0,0 +1,11 @@ +--- +comments: true +--- + +# 9.   图 + +
+ +![图](../assets/covers/chapter_graph.jpg){ width="70%" } + +
diff --git a/chapter_hashing/index.md b/chapter_hashing/index.md new file mode 100644 index 000000000..66ba669b3 --- /dev/null +++ b/chapter_hashing/index.md @@ -0,0 +1,11 @@ +--- +comments: true +--- + +# 6.   散列表 + +
+ +![散列表](../assets/covers/chapter_hashing.jpg){ width="70%" } + +
diff --git a/chapter_heap/index.md b/chapter_heap/index.md new file mode 100644 index 000000000..9a63d5992 --- /dev/null +++ b/chapter_heap/index.md @@ -0,0 +1,11 @@ +--- +comments: true +--- + +# 8.   堆 + +
+ +![堆](../assets/covers/chapter_heap.jpg){ width="70%" } + +
diff --git a/chapter_introduction/index.md b/chapter_introduction/index.md new file mode 100644 index 000000000..92be5dd2b --- /dev/null +++ b/chapter_introduction/index.md @@ -0,0 +1,11 @@ +--- +comments: true +--- + +# 1.   引言 + +
+ +![引言](../assets/covers/chapter_introduction.jpg){ width="70%" } + +
diff --git a/chapter_preface/index.md b/chapter_preface/index.md new file mode 100644 index 000000000..c6c3bce86 --- /dev/null +++ b/chapter_preface/index.md @@ -0,0 +1,11 @@ +--- +comments: true +--- + +# 0.   写在前面 + +
+ +![写在前面](../assets/covers/chapter_preface.jpg){ width="70%" } + +
diff --git a/chapter_searching/binary_search_edge.md b/chapter_searching/binary_search_edge.md index 431c092dc..46d45e322 100644 --- a/chapter_searching/binary_search_edge.md +++ b/chapter_searching/binary_search_edge.md @@ -169,7 +169,22 @@ comments: true === "C#" ```csharp title="binary_search_edge.cs" - [class]{binary_search_edge}-[func]{binarySearchLeftEdge} + /* 二分查找最左一个元素 */ + int binarySearchLeftEdge(int[] nums, int target) { + int i = 0, j = nums.Length - 1; // 初始化双闭区间 [0, n-1] + while (i <= j) { + int m = i + (j - i) / 2; // 计算中点索引 m + if (nums[m] < target) + i = m + 1; // target 在区间 [m+1, j] 中 + else if (nums[m] > target) + j = m - 1; // target 在区间 [i, m-1] 中 + else + j = m - 1; // 首个小于 target 的元素在区间 [i, m-1] 中 + } + if (i == nums.Length || nums[i] != target) + return -1; // 未找到目标元素,返回 -1 + return i; + } ``` === "Swift" @@ -320,7 +335,22 @@ comments: true === "C#" ```csharp title="binary_search_edge.cs" - [class]{binary_search_edge}-[func]{binarySearchRightEdge} + /* 二分查找最右一个元素 */ + int binarySearchRightEdge(int[] nums, int target) { + int i = 0, j = nums.Length - 1; // 初始化双闭区间 [0, n-1] + while (i <= j) { + int m = i + (j - i) / 2; // 计算中点索引 m + if (nums[m] < target) + i = m + 1; // target 在区间 [m+1, j] 中 + else if (nums[m] > target) + j = m - 1; // target 在区间 [i, m-1] 中 + else + i = m + 1; // 首个大于 target 的元素在区间 [m+1, j] 中 + } + if (j < 0 || nums[j] != target) + return -1; // 未找到目标元素,返回 -1 + return j; + } ``` === "Swift" diff --git a/chapter_searching/index.md b/chapter_searching/index.md new file mode 100644 index 000000000..36d23cfe0 --- /dev/null +++ b/chapter_searching/index.md @@ -0,0 +1,11 @@ +--- +comments: true +--- + +# 10.   搜索 + +
+ +![搜索](../assets/covers/chapter_searching.jpg){ width="70%" } + +
diff --git a/chapter_sorting/heap_sort.md b/chapter_sorting/heap_sort.md index 82d0631c0..842584b32 100644 --- a/chapter_sorting/heap_sort.md +++ b/chapter_sorting/heap_sort.md @@ -254,9 +254,41 @@ comments: true === "C#" ```csharp title="heap_sort.cs" - [class]{heap_sort}-[func]{siftDown} + /* 堆的长度为 n ,从节点 i 开始,从顶至底堆化 */ + void siftDown(int[] nums, int n, int i) { + while (true) { + // 判断节点 i, l, r 中值最大的节点,记为 ma + int l = 2 * i + 1; + int r = 2 * i + 2; + int ma = i; + if (l < n && nums[l] > nums[ma]) + ma = l; + if (r < n && nums[r] > nums[ma]) + ma = r; + // 若节点 i 最大或索引 l, r 越界,则无需继续堆化,跳出 + if (ma == i) + break; + // 交换两节点 + (nums[ma], nums[i]) = (nums[i], nums[ma]); + // 循环向下堆化 + i = ma; + } + } - [class]{heap_sort}-[func]{heapSort} + /* 堆排序 */ + void heapSort(int[] nums) { + // 建堆操作:堆化除叶节点以外的其他所有节点 + for (int i = nums.Length / 2 - 1; i >= 0; i--) { + siftDown(nums, nums.Length, i); + } + // 从堆中提取最大元素,循环 n-1 轮 + for (int i = nums.Length - 1; i > 0; i--) { + // 交换根节点与最右叶节点(即交换首元素与尾元素) + (nums[i], nums[0]) = (nums[0], nums[i]); + // 以根节点为起点,从顶至底进行堆化 + siftDown(nums, i, 0); + } + } ``` === "Swift" diff --git a/chapter_sorting/index.md b/chapter_sorting/index.md new file mode 100644 index 000000000..70b9efa5d --- /dev/null +++ b/chapter_sorting/index.md @@ -0,0 +1,11 @@ +--- +comments: true +--- + +# 11.   排序 + +
+ +![排序](../assets/covers/chapter_sorting.jpg){ width="70%" } + +
diff --git a/chapter_sorting/selection_sort.md b/chapter_sorting/selection_sort.md index 8d200792e..f6551447b 100644 --- a/chapter_sorting/selection_sort.md +++ b/chapter_sorting/selection_sort.md @@ -152,7 +152,21 @@ comments: true === "C#" ```csharp title="selection_sort.cs" - [class]{selection_sort}-[func]{selectionSort} + /* 选择排序 */ + void selectionSort(int[] nums) { + int n = nums.Length; + // 外循环:未排序区间为 [i, n-1] + for (int i = 0; i < n - 1; i++) { + // 内循环:找到未排序区间内的最小元素 + int k = i; + for (int j = i + 1; j < n; j++) { + if (nums[j] < nums[k]) + k = j; // 记录最小元素的索引 + } + // 将该最小元素与未排序区间的首个元素交换 + (nums[k], nums[i]) = (nums[i], nums[k]); + } + } ``` === "Swift" diff --git a/chapter_stack_and_queue/index.md b/chapter_stack_and_queue/index.md new file mode 100644 index 000000000..c5508d70f --- /dev/null +++ b/chapter_stack_and_queue/index.md @@ -0,0 +1,11 @@ +--- +comments: true +--- + +# 5.   栈与队列 + +
+ +![栈与队列](../assets/covers/chapter_stack_and_queue.jpg){ width="70%" } + +
diff --git a/chapter_tree/index.md b/chapter_tree/index.md new file mode 100644 index 000000000..cb43fb8c0 --- /dev/null +++ b/chapter_tree/index.md @@ -0,0 +1,11 @@ +--- +comments: true +--- + +# 7.   树 + +
+ +![树](../assets/covers/chapter_tree.jpg){ width="70%" } + +