This commit is contained in:
krahets 2023-02-11 18:21:44 +08:00
parent 76dcc6cbd3
commit 58ad2290b0
20 changed files with 418 additions and 219 deletions

View file

@ -70,7 +70,8 @@ comments: true
=== "C" === "C"
```c title="array.c" ```c title="array.c"
int arr[5] = { 0 }; // { 0, 0, 0, 0, 0 }
int nums[5] = { 1, 3, 2, 5, 4 };
``` ```
=== "C#" === "C#"
@ -195,7 +196,7 @@ elementAddr = firtstElementAddr + elementLength * elementIndex
=== "C" === "C"
```c title="array.c" ```c title="array.c"
[class]{}-[func]{randomAccess}
``` ```
=== "C#" === "C#"
@ -348,7 +349,7 @@ elementAddr = firtstElementAddr + elementLength * elementIndex
=== "C" === "C"
```c title="array.c" ```c title="array.c"
[class]{}-[func]{extend}
``` ```
=== "C#" === "C#"
@ -541,7 +542,9 @@ elementAddr = firtstElementAddr + elementLength * elementIndex
=== "C" === "C"
```c title="array.c" ```c title="array.c"
[class]{}-[func]{insert}
[class]{}-[func]{removeItem}
``` ```
=== "C#" === "C#"
@ -720,7 +723,7 @@ elementAddr = firtstElementAddr + elementLength * elementIndex
=== "C" === "C"
```c title="array.c" ```c title="array.c"
[class]{}-[func]{traverse}
``` ```
=== "C#" === "C#"
@ -863,7 +866,7 @@ elementAddr = firtstElementAddr + elementLength * elementIndex
=== "C" === "C"
```c title="array.c" ```c title="array.c"
[class]{}-[func]{find}
``` ```
=== "C#" === "C#"

View file

@ -453,7 +453,9 @@ comments: true
=== "C" === "C"
```c title="linked_list.c" ```c title="linked_list.c"
[class]{}-[func]{insertNode}
[class]{}-[func]{removeNode}
``` ```
=== "C#" === "C#"
@ -614,7 +616,7 @@ comments: true
=== "C" === "C"
```c title="linked_list.c" ```c title="linked_list.c"
[class]{}-[func]{access}
``` ```
=== "C#" === "C#"
@ -770,7 +772,7 @@ comments: true
=== "C" === "C"
```c title="linked_list.c" ```c title="linked_list.c"
[class]{}-[func]{findNode}
``` ```
=== "C#" === "C#"

View file

@ -1323,7 +1323,7 @@ comments: true
=== "C" === "C"
```c title="my_list.c" ```c title="my_list.c"
[class]{myList}-[func]{}
``` ```
=== "C#" === "C#"

View file

@ -706,7 +706,7 @@ $$
=== "C" === "C"
```c title="space_complexity.c" ```c title="space_complexity.c"
[class]{}-[func]{spaceConstant}
``` ```
=== "C#" === "C#"
@ -902,7 +902,7 @@ $$
=== "C" === "C"
```c title="space_complexity.c" ```c title="space_complexity.c"
[class]{}-[func]{spaceLinear}
``` ```
=== "C#" === "C#"
@ -1041,7 +1041,7 @@ $$
=== "C" === "C"
```c title="space_complexity.c" ```c title="space_complexity.c"
[class]{}-[func]{spaceLinearRecur}
``` ```
=== "C#" === "C#"
@ -1187,7 +1187,7 @@ $$
=== "C" === "C"
```c title="space_complexity.c" ```c title="space_complexity.c"
[class]{}-[func]{spaceQuadratic}
``` ```
=== "C#" === "C#"
@ -1322,7 +1322,7 @@ $$
=== "C" === "C"
```c title="space_complexity.c" ```c title="space_complexity.c"
[class]{}-[func]{spaceQuadraticRecur}
``` ```
=== "C#" === "C#"
@ -1456,7 +1456,7 @@ $$
=== "C" === "C"
```c title="space_complexity.c" ```c title="space_complexity.c"
[class]{}-[func]{buildTree}
``` ```
=== "C#" === "C#"

