From d1e1d76162b075cc7184801060db1f9a0d13060a Mon Sep 17 00:00:00 2001 From: krahets Date: Sat, 11 Feb 2023 18:22:27 +0800 Subject: [PATCH] Add sorce code blocks of C to the docs. --- .../linked_list.md | 6 +- docs/chapter_array_and_linkedlist/list.md | 2 +- .../space_complexity.md | 12 +- .../space_time_tradeoff.md | 4 +- .../time_complexity.md | 121 ++---------------- docs/chapter_hashing/hash_map.md | 4 +- docs/chapter_heap/heap.md | 12 +- docs/chapter_searching/binary_search.md | 4 +- docs/chapter_searching/hashing_search.md | 4 +- docs/chapter_searching/linear_search.md | 4 +- docs/chapter_sorting/bubble_sort.md | 39 +----- docs/chapter_sorting/insertion_sort.md | 18 +-- docs/chapter_sorting/quick_sort.md | 8 +- docs/chapter_stack_and_queue/queue.md | 4 +- docs/chapter_stack_and_queue/stack.md | 4 +- docs/chapter_tree/avl_tree.md | 22 ++-- docs/chapter_tree/binary_search_tree.md | 8 +- docs/chapter_tree/binary_tree_traversal.md | 8 +- 18 files changed, 78 insertions(+), 206 deletions(-) diff --git a/docs/chapter_array_and_linkedlist/linked_list.md b/docs/chapter_array_and_linkedlist/linked_list.md index ae6c627c9..f2df041fd 100755 --- a/docs/chapter_array_and_linkedlist/linked_list.md +++ b/docs/chapter_array_and_linkedlist/linked_list.md @@ -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#" diff --git a/docs/chapter_array_and_linkedlist/list.md b/docs/chapter_array_and_linkedlist/list.md index 822551479..a1edf9f44 100755 --- a/docs/chapter_array_and_linkedlist/list.md +++ b/docs/chapter_array_and_linkedlist/list.md @@ -752,7 +752,7 @@ comments: true === "C" ```c title="my_list.c" - + [class]{myList}-[func]{} ``` === "C#" diff --git a/docs/chapter_computational_complexity/space_complexity.md b/docs/chapter_computational_complexity/space_complexity.md index b55fafcc4..bf350a3c1 100755 --- a/docs/chapter_computational_complexity/space_complexity.md +++ b/docs/chapter_computational_complexity/space_complexity.md @@ -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#" diff --git a/docs/chapter_computational_complexity/space_time_tradeoff.md b/docs/chapter_computational_complexity/space_time_tradeoff.md index 0adb5c016..15aef9907 100755 --- a/docs/chapter_computational_complexity/space_time_tradeoff.md +++ b/docs/chapter_computational_complexity/space_time_tradeoff.md @@ -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#" diff --git a/docs/chapter_computational_complexity/time_complexity.md b/docs/chapter_computational_complexity/time_complexity.md index 7412b3f17..bdb55b4b6 100755 --- a/docs/chapter_computational_complexity/time_complexity.md +++ b/docs/chapter_computational_complexity/time_complexity.md @@ -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#" diff --git a/docs/chapter_hashing/hash_map.md b/docs/chapter_hashing/hash_map.md index 1f62e01d3..865a0e6f2 100755 --- a/docs/chapter_hashing/hash_map.md +++ b/docs/chapter_hashing/hash_map.md @@ -464,7 +464,9 @@ $$ === "C" ```c title="array_hash_map.c" - + [class]{entry}-[func]{} + + [class]{arrayHashMap}-[func]{} ``` === "C#" diff --git a/docs/chapter_heap/heap.md b/docs/chapter_heap/heap.md index b5c4f7fc5..286f99f74 100644 --- a/docs/chapter_heap/heap.md +++ b/docs/chapter_heap/heap.md @@ -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#" diff --git a/docs/chapter_searching/binary_search.md b/docs/chapter_searching/binary_search.md index 12ab6aa58..eb359fff2 100755 --- a/docs/chapter_searching/binary_search.md +++ b/docs/chapter_searching/binary_search.md @@ -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#" diff --git a/docs/chapter_searching/hashing_search.md b/docs/chapter_searching/hashing_search.md index 2a7b00675..007846d0d 100755 --- a/docs/chapter_searching/hashing_search.md +++ b/docs/chapter_searching/hashing_search.md @@ -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#" diff --git a/docs/chapter_searching/linear_search.md b/docs/chapter_searching/linear_search.md index bb1bf33eb..1a0b00275 100755 --- a/docs/chapter_searching/linear_search.md +++ b/docs/chapter_searching/linear_search.md @@ -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#" diff --git a/docs/chapter_sorting/bubble_sort.md b/docs/chapter_sorting/bubble_sort.md index 4581bb4fa..d3a5e8f6d 100755 --- a/docs/chapter_sorting/bubble_sort.md +++ b/docs/chapter_sorting/bubble_sort.md @@ -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#" diff --git a/docs/chapter_sorting/insertion_sort.md b/docs/chapter_sorting/insertion_sort.md index ff29ecaaf..b5562024d 100755 --- a/docs/chapter_sorting/insertion_sort.md +++ b/docs/chapter_sorting/insertion_sort.md @@ -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#" diff --git a/docs/chapter_sorting/quick_sort.md b/docs/chapter_sorting/quick_sort.md index ba1a32802..6037e81f2 100755 --- a/docs/chapter_sorting/quick_sort.md +++ b/docs/chapter_sorting/quick_sort.md @@ -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#" diff --git a/docs/chapter_stack_and_queue/queue.md b/docs/chapter_stack_and_queue/queue.md index 367f25ea2..581fb6218 100755 --- a/docs/chapter_stack_and_queue/queue.md +++ b/docs/chapter_stack_and_queue/queue.md @@ -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#" diff --git a/docs/chapter_stack_and_queue/stack.md b/docs/chapter_stack_and_queue/stack.md index 867c195af..bc51d0c7f 100755 --- a/docs/chapter_stack_and_queue/stack.md +++ b/docs/chapter_stack_and_queue/stack.md @@ -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#" diff --git a/docs/chapter_tree/avl_tree.md b/docs/chapter_tree/avl_tree.md index 29de45491..2c1ee4b0e 100644 --- a/docs/chapter_tree/avl_tree.md +++ b/docs/chapter_tree/avl_tree.md @@ -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#" diff --git a/docs/chapter_tree/binary_search_tree.md b/docs/chapter_tree/binary_search_tree.md index 01d9160fd..7a1890a9c 100755 --- a/docs/chapter_tree/binary_search_tree.md +++ b/docs/chapter_tree/binary_search_tree.md @@ -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#" diff --git a/docs/chapter_tree/binary_tree_traversal.md b/docs/chapter_tree/binary_tree_traversal.md index 5458be7b0..04b641d00 100755 --- a/docs/chapter_tree/binary_tree_traversal.md +++ b/docs/chapter_tree/binary_tree_traversal.md @@ -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#"