mirror of
https://github.com/krahets/hello-algo.git
synced 2024-12-27 01:46:31 +08:00
build
This commit is contained in:
parent
8cf488bed2
commit
874e75d92d
17 changed files with 228 additions and 18 deletions
11
chapter_array_and_linkedlist/index.md
Normal file
11
chapter_array_and_linkedlist/index.md
Normal file
|
@ -0,0 +1,11 @@
|
|||
---
|
||||
comments: true
|
||||
---
|
||||
|
||||
# 4. 数组与链表
|
||||
|
||||
<div class="center-table" markdown>
|
||||
|
||||
![数组与链表](../assets/covers/chapter_array_and_linkedlist.jpg){ width="70%" }
|
||||
|
||||
</div>
|
|
@ -8,7 +8,7 @@ comments: true
|
|||
- 数组支持随机访问、占用内存较少;但插入和删除元素效率低,且初始化后长度不可变。
|
||||
- 链表通过更改指针实现高效的节点插入与删除,且可以灵活调整长度;但节点访问效率低、占用内存较多。常见的链表类型包括单向链表、循环链表、双向链表。
|
||||
- 动态数组,又称列表,是基于数组实现的一种数据结构。它保留了数组的优势,同时可以灵活调整长度。列表的出现极大地提高了数组的易用性,但可能导致部分内存空间浪费。
|
||||
- 下表总结并对比了数组与链表的各项特性。
|
||||
- 下表总结并对比了数组与链表的各项特性与操作效率。
|
||||
|
||||
<div class="center-table" markdown>
|
||||
|
||||
|
@ -18,6 +18,9 @@ comments: true
|
|||
| 数据结构长度 | 长度不可变 | 长度可变 |
|
||||
| 内存使用率 | 占用内存少、缓存局部性好 | 占用内存多 |
|
||||
| 优势操作 | 随机访问 | 插入、删除 |
|
||||
| 访问元素 | $O(1)$ | $O(N)$ |
|
||||
| 添加元素 | $O(N)$ | $O(1)$ |
|
||||
| 删除元素 | $O(N)$ | $O(1)$ |
|
||||
|
||||
</div>
|
||||
|
||||
|
@ -25,18 +28,6 @@ comments: true
|
|||
|
||||
在计算机中,数据读写速度排序是“硬盘 < 内存 < CPU 缓存”。当我们访问数组元素时,计算机不仅会加载它,还会缓存其周围的其他数据,从而借助高速缓存来提升后续操作的执行速度。链表则不然,计算机只能挨个地缓存各个节点,这样的多次“搬运”降低了整体效率。
|
||||
|
||||
- 下表对比了数组与链表在各种操作上的效率。
|
||||
|
||||
<div class="center-table" markdown>
|
||||
|
||||
| 操作 | 数组 | 链表 |
|
||||
| ------- | ------ | ------ |
|
||||
| 访问元素 | $O(1)$ | $O(N)$ |
|
||||
| 添加元素 | $O(N)$ | $O(1)$ |
|
||||
| 删除元素 | $O(N)$ | $O(1)$ |
|
||||
|
||||
</div>
|
||||
|
||||
## 4.4.1. Q & A
|
||||
|
||||
!!! question "数组存储在栈上和存储在堆上,对时间效率和空间效率是否有影响?"
|
||||
|
|
11
chapter_backtracking/index.md
Normal file
11
chapter_backtracking/index.md
Normal file
|
@ -0,0 +1,11 @@
|
|||
---
|
||||
comments: true
|
||||
---
|
||||
|
||||
# 12. 回溯
|
||||
|
||||
<div class="center-table" markdown>
|
||||
|
||||
![回溯](../assets/covers/chapter_backtracking.jpg){ width="70%" }
|
||||
|
||||
</div>
|
11
chapter_computational_complexity/index.md
Normal file
11
chapter_computational_complexity/index.md
Normal file
|
@ -0,0 +1,11 @@
|
|||
---
|
||||
comments: true
|
||||
---
|
||||
|
||||
# 2. 复杂度分析
|
||||
|
||||
<div class="center-table" markdown>
|
||||
|
||||
![复杂度分析](../assets/covers/chapter_complexity_analysis.jpg){ width="70%" }
|
||||
|
||||
</div>
|
11
chapter_data_structure/index.md
Normal file
11
chapter_data_structure/index.md
Normal file
|
@ -0,0 +1,11 @@
|
|||
---
|
||||
comments: true
|
||||
---
|
||||
|
||||
# 3. 数据结构简介
|
||||
|
||||
<div class="center-table" markdown>
|
||||
|
||||
![数据结构](../assets/covers/chapter_data_structure.jpg){ width="70%" }
|
||||
|
||||
</div>
|
11
chapter_graph/index.md
Normal file
11
chapter_graph/index.md
Normal file
|
@ -0,0 +1,11 @@
|
|||
---
|
||||
comments: true
|
||||
---
|
||||
|
||||
# 9. 图
|
||||
|
||||
<div class="center-table" markdown>
|
||||
|
||||
![图](../assets/covers/chapter_graph.jpg){ width="70%" }
|
||||
|
||||
</div>
|
11
chapter_hashing/index.md
Normal file
11
chapter_hashing/index.md
Normal file
|
@ -0,0 +1,11 @@
|
|||
---
|
||||
comments: true
|
||||
---
|
||||
|
||||
# 6. 散列表
|
||||
|
||||
<div class="center-table" markdown>
|
||||
|
||||
![散列表](../assets/covers/chapter_hashing.jpg){ width="70%" }
|
||||
|
||||
</div>
|
11
chapter_heap/index.md
Normal file
11
chapter_heap/index.md
Normal file
|
@ -0,0 +1,11 @@
|
|||
---
|
||||
comments: true
|
||||
---
|
||||
|
||||
# 8. 堆
|
||||
|
||||
<div class="center-table" markdown>
|
||||
|
||||
![堆](../assets/covers/chapter_heap.jpg){ width="70%" }
|
||||
|
||||
</div>
|
11
chapter_introduction/index.md
Normal file
11
chapter_introduction/index.md
Normal file
|
@ -0,0 +1,11 @@
|
|||
---
|
||||
comments: true
|
||||
---
|
||||
|
||||
# 1. 引言
|
||||
|
||||
<div class="center-table" markdown>
|
||||
|
||||
![引言](../assets/covers/chapter_introduction.jpg){ width="70%" }
|
||||
|
||||
</div>
|
11
chapter_preface/index.md
Normal file
11
chapter_preface/index.md
Normal file
|
@ -0,0 +1,11 @@
|
|||
---
|
||||
comments: true
|
||||
---
|
||||
|
||||
# 0. 写在前面
|
||||
|
||||
<div class="center-table" markdown>
|
||||
|
||||
![写在前面](../assets/covers/chapter_preface.jpg){ width="70%" }
|
||||
|
||||
</div>
|
|
@ -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"
|
||||
|
|
11
chapter_searching/index.md
Normal file
11
chapter_searching/index.md
Normal file
|
@ -0,0 +1,11 @@
|
|||
---
|
||||
comments: true
|
||||
---
|
||||
|
||||
# 10. 搜索
|
||||
|
||||
<div class="center-table" markdown>
|
||||
|
||||
![搜索](../assets/covers/chapter_searching.jpg){ width="70%" }
|
||||
|
||||
</div>
|
|
@ -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"
|
||||
|
|
11
chapter_sorting/index.md
Normal file
11
chapter_sorting/index.md
Normal file
|
@ -0,0 +1,11 @@
|
|||
---
|
||||
comments: true
|
||||
---
|
||||
|
||||
# 11. 排序
|
||||
|
||||
<div class="center-table" markdown>
|
||||
|
||||
![排序](../assets/covers/chapter_sorting.jpg){ width="70%" }
|
||||
|
||||
</div>
|
|
@ -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"
|
||||
|
|
11
chapter_stack_and_queue/index.md
Normal file
11
chapter_stack_and_queue/index.md
Normal file
|
@ -0,0 +1,11 @@
|
|||
---
|
||||
comments: true
|
||||
---
|
||||
|
||||
# 5. 栈与队列
|
||||
|
||||
<div class="center-table" markdown>
|
||||
|
||||
![栈与队列](../assets/covers/chapter_stack_and_queue.jpg){ width="70%" }
|
||||
|
||||
</div>
|
11
chapter_tree/index.md
Normal file
11
chapter_tree/index.md
Normal file
|
@ -0,0 +1,11 @@
|
|||
---
|
||||
comments: true
|
||||
---
|
||||
|
||||
# 7. 树
|
||||
|
||||
<div class="center-table" markdown>
|
||||
|
||||
![树](../assets/covers/chapter_tree.jpg){ width="70%" }
|
||||
|
||||
</div>
|
Loading…
Reference in a new issue