View file

@ -134,7 +134,7 @@ comments: true
=== "C" === "C"
```c title="leetcode_two_sum.c" ```c title="leetcode_two_sum.c"
[class]{}-[func]{twoSumBruteForce}
``` ```
=== "C#" === "C#"
@ -313,7 +313,7 @@ comments: true
=== "C" === "C"
```c title="leetcode_two_sum.c" ```c title="leetcode_two_sum.c"
[class]{}-[func]{twoSumHashTable}
``` ```
=== "C#" === "C#"

View file

@ -871,16 +871,7 @@ $$
=== "C" === "C"
```c title="time_complexity.c" ```c title="time_complexity.c"
/* 常数阶 */ [class]{}-[func]{constant}
int constant(int n) {
int count = 0;
int size = 100000;
int i = 0;
for (int i = 0; i < size; i++) {
count ++;
}
return count;
}
``` ```
=== "C#" === "C#"
@ -1004,14 +995,7 @@ $$
=== "C" === "C"
```c title="time_complexity.c" ```c title="time_complexity.c"
/* 线性阶 */ [class]{}-[func]{linear}
int linear(int n) {
int count = 0;
for (int i = 0; i < n; i++) {
count ++;
}
return count;
}
``` ```
=== "C#" === "C#"
@ -1145,15 +1129,7 @@ $$
=== "C" === "C"
```c title="time_complexity.c" ```c title="time_complexity.c"
/* 线性阶(遍历数组) */ [class]{}-[func]{arrayTraversal}
int arrayTraversal(int *nums, int n) {
int count = 0;
// 循环次数与数组长度成正比
for (int i = 0; i < n; i++) {
count ++;
}
return count;
}
``` ```
=== "C#" === "C#"
@ -1300,17 +1276,7 @@ $$
=== "C" === "C"
```c title="time_complexity.c" ```c title="time_complexity.c"
/* 平方阶 */ [class]{}-[func]{quadratic}
int quadratic(int n) {
int count = 0;
// 循环次数与数组长度成平方关系
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
count ++;
}
}
return count;
}
``` ```
=== "C#" === "C#"
@ -1513,26 +1479,7 @@ $$
=== "C" === "C"
```c title="time_complexity.c" ```c title="time_complexity.c"
/* 平方阶(冒泡排序) */ [class]{}-[func]{bubbleSort}
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;
}
``` ```
=== "C#" === "C#"
@ -1728,20 +1675,7 @@ $$
=== "C" === "C"
```c title="time_complexity.c" ```c title="time_complexity.c"
/* 指数阶(循环实现) */ [class]{}-[func]{exponential}
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;
}
``` ```
=== "C#" === "C#"
@ -1875,11 +1809,7 @@ $$
=== "C" === "C"
```c title="time_complexity.c" ```c title="time_complexity.c"
/* 指数阶(递归实现) */ [class]{}-[func]{expRecur}
int expRecur(int n) {
if (n == 1) return 1;
return expRecur(n - 1) + expRecur(n - 1) + 1;
}
``` ```
=== "C#" === "C#"
@ -2008,15 +1938,7 @@ $$
=== "C" === "C"
```c title="time_complexity.c" ```c title="time_complexity.c"
/* 对数阶(循环实现) */ [class]{}-[func]{logarithmic}
int logarithmic(float n) {
int count = 0;
while (n > 1) {
n = n / 2;
count++;
}
return count;
}
``` ```
=== "C#" === "C#"
@ -2136,11 +2058,7 @@ $$
=== "C" === "C"
```c title="time_complexity.c" ```c title="time_complexity.c"
/* 对数阶(递归实现) */ [class]{}-[func]{logRecur}
int logRecur(float n) {
if (n <= 1) return 0;
return logRecur(n / 2) + 1;
}
``` ```
=== "C#" === "C#"
@ -2273,16 +2191,7 @@ $$
=== "C" === "C"
```c title="time_complexity.c" ```c title="time_complexity.c"
/* 线性对数阶 */ [class]{}-[func]{linearLogRecur}
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;
}
``` ```
=== "C#" === "C#"
@ -2441,15 +2350,7 @@ $$
=== "C" === "C"
```c title="time_complexity.c" ```c title="time_complexity.c"
/* 阶乘阶(递归实现) */ [class]{}-[func]{factorialRecur}
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;
}
``` ```
=== "C#" === "C#"

