diff --git a/chapter_array_and_linkedlist/array.md b/chapter_array_and_linkedlist/array.md index c8cb05a89..2e98b8013 100755 --- a/chapter_array_and_linkedlist/array.md +++ b/chapter_array_and_linkedlist/array.md @@ -70,7 +70,8 @@ comments: true === "C" ```c title="array.c" - + int arr[5] = { 0 }; // { 0, 0, 0, 0, 0 } + int nums[5] = { 1, 3, 2, 5, 4 }; ``` === "C#" @@ -195,7 +196,7 @@ elementAddr = firtstElementAddr + elementLength * elementIndex === "C" ```c title="array.c" - + [class]{}-[func]{randomAccess} ``` === "C#" @@ -348,7 +349,7 @@ elementAddr = firtstElementAddr + elementLength * elementIndex === "C" ```c title="array.c" - + [class]{}-[func]{extend} ``` === "C#" @@ -541,7 +542,9 @@ elementAddr = firtstElementAddr + elementLength * elementIndex === "C" ```c title="array.c" - + [class]{}-[func]{insert} + + [class]{}-[func]{removeItem} ``` === "C#" @@ -720,7 +723,7 @@ elementAddr = firtstElementAddr + elementLength * elementIndex === "C" ```c title="array.c" - + [class]{}-[func]{traverse} ``` === "C#" @@ -863,7 +866,7 @@ elementAddr = firtstElementAddr + elementLength * elementIndex === "C" ```c title="array.c" - + [class]{}-[func]{find} ``` === "C#" diff --git a/chapter_array_and_linkedlist/linked_list.md b/chapter_array_and_linkedlist/linked_list.md index ca3d6110b..6a7bd9ec9 100755 --- a/chapter_array_and_linkedlist/linked_list.md +++ b/chapter_array_and_linkedlist/linked_list.md @@ -453,7 +453,9 @@ comments: true === "C" ```c title="linked_list.c" + [class]{}-[func]{insertNode} + [class]{}-[func]{removeNode} ``` === "C#" @@ -614,7 +616,7 @@ comments: true === "C" ```c title="linked_list.c" - + [class]{}-[func]{access} ``` === "C#" @@ -770,7 +772,7 @@ comments: true === "C" ```c title="linked_list.c" - + [class]{}-[func]{findNode} ``` === "C#" diff --git a/chapter_array_and_linkedlist/list.md b/chapter_array_and_linkedlist/list.md index f8c2ef35f..421436e82 100755 --- a/chapter_array_and_linkedlist/list.md +++ b/chapter_array_and_linkedlist/list.md @@ -1323,7 +1323,7 @@ comments: true === "C" ```c title="my_list.c" - + [class]{myList}-[func]{} ``` === "C#" diff --git a/chapter_computational_complexity/space_complexity.md b/chapter_computational_complexity/space_complexity.md index 1a9935146..1c31fffd7 100755 --- a/chapter_computational_complexity/space_complexity.md +++ b/chapter_computational_complexity/space_complexity.md @@ -706,7 +706,7 @@ $$ === "C" ```c title="space_complexity.c" - + [class]{}-[func]{spaceConstant} ``` === "C#" @@ -902,7 +902,7 @@ $$ === "C" ```c title="space_complexity.c" - + [class]{}-[func]{spaceLinear} ``` === "C#" @@ -1041,7 +1041,7 @@ $$ === "C" ```c title="space_complexity.c" - + [class]{}-[func]{spaceLinearRecur} ``` === "C#" @@ -1187,7 +1187,7 @@ $$ === "C" ```c title="space_complexity.c" - + [class]{}-[func]{spaceQuadratic} ``` === "C#" @@ -1322,7 +1322,7 @@ $$ === "C" ```c title="space_complexity.c" - + [class]{}-[func]{spaceQuadraticRecur} ``` === "C#" @@ -1456,7 +1456,7 @@ $$ === "C" ```c title="space_complexity.c" - + [class]{}-[func]{buildTree} ``` === "C#" diff --git a/chapter_computational_complexity/space_time_tradeoff.md b/chapter_computational_complexity/space_time_tradeoff.md index 765826e03..ea4e7c79c 100755 --- a/chapter_computational_complexity/space_time_tradeoff.md +++ b/chapter_computational_complexity/space_time_tradeoff.md @@ -134,7 +134,7 @@ comments: true === "C" ```c title="leetcode_two_sum.c" - + [class]{}-[func]{twoSumBruteForce} ``` === "C#" @@ -313,7 +313,7 @@ comments: true === "C" ```c title="leetcode_two_sum.c" - + [class]{}-[func]{twoSumHashTable} ``` === "C#" diff --git a/chapter_computational_complexity/time_complexity.md b/chapter_computational_complexity/time_complexity.md index 44ad40fbd..aeacab92f 100755 --- a/chapter_computational_complexity/time_complexity.md +++ b/chapter_computational_complexity/time_complexity.md @@ -871,16 +871,7 @@ $$ === "C" ```c title="time_complexity.c" - /* 常数阶 */ - int constant(int n) { - int count = 0; - int size = 100000; - int i = 0; - for (int i = 0; i < size; i++) { - count ++; - } - return count; - } + [class]{}-[func]{constant} ``` === "C#" @@ -1004,14 +995,7 @@ $$ === "C" ```c title="time_complexity.c" - /* 线性阶 */ - int linear(int n) { - int count = 0; - for (int i = 0; i < n; i++) { - count ++; - } - return count; - } + [class]{}-[func]{linear} ``` === "C#" @@ -1145,15 +1129,7 @@ $$ === "C" ```c title="time_complexity.c" - /* 线性阶(遍历数组) */ - int arrayTraversal(int *nums, int n) { - int count = 0; - // 循环次数与数组长度成正比 - for (int i = 0; i < n; i++) { - count ++; - } - return count; - } + [class]{}-[func]{arrayTraversal} ``` === "C#" @@ -1300,17 +1276,7 @@ $$ === "C" ```c title="time_complexity.c" - /* 平方阶 */ - int quadratic(int n) { - int count = 0; - // 循环次数与数组长度成平方关系 - for (int i = 0; i < n; i++) { - for (int j = 0; j < n; j++) { - count ++; - } - } - return count; - } + [class]{}-[func]{quadratic} ``` === "C#" @@ -1513,26 +1479,7 @@ $$ === "C" ```c title="time_complexity.c" - /* 平方阶(冒泡排序) */ - int bubbleSort(int *nums, int n) { - int count = 0; // 计数器 - // 外循环:待排序元素数量为 n-1, n-2, ..., 1 - for (int i = n - 1; i > 0; i--) { - // 内循环:冒泡操作 - for (int j = 0; j < i; j++) { - if (nums[j] > nums [j + 1]) - { - // 交换 nums[j] 与 nums[j + 1] - int tmp = nums[j]; - nums[j] = nums[j + 1]; - nums[j + 1] = tmp; - count += 3; // 元素交换包含 3 个单元操作 - } - } - - } - return count; - } + [class]{}-[func]{bubbleSort} ``` === "C#" @@ -1728,20 +1675,7 @@ $$ === "C" ```c title="time_complexity.c" - /* 指数阶(循环实现) */ - int exponential(int n) { - int count = 0; - int bas = 1; - // cell 每轮一分为二,形成数列 1, 2, 4, 8, ..., 2^(n-1) - for (int i = 0; i < n; i++) { - for (int j = 0; j < bas; j++) { - count++; - } - bas *= 2; - } - // count = 1 + 2 + 4 + 8 + .. + 2^(n-1) = 2^n - 1 - return count; - } + [class]{}-[func]{exponential} ``` === "C#" @@ -1875,11 +1809,7 @@ $$ === "C" ```c title="time_complexity.c" - /* 指数阶(递归实现) */ - int expRecur(int n) { - if (n == 1) return 1; - return expRecur(n - 1) + expRecur(n - 1) + 1; - } + [class]{}-[func]{expRecur} ``` === "C#" @@ -2008,15 +1938,7 @@ $$ === "C" ```c title="time_complexity.c" - /* 对数阶(循环实现) */ - int logarithmic(float n) { - int count = 0; - while (n > 1) { - n = n / 2; - count++; - } - return count; - } + [class]{}-[func]{logarithmic} ``` === "C#" @@ -2136,11 +2058,7 @@ $$ === "C" ```c title="time_complexity.c" - /* 对数阶(递归实现) */ - int logRecur(float n) { - if (n <= 1) return 0; - return logRecur(n / 2) + 1; - } + [class]{}-[func]{logRecur} ``` === "C#" @@ -2273,16 +2191,7 @@ $$ === "C" ```c title="time_complexity.c" - /* 线性对数阶 */ - int linearLogRecur(float n) { - if (n <= 1) return 1; - int count = linearLogRecur(n / 2) + - linearLogRecur(n / 2); - for (int i = 0; i < n; i++) { - count ++; - } - return count; - } + [class]{}-[func]{linearLogRecur} ``` === "C#" @@ -2441,15 +2350,7 @@ $$ === "C" ```c title="time_complexity.c" - /* 阶乘阶(递归实现) */ - int factorialRecur(int n) { - if (n == 0) return 1; - int count = 0; - for (int i = 0; i < n; i++) { - count += factorialRecur(n - 1); - } - return count; - } + [class]{}-[func]{factorialRecur} ``` === "C#" diff --git a/chapter_graph/graph_operations.md b/chapter_graph/graph_operations.md index 94a0a155d..268ebcb75 100644 --- a/chapter_graph/graph_operations.md +++ b/chapter_graph/graph_operations.md @@ -125,7 +125,89 @@ comments: true === "C++" ```cpp title="graph_adjacency_matrix.cpp" - [class]{GraphAdjMat}-[func]{} + /* 基于邻接矩阵实现的无向图类 */ + class GraphAdjMat { + vector vertices; // 顶点列表,元素代表“顶点值”,索引代表“顶点索引” + vector> adjMat; // 邻接矩阵,行列索引对应“顶点索引” + + public: + /* 构造方法 */ + GraphAdjMat(const vector& vertices, const vector>& edges) { + // 添加顶点 + for (int val : vertices) { + addVertex(val); + } + // 添加边 + // 请注意,edges 元素代表顶点索引,即对应 vertices 元素索引 + for (const vector& edge : edges) { + addEdge(edge[0], edge[1]); + } + } + + /* 获取顶点数量 */ + int size() const { + return vertices.size(); + } + + /* 添加顶点 */ + void addVertex(int val) { + int n = size(); + // 向顶点列表中添加新顶点的值 + vertices.push_back(val); + // 在邻接矩阵中添加一行 + adjMat.emplace_back(n, 0); + // 在邻接矩阵中添加一列 + for (vector& row : adjMat) { + row.push_back(0); + } + } + + /* 删除顶点 */ + void removeVertex(int index) { + if (index >= size()) { + throw out_of_range("顶点不存在"); + } + // 在顶点列表中移除索引 index 的顶点 + vertices.erase(vertices.begin() + index); + // 在邻接矩阵中删除索引 index 的行 + adjMat.erase(adjMat.begin() + index); + // 在邻接矩阵中删除索引 index 的列 + for (vector& row : adjMat) { + row.erase(row.begin() + index); + } + } + + /* 添加边 */ + // 参数 i, j 对应 vertices 元素索引 + void addEdge(int i, int j) { + // 索引越界与相等处理 + if (i < 0 || j < 0 || i >= size() || j >= size() || i == j) { + throw out_of_range("顶点不存在"); + } + // 在无向图中,邻接矩阵沿主对角线对称,即满足 (i, j) == (j, i) + adjMat[i][j] = 1; + adjMat[j][i] = 1; + } + + /* 删除边 */ + // 参数 i, j 对应 vertices 元素索引 + void removeEdge(int i, int j) { + // 索引越界与相等处理 + if (i < 0 || j < 0 || i >= size() || j >= size() || i == j) { + throw out_of_range("顶点不存在"); + } + adjMat[i][j] = 0; + adjMat[j][i] = 0; + } + + /* 打印邻接矩阵 */ + void print() { + cout << "顶点列表 = "; + PrintUtil::printVector(vertices); + cout << "邻接矩阵 =" << endl; + PrintUtil::printVectorMatrix(adjMat); + } + }; ``` === "Python" @@ -236,13 +318,185 @@ comments: true === "JavaScript" ```javascript title="graph_adjacency_matrix.js" - [class]{GraphAdjMat}-[func]{} + /* 基于邻接矩阵实现的无向图类 */ + class GraphAdjMat { + vertices; // 顶点列表,元素代表“顶点值”,索引代表“顶点索引” + adjMat; // 邻接矩阵,行列索引对应“顶点索引” + + /* 构造函数 */ + constructor(vertices, edges) { + this.vertices = []; + this.adjMat = []; + // 添加顶点 + for (const val of vertices) { + this.addVertex(val); + } + // 添加边 + // 请注意,edges 元素代表顶点索引,即对应 vertices 元素索引 + for (const e of edges) { + this.addEdge(e[0], e[1]); + } + } + + /* 获取顶点数量 */ + size() { + return this.vertices.length; + } + + /* 添加顶点 */ + addVertex(val) { + const n = this.size(); + // 向顶点列表中添加新顶点的值 + this.vertices.push(val); + // 在邻接矩阵中添加一行 + const newRow = []; + for (let j = 0; j < n; j++) { + newRow.push(0); + } + this.adjMat.push(newRow); + // 在邻接矩阵中添加一列 + for (const row of this.adjMat) { + row.push(0); + } + } + + /* 删除顶点 */ + removeVertex(index) { + if (index >= this.size()) { + throw new RangeError("Index Out Of Bounds Exception"); + } + // 在顶点列表中移除索引 index 的顶点 + this.vertices.splice(index, 1); + + // 在邻接矩阵中删除索引 index 的行 + this.adjMat.splice(index, 1); + // 在邻接矩阵中删除索引 index 的列 + for (const row of this.adjMat) { + row.splice(index, 1); + } + } + + /* 添加边 */ + // 参数 i, j 对应 vertices 元素索引 + addEdge(i, j) { + // 索引越界与相等处理 + if (i < 0 || j < 0 || i >= this.size() || j >= this.size() || i === j) { + throw new RangeError("Index Out Of Bounds Exception"); + } + // 在无向图中,邻接矩阵沿主对角线对称,即满足 (i, j) == (j, i) + this.adjMat[i][j] = 1; + this.adjMat[j][i] = 1; + } + + /* 删除边 */ + // 参数 i, j 对应 vertices 元素索引 + removeEdge(i, j) { + // 索引越界与相等处理 + if (i < 0 || j < 0 || i >= this.size() || j >= this.size() || i === j) { + throw new RangeError("Index Out Of Bounds Exception"); + } + this.adjMat[i][j] = 0; + this.adjMat[j][i] = 0; + } + + /* 打印邻接矩阵 */ + print() { + console.log("顶点列表 = ", this.vertices); + console.log("邻接矩阵 =", this.adjMat); + } + } ``` === "TypeScript" ```typescript title="graph_adjacency_matrix.ts" - [class]{GraphAdjMat}-[func]{} + /* 基于邻接矩阵实现的无向图类 */ + class GraphAdjMat { + vertices: number[]; // 顶点列表,元素代表“顶点值”,索引代表“顶点索引” + adjMat: number[][]; // 邻接矩阵,行列索引对应“顶点索引” + + /* 构造函数 */ + constructor(vertices: number[], edges: number[][]) { + this.vertices = []; + this.adjMat = []; + // 添加顶点 + for (const val of vertices) { + this.addVertex(val); + } + // 添加边 + // 请注意,edges 元素代表顶点索引,即对应 vertices 元素索引 + for (const e of edges) { + this.addEdge(e[0], e[1]); + } + } + + /* 获取顶点数量 */ + size(): number { + return this.vertices.length; + } + + /* 添加顶点 */ + addVertex(val: number): void { + const n: number = this.size(); + // 向顶点列表中添加新顶点的值 + this.vertices.push(val); + // 在邻接矩阵中添加一行 + const newRow: number[] = []; + for (let j: number = 0; j < n; j++) { + newRow.push(0); + } + this.adjMat.push(newRow); + // 在邻接矩阵中添加一列 + for (const row of this.adjMat) { + row.push(0); + } + } + + /* 删除顶点 */ + removeVertex(index: number): void { + if (index >= this.size()) { + throw new RangeError("Index Out Of Bounds Exception"); + } + // 在顶点列表中移除索引 index 的顶点 + this.vertices.splice(index, 1); + + // 在邻接矩阵中删除索引 index 的行 + this.adjMat.splice(index, 1); + // 在邻接矩阵中删除索引 index 的列 + for (const row of this.adjMat) { + row.splice(index, 1); + } + } + + /* 添加边 */ + // 参数 i, j 对应 vertices 元素索引 + addEdge(i: number, j: number): void { + // 索引越界与相等处理 + if (i < 0 || j < 0 || i >= this.size() || j >= this.size() || i === j) { + throw new RangeError("Index Out Of Bounds Exception"); + } + // 在无向图中,邻接矩阵沿主对角线对称,即满足 (i, j) == (j, i) + this.adjMat[i][j] = 1; + this.adjMat[j][i] = 1; + } + + /* 删除边 */ + // 参数 i, j 对应 vertices 元素索引 + removeEdge(i: number, j: number): void { + // 索引越界与相等处理 + if (i < 0 || j < 0 || i >= this.size() || j >= this.size() || i === j) { + throw new RangeError("Index Out Of Bounds Exception"); + } + this.adjMat[i][j] = 0; + this.adjMat[j][i] = 0; + } + + /* 打印邻接矩阵 */ + print(): void { + console.log("顶点列表 = ", this.vertices); + console.log("邻接矩阵 =", this.adjMat); + } + } ``` === "C" @@ -466,9 +720,80 @@ comments: true === "C++" ```cpp title="graph_adjacency_list.cpp" - [class]{Vertex}-[func]{} + /* 顶点类 */ + struct Vertex { + int val; + Vertex(int val) : val(val) {} + }; - [class]{GraphAdjList}-[func]{} + /* 基于邻接表实现的无向图类 */ + class GraphAdjList { + // 请注意,vertices 和 adjList 中存储的都是 Vertex 对象 + unordered_map> adjList; // 邻接表(使用哈希表实现) + + public: + /* 构造方法 */ + GraphAdjList(const vector>& edges) { + // 添加所有顶点和边 + for (const vector& edge : edges) { + addVertex(edge[0]); + addVertex(edge[1]); + addEdge(edge[0], edge[1]); + } + } + + /* 获取顶点数量 */ + int size() { return adjList.size(); } + + /* 添加边 */ + void addEdge(Vertex* vet1, Vertex* vet2) { + if (!adjList.count(vet1) || !adjList.count(vet2) || vet1 == vet2) + throw invalid_argument("不存在顶点"); + // 添加边 vet1 - vet2 + adjList[vet1].insert(vet2); + adjList[vet2].insert(vet1); + } + + /* 删除边 */ + void removeEdge(Vertex* vet1, Vertex* vet2) { + if (!adjList.count(vet1) || !adjList.count(vet2) || vet1 == vet2) + throw invalid_argument("不存在顶点"); + // 删除边 vet1 - vet2 + adjList[vet1].erase(vet2); + adjList[vet2].erase(vet1); + } + + /* 添加顶点 */ + void addVertex(Vertex* vet) { + if (adjList.count(vet)) return; + // 在邻接表中添加一个新链表(即 HashSet) + adjList[vet] = unordered_set(); + } + + /* 删除顶点 */ + void removeVertex(Vertex* vet) { + if (!adjList.count(vet)) + throw invalid_argument("不存在顶点"); + // 在邻接表中删除顶点 vet 对应的链表(即 HashSet) + adjList.erase(vet); + // 遍历其它顶点的链表(即 HashSet),删除所有包含 vet 的边 + for (auto& [key, set_] : adjList) { + set_.erase(vet); + } + } + + /* 打印邻接表 */ + void print() { + cout << "邻接表 =" << endl; + for (auto& [key, value] : adjList) { + vector tmp; + for (Vertex* vertex : value) + tmp.push_back(vertex->val); + cout << key->val << ": "; + PrintUtil::printVector(tmp); + } + } + }; ``` === "Python" diff --git a/chapter_hashing/hash_map.md b/chapter_hashing/hash_map.md index 66a9dd4a5..e64db248c 100755 --- a/chapter_hashing/hash_map.md +++ b/chapter_hashing/hash_map.md @@ -950,7 +950,9 @@ $$ === "C" ```c title="array_hash_map.c" - + [class]{entry}-[func]{} + + [class]{arrayHashMap}-[func]{} ``` === "C#" diff --git a/chapter_heap/heap.md b/chapter_heap/heap.md index d8bbbe83a..1ebd6214f 100644 --- a/chapter_heap/heap.md +++ b/chapter_heap/heap.md @@ -362,7 +362,11 @@ comments: true === "C" ```c title="my_heap.c" + [class]{maxHeap}-[func]{left} + [class]{maxHeap}-[func]{right} + + [class]{maxHeap}-[func]{parent} ``` === "C#" @@ -472,7 +476,7 @@ comments: true === "C" ```c title="my_heap.c" - + [class]{maxHeap}-[func]{peek} ``` === "C#" @@ -668,7 +672,9 @@ comments: true === "C" ```c title="my_heap.c" + [class]{maxHeap}-[func]{push} + [class]{maxHeap}-[func]{siftUp} ``` === "C#" @@ -821,8 +827,7 @@ comments: true void poll() { // 判空处理 if (empty()) { - cout << "Error:堆为空" << endl; - return; + throw out_of_range("堆为空"); } // 交换根结点与最右叶结点(即交换首元素与尾元素) swap(maxHeap[0], maxHeap[size() - 1]); @@ -977,7 +982,9 @@ comments: true === "C" ```c title="my_heap.c" + [class]{maxHeap}-[func]{poll} + [class]{maxHeap}-[func]{siftDown} ``` === "C#" @@ -1156,7 +1163,7 @@ comments: true === "C" ```c title="my_heap.c" - + [class]{maxHeap}-[func]{newMaxHeap} ``` === "C#" diff --git a/chapter_searching/binary_search.md b/chapter_searching/binary_search.md index 36bcabfaa..337506cb2 100755 --- a/chapter_searching/binary_search.md +++ b/chapter_searching/binary_search.md @@ -183,7 +183,7 @@ $$ === "C" ```c title="binary_search.c" - + [class]{}-[func]{binarySearch} ``` === "C#" @@ -395,7 +395,7 @@ $$ === "C" ```c title="binary_search.c" - + [class]{}-[func]{binarySearch1} ``` === "C#" diff --git a/chapter_searching/hashing_search.md b/chapter_searching/hashing_search.md index 0cc61b3a4..ba5ce772f 100755 --- a/chapter_searching/hashing_search.md +++ b/chapter_searching/hashing_search.md @@ -90,7 +90,7 @@ comments: true === "C" ```c title="hashing_search.c" - + [class]{}-[func]{hashingSearchArray} ``` === "C#" @@ -206,7 +206,7 @@ comments: true === "C" ```c title="hashing_search.c" - + [class]{}-[func]{hashingSearchLinkedList} ``` === "C#" diff --git a/chapter_searching/linear_search.md b/chapter_searching/linear_search.md index 2b653a040..bc9ff90c6 100755 --- a/chapter_searching/linear_search.md +++ b/chapter_searching/linear_search.md @@ -110,7 +110,7 @@ comments: true === "C" ```c title="linear_search.c" - + [class]{}-[func]{linearSearchArray} ``` === "C#" @@ -271,7 +271,7 @@ comments: true === "C" ```c title="linear_search.c" - + [class]{}-[func]{linearSearchLinkedList} ``` === "C#" diff --git a/chapter_sorting/bubble_sort.md b/chapter_sorting/bubble_sort.md index ff2f2ce5f..3da3abd44 100755 --- a/chapter_sorting/bubble_sort.md +++ b/chapter_sorting/bubble_sort.md @@ -162,23 +162,7 @@ comments: true === "C" ```c title="bubble_sort.c" - /* 冒泡排序 */ - void bubbleSort(int nums[], int size) { - // 外循环:待排序元素数量为 n-1, n-2, ..., 1 - for (int i = 0; i < size - 1; i++) - { - // 内循环:冒泡操作 - for (int j = 0; j < size - 1 - i; j++) - { - if (nums[j] > nums[j + 1]) - { - int temp = nums[j]; - nums[j] = nums[j + 1]; - nums[j + 1] = temp; - } - } - } - } + [class]{}-[func]{bubbleSort} ``` === "C#" @@ -401,26 +385,7 @@ comments: true === "C" ```c title="bubble_sort.c" - /* 冒泡排序 */ - void bubbleSortWithFlag(int nums[], int size) { - // 外循环:待排序元素数量为 n-1, n-2, ..., 1 - for (int i = 0; i < size - 1; i++) - { - bool flag = false; - // 内循环:冒泡操作 - for (int j = 0; j < size - 1 - i; j++) - { - if (nums[j] > nums[j + 1]) - { - int temp = nums[j]; - nums[j] = nums[j + 1]; - nums[j + 1] = temp; - flag = true; - } - } - if(!flag) break; - } - } + [class]{}-[func]{bubbleSortWithFlag} ``` === "C#" diff --git a/chapter_sorting/insertion_sort.md b/chapter_sorting/insertion_sort.md index 1eacaca60..21e8f8279 100755 --- a/chapter_sorting/insertion_sort.md +++ b/chapter_sorting/insertion_sort.md @@ -135,23 +135,7 @@ 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; - } - } + [class]{}-[func]{insertionSort} ``` === "C#" diff --git a/chapter_sorting/quick_sort.md b/chapter_sorting/quick_sort.md index 376d12dfb..8b56ffb64 100755 --- a/chapter_sorting/quick_sort.md +++ b/chapter_sorting/quick_sort.md @@ -198,7 +198,7 @@ comments: true === "C" ```c title="quick_sort.c" - + [class]{quickSort}-[func]{partition} ``` === "C#" @@ -399,7 +399,7 @@ comments: true === "C" ```c title="quick_sort.c" - + [class]{quickSort}-[func]{quickSort} ``` === "C#" @@ -694,7 +694,9 @@ comments: true === "C" ```c title="quick_sort.c" + [class]{quickSortMedian}-[func]{medianThree} + [class]{quickSortMedian}-[func]{partition} ``` === "C#" @@ -926,7 +928,7 @@ comments: true === "C" ```c title="quick_sort.c" - + [class]{quickSortTailCall}-[func]{quickSort} ``` === "C#" diff --git a/chapter_stack_and_queue/queue.md b/chapter_stack_and_queue/queue.md index 5432410d9..c3cfc5fa3 100755 --- a/chapter_stack_and_queue/queue.md +++ b/chapter_stack_and_queue/queue.md @@ -684,7 +684,7 @@ comments: true === "C" ```c title="linkedlist_queue.c" - + [class]{linkedListQueue}-[func]{} ``` === "C#" @@ -1367,7 +1367,7 @@ comments: true === "C" ```c title="array_queue.c" - + [class]{arrayQueue}-[func]{} ``` === "C#" diff --git a/chapter_stack_and_queue/stack.md b/chapter_stack_and_queue/stack.md index e80725298..77ea6c8cf 100755 --- a/chapter_stack_and_queue/stack.md +++ b/chapter_stack_and_queue/stack.md @@ -634,7 +634,7 @@ comments: true === "C" ```c title="linkedlist_stack.c" - + [class]{linkedListStack}-[func]{} ``` === "C#" @@ -1136,7 +1136,7 @@ comments: true === "C" ```c title="array_stack.c" - + [class]{arrayStack}-[func]{} ``` === "C#" diff --git a/chapter_tree/avl_tree.md b/chapter_tree/avl_tree.md index 59398dc73..e1b7b9108 100644 --- a/chapter_tree/avl_tree.md +++ b/chapter_tree/avl_tree.md @@ -263,7 +263,9 @@ G. M. Adelson-Velsky 和 E. M. Landis 在其 1962 年发表的论文 "An algorit === "C" ```c title="avl_tree.c" + [class]{aVLTree}-[func]{height} + [class]{aVLTree}-[func]{updateHeight} ``` === "C#" @@ -398,9 +400,7 @@ G. M. Adelson-Velsky 和 E. M. Landis 在其 1962 年发表的论文 "An algorit === "C" ```c title="avl_tree.c" - - - + [class]{aVLTree}-[func]{balanceFactor} ``` === "C#" @@ -582,7 +582,7 @@ AVL 树的独特之处在于「旋转 Rotation」的操作,其可 **在不影 === "C" ```c title="avl_tree.c" - + [class]{aVLTree}-[func]{rightRotate} ``` === "C#" @@ -762,9 +762,7 @@ AVL 树的独特之处在于「旋转 Rotation」的操作,其可 **在不影 === "C" ```c title="avl_tree.c" - - - + [class]{aVLTree}-[func]{leftRotate} ``` === "C#" @@ -1058,7 +1056,7 @@ AVL 树的独特之处在于「旋转 Rotation」的操作,其可 **在不影 === "C" ```c title="avl_tree.c" - + [class]{aVLTree}-[func]{rotate} ``` === "C#" @@ -1345,9 +1343,9 @@ AVL 树的独特之处在于「旋转 Rotation」的操作,其可 **在不影 === "C" ```c title="avl_tree.c" + [class]{aVLTree}-[func]{insert} - - + [class]{aVLTree}-[func]{insertHelper} ``` === "C#" @@ -1761,7 +1759,11 @@ AVL 树的独特之处在于「旋转 Rotation」的操作,其可 **在不影 === "C" ```c title="avl_tree.c" + [class]{aVLTree}-[func]{remove} + [class]{aVLTree}-[func]{removeHelper} + + [class]{aVLTree}-[func]{getInOrderNext} ``` === "C#" diff --git a/chapter_tree/binary_search_tree.md b/chapter_tree/binary_search_tree.md index cdf825125..8c0144a62 100755 --- a/chapter_tree/binary_search_tree.md +++ b/chapter_tree/binary_search_tree.md @@ -163,7 +163,7 @@ comments: true === "C" ```c title="binary_search_tree.c" - + [class]{binarySearchTree}-[func]{search} ``` === "C#" @@ -432,7 +432,7 @@ comments: true === "C" ```c title="binary_search_tree.c" - + [class]{binarySearchTree}-[func]{insert} ``` === "C#" @@ -927,7 +927,9 @@ comments: true === "C" ```c title="binary_search_tree.c" - + [class]{binarySearchTree}-[func]{remove} + + [class]{binarySearchTree}-[func]{getInOrderNext} ``` === "C#" diff --git a/chapter_tree/binary_tree_traversal.md b/chapter_tree/binary_tree_traversal.md index 8804a5d6d..b850804e9 100755 --- a/chapter_tree/binary_tree_traversal.md +++ b/chapter_tree/binary_tree_traversal.md @@ -158,7 +158,7 @@ comments: true === "C" ```c title="binary_tree_bfs.c" - + [class]{}-[func]{hierOrder} ``` === "C#" @@ -461,7 +461,11 @@ comments: true === "C" ```c title="binary_tree_dfs.c" - + [class]{}-[func]{preOrder} + + [class]{}-[func]{inOrder} + + [class]{}-[func]{postOrder} ``` === "C#"