Add sorce code blocks of C to the docs.

This commit is contained in:
krahets 2023-02-11 18:22:27 +08:00
parent d3ea84ba97
commit d1e1d76162
18 changed files with 78 additions and 206 deletions

View file

@ -373,7 +373,9 @@ comments: true
=== "C"
```c title="linked_list.c"
[class]{}-[func]{insertNode}
[class]{}-[func]{removeNode}
```
=== "C#"
@ -443,7 +445,7 @@ comments: true
=== "C"
```c title="linked_list.c"
[class]{}-[func]{access}
```
=== "C#"
@ -509,7 +511,7 @@ comments: true
=== "C"
```c title="linked_list.c"
[class]{}-[func]{findNode}
```
=== "C#"

View file

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

View file

@ -618,7 +618,7 @@ $$
=== "C"
```c title="space_complexity.c"
[class]{}-[func]{spaceConstant}
```
=== "C#"
@ -682,7 +682,7 @@ $$
=== "C"
```c title="space_complexity.c"
[class]{}-[func]{spaceLinear}
```
=== "C#"
@ -744,7 +744,7 @@ $$
=== "C"
```c title="space_complexity.c"
[class]{}-[func]{spaceLinearRecur}
```
=== "C#"
@ -812,7 +812,7 @@ $$
=== "C"
```c title="space_complexity.c"
[class]{}-[func]{spaceQuadratic}
```
=== "C#"
@ -874,7 +874,7 @@ $$
=== "C"
```c title="space_complexity.c"
[class]{}-[func]{spaceQuadraticRecur}
```
=== "C#"
@ -942,7 +942,7 @@ $$
=== "C"
```c title="space_complexity.c"
[class]{}-[func]{buildTree}
```
=== "C#"

View file

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

View file

@ -831,16 +831,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#"
@ -904,14 +895,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#"
@ -977,15 +961,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#"
@ -1049,17 +1025,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#"
@ -1129,26 +1095,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#"
@ -1216,20 +1163,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#"
@ -1295,11 +1229,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#"
@ -1367,15 +1297,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#"
@ -1441,11 +1363,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#"
@ -1511,16 +1429,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#"
@ -1594,15 +1503,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#"

View file

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

View file

@ -316,7 +316,11 @@ comments: true
=== "C"
```c title="my_heap.c"
[class]{maxHeap}-[func]{left}
[class]{maxHeap}-[func]{right}
[class]{maxHeap}-[func]{parent}
```
=== "C#"
@ -392,7 +396,7 @@ comments: true
=== "C"
```c title="my_heap.c"
[class]{maxHeap}-[func]{peek}
```
=== "C#"
@ -488,7 +492,9 @@ comments: true
=== "C"
```c title="my_heap.c"
[class]{maxHeap}-[func]{push}
[class]{maxHeap}-[func]{siftUp}
```
=== "C#"
@ -606,7 +612,9 @@ comments: true
=== "C"
```c title="my_heap.c"
[class]{maxHeap}-[func]{poll}
[class]{maxHeap}-[func]{siftDown}
```
=== "C#"
@ -678,7 +686,7 @@ comments: true
=== "C"
```c title="my_heap.c"
[class]{maxHeap}-[func]{newMaxHeap}
```
=== "C#"

View file

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

View file

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

View file

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

View file

@ -86,23 +86,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#"
@ -180,26 +164,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#"

View file

@ -63,23 +63,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#"

View file

@ -90,7 +90,7 @@ comments: true
=== "C"
```c title="quick_sort.c"
[class]{quickSort}-[func]{partition}
```
=== "C#"
@ -172,7 +172,7 @@ comments: true
=== "C"
```c title="quick_sort.c"
[class]{quickSort}-[func]{quickSort}
```
=== "C#"
@ -274,7 +274,9 @@ comments: true
=== "C"
```c title="quick_sort.c"
[class]{quickSortMedian}-[func]{medianThree}
[class]{quickSortMedian}-[func]{partition}
```
=== "C#"
@ -346,7 +348,7 @@ comments: true
=== "C"
```c title="quick_sort.c"
[class]{quickSortTailCall}-[func]{quickSort}
```
=== "C#"

View file

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

View file

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

View file

@ -206,7 +206,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#"
@ -276,9 +278,7 @@ G. M. Adelson-Velsky 和 E. M. Landis 在其 1962 年发表的论文 "An algorit
=== "C"
```c title="avl_tree.c"
[class]{aVLTree}-[func]{balanceFactor}
```
=== "C#"
@ -370,7 +370,7 @@ AVL 树的独特之处在于「旋转 Rotation」的操作其可 **在不影
=== "C"
```c title="avl_tree.c"
[class]{aVLTree}-[func]{rightRotate}
```
=== "C#"
@ -442,9 +442,7 @@ AVL 树的独特之处在于「旋转 Rotation」的操作其可 **在不影
=== "C"
```c title="avl_tree.c"
[class]{aVLTree}-[func]{leftRotate}
```
=== "C#"
@ -537,7 +535,7 @@ AVL 树的独特之处在于「旋转 Rotation」的操作其可 **在不影
=== "C"
```c title="avl_tree.c"
[class]{aVLTree}-[func]{rotate}
```
=== "C#"
@ -615,9 +613,9 @@ AVL 树的独特之处在于「旋转 Rotation」的操作其可 **在不影
=== "C"
```c title="avl_tree.c"
[class]{aVLTree}-[func]{insert}
[class]{aVLTree}-[func]{insertHelper}
```
=== "C#"
@ -711,7 +709,11 @@ AVL 树的独特之处在于「旋转 Rotation」的操作其可 **在不影
=== "C"
```c title="avl_tree.c"
[class]{aVLTree}-[func]{remove}
[class]{aVLTree}-[func]{removeHelper}
[class]{aVLTree}-[func]{getInOrderNext}
```
=== "C#"

View file

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

View file

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