View file

@ -125,7 +125,89 @@ comments: true
=== "C++" === "C++"
```cpp title="graph_adjacency_matrix.cpp" ```cpp title="graph_adjacency_matrix.cpp"
[class]{GraphAdjMat}-[func]{} /* 基于邻接矩阵实现的无向图类 */
class GraphAdjMat {
vector<int> vertices; // 顶点列表,元素代表“顶点值”,索引代表“顶点索引”
vector<vector<int>> adjMat; // 邻接矩阵,行列索引对应“顶点索引”
public:
/* 构造方法 */
GraphAdjMat(const vector<int>& vertices, const vector<vector<int>>& edges) {
// 添加顶点
for (int val : vertices) {
addVertex(val);
}
// 添加边
// 请注意edges 元素代表顶点索引,即对应 vertices 元素索引
for (const vector<int>& 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<int>& 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<int>& 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" === "Python"
@ -236,13 +318,185 @@ comments: true
=== "JavaScript" === "JavaScript"
```javascript title="graph_adjacency_matrix.js" ```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"
```typescript title="graph_adjacency_matrix.ts" ```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" === "C"
@ -466,9 +720,80 @@ comments: true
=== "C++" === "C++"
```cpp title="graph_adjacency_list.cpp" ```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<Vertex*, unordered_set<Vertex*>> adjList; // 邻接表(使用哈希表实现)
public:
/* 构造方法 */
GraphAdjList(const vector<vector<Vertex*>>& edges) {
// 添加所有顶点和边
for (const vector<Vertex*>& 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<Vertex*>();
}
/* 删除顶点 */
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<int> tmp;
for (Vertex* vertex : value)
tmp.push_back(vertex->val);
cout << key->val << ": ";
PrintUtil::printVector(tmp);
}
}
};
``` ```
=== "Python" === "Python"

View file

@ -950,7 +950,9 @@ $$
=== "C" === "C"
```c title="array_hash_map.c" ```c title="array_hash_map.c"
[class]{entry}-[func]{}
[class]{arrayHashMap}-[func]{}
``` ```
=== "C#" === "C#"

View file

@ -362,7 +362,11 @@ comments: true
=== "C" === "C"
```c title="my_heap.c" ```c title="my_heap.c"
[class]{maxHeap}-[func]{left}
[class]{maxHeap}-[func]{right}
[class]{maxHeap}-[func]{parent}
``` ```
=== "C#" === "C#"
@ -472,7 +476,7 @@ comments: true
=== "C" === "C"
```c title="my_heap.c" ```c title="my_heap.c"
[class]{maxHeap}-[func]{peek}
``` ```
=== "C#" === "C#"
@ -668,7 +672,9 @@ comments: true
=== "C" === "C"
```c title="my_heap.c" ```c title="my_heap.c"
[class]{maxHeap}-[func]{push}
[class]{maxHeap}-[func]{siftUp}
``` ```
=== "C#" === "C#"
@ -821,8 +827,7 @@ comments: true
void poll() { void poll() {
// 判空处理 // 判空处理
if (empty()) { if (empty()) {
cout << "Error:堆为空" << endl; throw out_of_range("堆为空");
return;
} }
// 交换根结点与最右叶结点(即交换首元素与尾元素) // 交换根结点与最右叶结点(即交换首元素与尾元素)
swap(maxHeap[0], maxHeap[size() - 1]); swap(maxHeap[0], maxHeap[size() - 1]);
@ -977,7 +982,9 @@ comments: true
=== "C" === "C"
```c title="my_heap.c" ```c title="my_heap.c"
[class]{maxHeap}-[func]{poll}
[class]{maxHeap}-[func]{siftDown}
``` ```
=== "C#" === "C#"
@ -1156,7 +1163,7 @@ comments: true
=== "C" === "C"
```c title="my_heap.c" ```c title="my_heap.c"
[class]{maxHeap}-[func]{newMaxHeap}
``` ```
=== "C#" === "C#"

View file

@ -183,7 +183,7 @@ $$
=== "C" === "C"
```c title="binary_search.c" ```c title="binary_search.c"
[class]{}-[func]{binarySearch}
``` ```
=== "C#" === "C#"
@ -395,7 +395,7 @@ $$
=== "C" === "C"
```c title="binary_search.c" ```c title="binary_search.c"
[class]{}-[func]{binarySearch1}
``` ```
=== "C#" === "C#"

View file

@ -90,7 +90,7 @@ comments: true
=== "C" === "C"
```c title="hashing_search.c" ```c title="hashing_search.c"
[class]{}-[func]{hashingSearchArray}
``` ```
=== "C#" === "C#"
@ -206,7 +206,7 @@ comments: true
=== "C" === "C"
```c title="hashing_search.c" ```c title="hashing_search.c"
[class]{}-[func]{hashingSearchLinkedList}
``` ```
=== "C#" === "C#"

View file

@ -110,7 +110,7 @@ comments: true
=== "C" === "C"
```c title="linear_search.c" ```c title="linear_search.c"
[class]{}-[func]{linearSearchArray}
``` ```
=== "C#" === "C#"
@ -271,7 +271,7 @@ comments: true
=== "C" === "C"
```c title="linear_search.c" ```c title="linear_search.c"
[class]{}-[func]{linearSearchLinkedList}
``` ```
=== "C#" === "C#"

View file

@ -162,23 +162,7 @@ comments: true
=== "C" === "C"
```c title="bubble_sort.c" ```c title="bubble_sort.c"
/* 冒泡排序 */ [class]{}-[func]{bubbleSort}
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;
}
}
}
}
``` ```
=== "C#" === "C#"
@ -401,26 +385,7 @@ comments: true
=== "C" === "C"
```c title="bubble_sort.c" ```c title="bubble_sort.c"
/* 冒泡排序 */ [class]{}-[func]{bubbleSortWithFlag}
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;
}
}
``` ```
=== "C#" === "C#"

