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/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 new file mode 100644 index 00000000..eb21143f --- /dev/null +++ b/codes/go/chapter_array_and_linkedlist/array.go @@ -0,0 +1,80 @@ +// File: array.go +// Created Time: 2022-12-29 +// Author: GuoWei (gongguowei01@gmail.com) + +package chapter_array_and_linkedlist + +import ( + "fmt" + "math/rand" +) + +/** +我们将 Go 中的 Slice 切片看作 Array 数组,降低理解成本, +有利于我们将关注点放在数据结构与算法上。 +*/ + +/* 随机返回一个数组元素 */ +func randomAccess(nums []int) (randomNum int) { + // 在区间 [0, nums.length) 中随机抽取一个数字 + randomIndex := rand.Intn(len(nums)) + // 获取并返回随机元素 + randomNum = nums[randomIndex] + return +} + +/* 扩展数组长度 */ +func extend(nums []int, enlarge int) []int { + // 初始化一个扩展长度后的数组 + res := make([]int, len(nums)+enlarge) + // 将原数组中的所有元素复制到新数组 + for i := 0; i < len(nums); i++ { + res[i] = nums[i] + } + // 返回扩展后的新数组 + return res +} + +/* 在数组的索引 index 处插入元素 num */ +func insert(nums []int, num int, index int) { + // 把索引 index 以及之后的所有元素向后移动一位 + // 如果超出了数组长度,会被直接舍弃 + for i := len(nums) - 1; i > index; i-- { + nums[i] = nums[i-1] + } + // 将 num 赋给 index 处元素 + nums[index] = num +} + +/* 删除索引 index 处元素 */ +func remove(nums []int, index int) { + // 把索引 index 之后的所有元素向前移动一位 + for i := index; i < len(nums) - 1; i++ { + nums[i] = nums[i+1] + } +} + +/* 遍历数组 */ +func traverse(nums []int) { + var count int + // 通过索引遍历数组 + for i := 0; i < len(nums); i++ { + count++ + } + // 直接遍历数组 + for index, val := range nums { + fmt.Printf("index:%v value:%v\n", index, val) + } +} + +/* 在数组中查找指定元素 */ +func find(nums []int, target int) (index int) { + index = -1 + for i := 0; i < len(nums); i++ { + if nums[i] == target { + index = i + break + } + } + return +} diff --git a/codes/go/chapter_array_and_linkedlist/array_test.go b/codes/go/chapter_array_and_linkedlist/array_test.go new file mode 100644 index 00000000..384cc34d --- /dev/null +++ b/codes/go/chapter_array_and_linkedlist/array_test.go @@ -0,0 +1,47 @@ +// File: array_test.go +// Created Time: 2022-12-29 +// Author: GuoWei (gongguowei01@gmail.com) + +package chapter_array_and_linkedlist + +/** +我们将 Go 中的 Slice 切片看作 Array 数组。因为这样可以 +降低理解成本,利于我们将关注点放在数据结构与算法上。 +*/ + +import ( + "fmt" + "testing" +) + +/* Driver Code */ +func TestArray(t *testing.T) { + /* 初始化数组 */ + var arr []int + fmt.Println("数组 arr = ", arr) + nums := []int{1, 3, 2, 5, 4} + fmt.Println("数组 nums = ", nums) + + /* 随机访问 */ + randomNum := randomAccess(nums) + fmt.Println("在 nums 中获取随机元素 ", randomNum) + + /* 长度扩展 */ + nums = extend(nums, 3) + fmt.Println("将数组长度扩展至 8 ,得到 nums = ", nums) + + /* 插入元素 */ + insert(nums, 6, 3) + fmt.Println("在索引 3 处插入数字 6 ,得到 nums = ", nums) + + /* 删除元素 */ + remove(nums, 2) + fmt.Println("删除索引 2 处的元素,得到 nums = ", nums) + + /* 遍历数组 */ + traverse(nums) + + /* 查找元素 */ + index := find(nums, 3) + fmt.Println("在 nums 中查找元素 3 ,得到索引 = ", index) +} diff --git a/docs/chapter_array_and_linkedlist/array.md b/docs/chapter_array_and_linkedlist/array.md index 3228e678..09596056 100644 --- a/docs/chapter_array_and_linkedlist/array.md +++ b/docs/chapter_array_and_linkedlist/array.md @@ -43,7 +43,9 @@ comments: true === "Go" ```go title="array.go" - + /* 初始化数组 */ + var arr []int + nums := []int{1, 3, 2, 5, 4} ``` === "JavaScript" @@ -133,7 +135,14 @@ elementAddr = firtstElementAddr + elementLength * elementIndex === "Go" ```go title="array.go" - + /* 随机返回一个数组元素 */ + func randomAccess(nums [5]int) (randomNum int) { + // 在区间 [0, nums.length) 中随机抽取一个数字 + randomIndex := rand.Intn(len(nums)) + // 获取并返回随机元素 + randomNum = nums[randomIndex] + return + } ``` === "JavaScript" @@ -236,7 +245,17 @@ elementAddr = firtstElementAddr + elementLength * elementIndex === "Go" ```go title="array.go" - + /* 扩展数组长度 */ + func extend(nums []int, enlarge int) []int { + // 初始化一个扩展长度后的数组 + res := make([]int, len(nums)+enlarge) + // 将原数组中的所有元素复制到新数组 + for i := 0; i < len(nums); i++ { + res[i] = nums[i] + } + // 返回扩展后的新数组 + return res + } ``` === "JavaScript" @@ -370,7 +389,24 @@ elementAddr = firtstElementAddr + elementLength * elementIndex === "Go" ```go title="array.go" - + /* 在数组的索引 index 处插入元素 num */ + func insert(nums []int, num int, index int) { + // 把索引 index 以及之后的所有元素向后移动一位 + // 如果超出了数组长度,会被直接舍弃 + for i := len(nums) - 1; i > index; i-- { + nums[i] = nums[i-1] + } + // 将 num 赋给 index 处元素 + nums[index] = num + } + + /* 删除索引 index 处元素 */ + func remove(nums []int, index int) { + // 把索引 index 之后的所有元素向前移动一位 + for i := index; i < len(nums) - 1; i++ { + nums[i] = nums[i+1] + } + } ``` === "JavaScript" @@ -499,7 +535,18 @@ elementAddr = firtstElementAddr + elementLength * elementIndex === "Go" ```go title="array.go" - + /* 遍历数组 */ + func traverse(nums []int) { + var count int + // 通过索引遍历数组 + for i := 0; i < len(nums); i++ { + count++ + } + // 直接遍历数组 + for index, val := range nums { + fmt.Printf("index:%v value:%v\n", index, val) + } + } ``` === "JavaScript" @@ -604,7 +651,17 @@ elementAddr = firtstElementAddr + elementLength * elementIndex === "Go" ```go title="array.go" - + /* 在数组中查找指定元素 */ + func find(nums []int, target int) (index int) { + index = -1 + for i := 0; i < len(nums); i++ { + if nums[i] == target { + index = i + break + } + } + return + } ``` === "JavaScript" 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_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