From 1b356ba0a2b0c67b549cee72fe9ea5dd191d24cd Mon Sep 17 00:00:00 2001 From: sjinzh <99076655+sjinzh@users.noreply.github.com> Date: Tue, 3 Jan 2023 21:19:11 +0800 Subject: [PATCH 1/2] add c code for chapter_computational_complexity --- .../time_complexity.c | 192 ++++++++++++++++++ .../worst_best_time_complexity.c | 65 ++++++ 2 files changed, 257 insertions(+) create mode 100644 codes/c/chapter_computational_complexity/time_complexity.c create mode 100644 codes/c/chapter_computational_complexity/worst_best_time_complexity.c diff --git a/codes/c/chapter_computational_complexity/time_complexity.c b/codes/c/chapter_computational_complexity/time_complexity.c new file mode 100644 index 000000000..b7e6cef9c --- /dev/null +++ b/codes/c/chapter_computational_complexity/time_complexity.c @@ -0,0 +1,192 @@ +// File: time_complexity.c +// Created Time: 2023-01-03 +// Author: sjinzh (sjinzh@gmail.com) + +#include "stdio.h" +#include "stdlib.h" + +// 常数阶 +int constant(int n) +{ + int count = 0; + int size = 100000; + int i = 0; + for(int i = 0; i < size; i++){ + count ++; + } + return count; +} + +// 线性阶 +int linear(int n) +{ + int count = 0; + for(int i = 0; i < n; i++){ + count ++; + } + return count; +} + +// 线性阶(遍历数组) +int arrayTraversal(int *nums, int n) +{ + int count = 0; + // 循环次数与数组长度成正比 + for(int i = 0; i < n; i++){ + count ++; + } + return count; +} + +// 平方阶 +int quadratic(int n) +{ + int count = 0; + // 循环次数与数组长度成平方关系 + for(int i = 0; i < n; i++){ + for(int j = 0; j < n; j++){ + count ++; + } + } + return count; +} + +// 平方阶(冒泡排序) +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++) + { + // 交换 nums[j] 与 nums[j + 1] + int tmp = nums[j]; + nums[j] = nums[j + 1]; + nums[j + 1] = tmp; + count += 3; // 元素交换包含 3 个单元操作 + } + + } + return count; +} + +// 指数阶(循环实现) +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; +} + +// 指数阶(递归实现) +int expRecur(int n) +{ + if (n == 1) return 1; + return expRecur(n - 1) + expRecur(n - 1) + 1; +} + +// 对数阶(循环实现) +int logarithmic(float n) +{ + int count = 0; + while (n > 1) + { + n = n / 2; + count++; + } + return count; +} + +// 对数阶(递归实现) +int logRecur(float n) +{ + if (n <= 1) return 0; + return logRecur(n / 2) + 1; +} + +// 线性对数阶 +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; +} + +// 阶乘阶(递归实现) +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; +} + +// Driver Code +int main(int argc, char *argv[]) + { + // 可以修改 n 运行,体会一下各种复杂度的操作数量变化趋势 + int n = 8; + printf("输入数据大小 n = %d\n", n); + + int count = constant(n); + printf("常数阶的计算操作数量 = %d\n", count); + + count = linear(n); + printf("线性阶的计算操作数量 = %d\n", count); + // 分配堆区内存(创建一维可变长数组:数组中元素数量为n,元素类型为int) + int *nums = (int *)malloc(n * sizeof(int)); + count = arrayTraversal(nums, n); + printf("线性阶(遍历数组)的计算操作数量 = %d\n", count); + + count = quadratic(n); + printf("平方阶的计算操作数量 = %d\n", count); + for(int i = 0; i < n; i++){ + nums[i] = n - i; // [n,n-1,...,2,1] + } + count = bubbleSort(nums, n); + printf("平方阶(冒泡排序)的计算操作数量 = %d\n", count); + + count = exponential(n); + printf("指数阶(循环实现)的计算操作数量 = %d\n", count); + count = expRecur(n); + printf("指数阶(递归实现)的计算操作数量 = %d\n", count); + + count = logarithmic(n); + printf("对数阶(循环实现)的计算操作数量 = %d\n", count); + count = logRecur(n); + printf("对数阶(递归实现)的计算操作数量 = %d\n", count); + + count = linearLogRecur(n); + printf("线性对数阶(递归实现)的计算操作数量 = %d\n", count); + + count = factorialRecur(n); + printf("阶乘阶(递归实现)的计算操作数量 = %d\n", count); + + // 释放堆区内存 + if(nums != NULL){ + free(nums); + nums = NULL; + } + getchar(); + return 0; +} + diff --git a/codes/c/chapter_computational_complexity/worst_best_time_complexity.c b/codes/c/chapter_computational_complexity/worst_best_time_complexity.c new file mode 100644 index 000000000..11139630f --- /dev/null +++ b/codes/c/chapter_computational_complexity/worst_best_time_complexity.c @@ -0,0 +1,65 @@ +// File: worst_best_time_complexity.c +// Created Time: 2023-01-03 +// Author: sjinzh (sjinzh@gmail.com) + +#include "stdio.h" +#include "stdlib.h" +#include "time.h" + +// 生成一个数组,元素为 { 1, 2, ..., n },顺序被打乱 +int *randomNumbers(int n) +{ + // 分配堆区内存(创建一维可变长数组:数组中元素数量为n,元素类型为int) + int *nums = (int *)malloc(n * sizeof(int)); + // 生成数组 nums = { 1, 2, 3, ..., n } + for (int i = 0; i < n; i++) + { + nums[i] = i + 1; + } + // 随机打乱数组元素 + for (int i = n - 1; i > 0; i--) + { + int j = rand() % (i + 1); + int temp = nums[i]; + nums[i] = nums[j]; + nums[j] = temp; + } + return nums; +} + +// 查找数组 nums 中数字 1 所在索引 +int findOne(int *nums, int n) +{ + for (int i = 0; i < n; i++) + { + if (nums[i] == 1) return i; + } + return -1; +} + +// Driver Code +int main(int argc, char *argv[]) +{ + // 初始化随机数种子 + srand((unsigned int)time(NULL)); + for (int i = 0; i < 10; i++) + { + int n = 100; + int *nums = randomNumbers(n); + int index = findOne(nums, n); + printf("\n数组 [ 1, 2, ..., n ] 被打乱后 = "); + for (int j = 0; j < n; j++) + { + printf("%d%s", nums[j], (j == n-1)? "" : "," ); + } + printf("\n数字 1 的索引为 %d\n", index); + // 释放堆区内存 + if(nums != NULL){ + free(nums); + nums = NULL; + } + } + getchar(); + return 0; +} + From 9e4a5fd6d897bb70e049758ef3ccb975e96c30a8 Mon Sep 17 00:00:00 2001 From: sjinzh <99076655+sjinzh@users.noreply.github.com> Date: Wed, 4 Jan 2023 08:09:22 +0800 Subject: [PATCH 2/2] update c code for chapter_computational_complexity, add c code to time_complexity.md --- .../time_complexity.c | 35 +-- .../worst_best_time_complexity.c | 18 +- codes/c/include/include.h | 4 +- .../time_complexity.md | 249 ++++++++++++++++-- 4 files changed, 264 insertions(+), 42 deletions(-) diff --git a/codes/c/chapter_computational_complexity/time_complexity.c b/codes/c/chapter_computational_complexity/time_complexity.c index b7e6cef9c..4355ea3ad 100644 --- a/codes/c/chapter_computational_complexity/time_complexity.c +++ b/codes/c/chapter_computational_complexity/time_complexity.c @@ -1,11 +1,12 @@ -// File: time_complexity.c -// Created Time: 2023-01-03 -// Author: sjinzh (sjinzh@gmail.com) +/* + * File: time_complexity.c + * Created Time: 2023-01-03 + * Author: sjinzh (sjinzh@gmail.com) + */ -#include "stdio.h" -#include "stdlib.h" +#include "../include/include.h" -// 常数阶 +/* 常数阶 */ int constant(int n) { int count = 0; @@ -17,7 +18,7 @@ int constant(int n) return count; } -// 线性阶 +/* 线性阶 */ int linear(int n) { int count = 0; @@ -27,7 +28,7 @@ int linear(int n) return count; } -// 线性阶(遍历数组) +/* 线性阶(遍历数组) */ int arrayTraversal(int *nums, int n) { int count = 0; @@ -38,7 +39,7 @@ int arrayTraversal(int *nums, int n) return count; } -// 平方阶 +/* 平方阶 */ int quadratic(int n) { int count = 0; @@ -51,7 +52,7 @@ int quadratic(int n) return count; } -// 平方阶(冒泡排序) +/* 平方阶(冒泡排序) */ int bubbleSort(int *nums, int n) { int count = 0; // 计数器 @@ -71,7 +72,7 @@ int bubbleSort(int *nums, int n) return count; } -// 指数阶(循环实现) +/* 指数阶(循环实现) */ int exponential(int n) { int count = 0; @@ -89,14 +90,14 @@ int exponential(int n) return count; } -// 指数阶(递归实现) +/* 指数阶(递归实现) */ int expRecur(int n) { if (n == 1) return 1; return expRecur(n - 1) + expRecur(n - 1) + 1; } -// 对数阶(循环实现) +/* 对数阶(循环实现) */ int logarithmic(float n) { int count = 0; @@ -108,14 +109,14 @@ int logarithmic(float n) return count; } -// 对数阶(递归实现) +/* 对数阶(递归实现) */ int logRecur(float n) { if (n <= 1) return 0; return logRecur(n / 2) + 1; } -// 线性对数阶 +/* 线性对数阶 */ int linearLogRecur(float n) { if (n <= 1) return 1; @@ -128,7 +129,7 @@ int linearLogRecur(float n) return count; } -// 阶乘阶(递归实现) +/* 阶乘阶(递归实现) */ int factorialRecur(int n) { if (n == 0) return 1; @@ -140,7 +141,7 @@ int factorialRecur(int n) return count; } -// Driver Code +/* Driver Code */ int main(int argc, char *argv[]) { // 可以修改 n 运行,体会一下各种复杂度的操作数量变化趋势 diff --git a/codes/c/chapter_computational_complexity/worst_best_time_complexity.c b/codes/c/chapter_computational_complexity/worst_best_time_complexity.c index 11139630f..fc37ae0f0 100644 --- a/codes/c/chapter_computational_complexity/worst_best_time_complexity.c +++ b/codes/c/chapter_computational_complexity/worst_best_time_complexity.c @@ -1,12 +1,12 @@ -// File: worst_best_time_complexity.c -// Created Time: 2023-01-03 -// Author: sjinzh (sjinzh@gmail.com) +/* + * File: worst_best_time_complexity.c + * Created Time: 2023-01-03 + * Author: sjinzh (sjinzh@gmail.com) + */ -#include "stdio.h" -#include "stdlib.h" -#include "time.h" +#include "../include/include.h" -// 生成一个数组,元素为 { 1, 2, ..., n },顺序被打乱 +/* 生成一个数组,元素为 { 1, 2, ..., n },顺序被打乱 */ int *randomNumbers(int n) { // 分配堆区内存(创建一维可变长数组:数组中元素数量为n,元素类型为int) @@ -27,7 +27,7 @@ int *randomNumbers(int n) return nums; } -// 查找数组 nums 中数字 1 所在索引 +/* 查找数组 nums 中数字 1 所在索引 */ int findOne(int *nums, int n) { for (int i = 0; i < n; i++) @@ -37,7 +37,7 @@ int findOne(int *nums, int n) return -1; } -// Driver Code +/* Driver Code */ int main(int argc, char *argv[]) { // 初始化随机数种子 diff --git a/codes/c/include/include.h b/codes/c/include/include.h index 44843254e..8ce104cd2 100644 --- a/codes/c/include/include.h +++ b/codes/c/include/include.h @@ -1,2 +1,4 @@ #include -#include \ No newline at end of file +#include +#include +#include \ No newline at end of file diff --git a/docs/chapter_computational_complexity/time_complexity.md b/docs/chapter_computational_complexity/time_complexity.md index 539c9b5cb..5d61dc005 100644 --- a/docs/chapter_computational_complexity/time_complexity.md +++ b/docs/chapter_computational_complexity/time_complexity.md @@ -91,7 +91,17 @@ $$ === "C" ```c title="" - + // 在某运行平台下 + void algorithm(int n) + { + int a = 2; // 1 ns + a = a + 1; // 1 ns + a = a * 2; // 10 ns + // 循环 n 次 + for (int i = 0; i < n; i++) { // 1 ns ,每轮都要执行 i++ + printf("%d", 0); // 5 ns + } + } ``` === "C#" @@ -232,7 +242,25 @@ $$ === "C" ```c title="" - + // 算法 A 时间复杂度:常数阶 + void algorithm_A(int n) + { + printf("%d", 0); + } + // 算法 B 时间复杂度:线性阶 + void algorithm_B(int n) + { + for (int i = 0; i < n; i++) { + printf("%d", 0); + } + } + // 算法 C 时间复杂度:常数阶 + void algorithm_C(int n) + { + for (int i = 0; i < 1000000; i++) { + printf("%d", 0); + } + } ``` === "C#" @@ -373,7 +401,16 @@ $$ === "C" ```c title="" - + void algorithm(int n) + { + int a = 1; // +1 + a = a + 1; // +1 + a = a * 2; // +1 + // 循环 n 次 + for (int i = 0; i < n; i++) { // +1(每轮都执行 i ++) + printf("%d", 0); // +1 + } + } ``` === "C#" @@ -542,7 +579,21 @@ $$ === "C" ```c title="" - + void algorithm(int n) + { + int a = 1; // +0(技巧 1) + a = a + n; // +0(技巧 1) + // +n(技巧 2) + for (int i = 0; i < 5 * n + 1; i++) { + printf("%d", 0); + } + // +n*n(技巧 3) + for (int i = 0; i < 2 * n; i++) { + for (int j = 0; j < n + 1; j++) { + printf("%d", 0); + } + } + } ``` === "C#" @@ -697,7 +748,17 @@ $$ === "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; + } ``` === "C#" @@ -795,7 +856,15 @@ $$ === "C" ```c title="time_complexity.c" - + /* 线性阶 */ + int linear(int n) + { + int count = 0; + for(int i = 0; i < n; i++){ + count ++; + } + return count; + } ``` === "C#" @@ -899,7 +968,16 @@ $$ === "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; + } ``` === "C#" @@ -1012,7 +1090,18 @@ $$ === "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; + } ``` === "C#" @@ -1163,7 +1252,25 @@ $$ === "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++) + { + // 交换 nums[j] 与 nums[j + 1] + int tmp = nums[j]; + nums[j] = nums[j + 1]; + nums[j + 1] = tmp; + count += 3; // 元素交换包含 3 个单元操作 + } + + } + return count; + } ``` === "C#" @@ -1309,7 +1416,23 @@ $$ === "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; + } ``` === "C#" @@ -1414,7 +1537,12 @@ $$ === "C" ```c title="time_complexity.c" - + /* 指数阶(递归实现) */ + int expRecur(int n) + { + if (n == 1) return 1; + return expRecur(n - 1) + expRecur(n - 1) + 1; + } ``` === "C#" @@ -1517,7 +1645,17 @@ $$ === "C" ```c title="time_complexity.c" - + /* 对数阶(循环实现) */ + int logarithmic(float n) + { + int count = 0; + while (n > 1) + { + n = n / 2; + count++; + } + return count; + } ``` === "C#" @@ -1613,7 +1751,12 @@ $$ === "C" ```c title="time_complexity.c" - + /* 对数阶(递归实现) */ + int logRecur(float n) + { + if (n <= 1) return 0; + return logRecur(n / 2) + 1; + } ``` === "C#" @@ -1720,7 +1863,18 @@ $$ === "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; + } ``` === "C#" @@ -1845,7 +1999,17 @@ $$ === "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; + } ``` === "C#" @@ -2061,7 +2225,62 @@ $$ === "C" ```c title="worst_best_time_complexity.c" + /* 生成一个数组,元素为 { 1, 2, ..., n },顺序被打乱 */ + int *randomNumbers(int n) + { + // 分配堆区内存(创建一维可变长数组:数组中元素数量为n,元素类型为int) + int *nums = (int *)malloc(n * sizeof(int)); + // 生成数组 nums = { 1, 2, 3, ..., n } + for (int i = 0; i < n; i++) + { + nums[i] = i + 1; + } + // 随机打乱数组元素 + for (int i = n - 1; i > 0; i--) + { + int j = rand() % (i + 1); + int temp = nums[i]; + nums[i] = nums[j]; + nums[j] = temp; + } + return nums; + } + /* 查找数组 nums 中数字 1 所在索引 */ + int findOne(int *nums, int n) + { + for (int i = 0; i < n; i++) + { + if (nums[i] == 1) return i; + } + return -1; + } + + /* Driver Code */ + int main(int argc, char *argv[]) + { + // 初始化随机数种子 + srand((unsigned int)time(NULL)); + for (int i = 0; i < 10; i++) + { + int n = 100; + int *nums = randomNumbers(n); + int index = findOne(nums, n); + printf("\n数组 [ 1, 2, ..., n ] 被打乱后 = "); + for (int j = 0; j < n; j++) + { + printf("%d%s", nums[j], (j == n-1)? "" : "," ); + } + printf("\n数字 1 的索引为 %d\n", index); + // 释放堆区内存 + if(nums != NULL){ + free(nums); + nums = NULL; + } + } + getchar(); + return 0; + } ``` === "C#"