View file

@ -135,23 +135,7 @@ comments: true
=== "C" === "C"
```c title="insertion_sort.c" ```c title="insertion_sort.c"
/* 插入排序 */ [class]{}-[func]{insertionSort}
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#" === "C#"

View file

@ -198,7 +198,7 @@ comments: true
=== "C" === "C"
```c title="quick_sort.c" ```c title="quick_sort.c"
[class]{quickSort}-[func]{partition}
``` ```
=== "C#" === "C#"
@ -399,7 +399,7 @@ comments: true
=== "C" === "C"
```c title="quick_sort.c" ```c title="quick_sort.c"
[class]{quickSort}-[func]{quickSort}
``` ```
=== "C#" === "C#"
@ -694,7 +694,9 @@ comments: true
=== "C" === "C"
```c title="quick_sort.c" ```c title="quick_sort.c"
[class]{quickSortMedian}-[func]{medianThree}
[class]{quickSortMedian}-[func]{partition}
``` ```
=== "C#" === "C#"
@ -926,7 +928,7 @@ comments: true
=== "C" === "C"
```c title="quick_sort.c" ```c title="quick_sort.c"
[class]{quickSortTailCall}-[func]{quickSort}
``` ```
=== "C#" === "C#"

View file

@ -684,7 +684,7 @@ comments: true
=== "C" === "C"
```c title="linkedlist_queue.c" ```c title="linkedlist_queue.c"
[class]{linkedListQueue}-[func]{}
``` ```
=== "C#" === "C#"
@ -1367,7 +1367,7 @@ comments: true
=== "C" === "C"
```c title="array_queue.c" ```c title="array_queue.c"
[class]{arrayQueue}-[func]{}
``` ```
=== "C#" === "C#"

View file

@ -634,7 +634,7 @@ comments: true
=== "C" === "C"
```c title="linkedlist_stack.c" ```c title="linkedlist_stack.c"
[class]{linkedListStack}-[func]{}
``` ```
=== "C#" === "C#"
@ -1136,7 +1136,7 @@ comments: true
=== "C" === "C"
```c title="array_stack.c" ```c title="array_stack.c"
[class]{arrayStack}-[func]{}
``` ```
=== "C#" === "C#"

View file

@ -263,7 +263,9 @@ G. M. Adelson-Velsky 和 E. M. Landis 在其 1962 年发表的论文 "An algorit
=== "C" === "C"
```c title="avl_tree.c" ```c title="avl_tree.c"
[class]{aVLTree}-[func]{height}
[class]{aVLTree}-[func]{updateHeight}
``` ```
=== "C#" === "C#"
@ -398,9 +400,7 @@ G. M. Adelson-Velsky 和 E. M. Landis 在其 1962 年发表的论文 "An algorit
=== "C" === "C"
```c title="avl_tree.c" ```c title="avl_tree.c"
[class]{aVLTree}-[func]{balanceFactor}
``` ```
=== "C#" === "C#"
@ -582,7 +582,7 @@ AVL 树的独特之处在于「旋转 Rotation」的操作其可 **在不影
=== "C" === "C"
```c title="avl_tree.c" ```c title="avl_tree.c"
[class]{aVLTree}-[func]{rightRotate}
``` ```
=== "C#" === "C#"
@ -762,9 +762,7 @@ AVL 树的独特之处在于「旋转 Rotation」的操作其可 **在不影
=== "C" === "C"
```c title="avl_tree.c" ```c title="avl_tree.c"
[class]{aVLTree}-[func]{leftRotate}
``` ```
=== "C#" === "C#"
@ -1058,7 +1056,7 @@ AVL 树的独特之处在于「旋转 Rotation」的操作其可 **在不影
=== "C" === "C"
```c title="avl_tree.c" ```c title="avl_tree.c"
[class]{aVLTree}-[func]{rotate}
``` ```
=== "C#" === "C#"
@ -1345,9 +1343,9 @@ AVL 树的独特之处在于「旋转 Rotation」的操作其可 **在不影
=== "C" === "C"
```c title="avl_tree.c" ```c title="avl_tree.c"
[class]{aVLTree}-[func]{insert}
[class]{aVLTree}-[func]{insertHelper}
``` ```
=== "C#" === "C#"
@ -1761,7 +1759,11 @@ AVL 树的独特之处在于「旋转 Rotation」的操作其可 **在不影
=== "C" === "C"
```c title="avl_tree.c" ```c title="avl_tree.c"
[class]{aVLTree}-[func]{remove}
[class]{aVLTree}-[func]{removeHelper}
[class]{aVLTree}-[func]{getInOrderNext}
``` ```
=== "C#" === "C#"

View file

@ -163,7 +163,7 @@ comments: true
=== "C" === "C"
```c title="binary_search_tree.c" ```c title="binary_search_tree.c"
[class]{binarySearchTree}-[func]{search}
``` ```
=== "C#" === "C#"
@ -432,7 +432,7 @@ comments: true
=== "C" === "C"
```c title="binary_search_tree.c" ```c title="binary_search_tree.c"
[class]{binarySearchTree}-[func]{insert}
``` ```
=== "C#" === "C#"
@ -927,7 +927,9 @@ comments: true
=== "C" === "C"
```c title="binary_search_tree.c" ```c title="binary_search_tree.c"
[class]{binarySearchTree}-[func]{remove}
[class]{binarySearchTree}-[func]{getInOrderNext}
``` ```
=== "C#" === "C#"

View file

@ -158,7 +158,7 @@ comments: true
=== "C" === "C"
```c title="binary_tree_bfs.c" ```c title="binary_tree_bfs.c"
[class]{}-[func]{hierOrder}
``` ```
=== "C#" === "C#"
@ -461,7 +461,11 @@ comments: true
=== "C" === "C"
```c title="binary_tree_dfs.c" ```c title="binary_tree_dfs.c"
[class]{}-[func]{preOrder}
[class]{}-[func]{inOrder}
[class]{}-[func]{postOrder}
``` ```
=== "C#" === "C#"