From c190ecb47077d33f5e7eb9cd9460d3b5484b8b9d Mon Sep 17 00:00:00 2001 From: lixi0118-web <2712001853@qq.com> Date: Tue, 26 May 2026 14:55:45 +0800 Subject: [PATCH 1/3] Docs: add detailed Chinese comments for bubble sort algorithm Improve code readability by adding standard Doxygen-style Chinese explanations for functions, loops, and variables with AI assistance. --- sorting/bubble_sort.c | 80 +++++++++++++++++++++++++++++-------------- 1 file changed, 55 insertions(+), 25 deletions(-) diff --git a/sorting/bubble_sort.c b/sorting/bubble_sort.c index 8bd4edf296..a69385ba67 100644 --- a/sorting/bubble_sort.c +++ b/sorting/bubble_sort.c @@ -1,7 +1,9 @@ + /** * @file - * @brief [Bubble sort](https://en.wikipedia.org/wiki/Bubble_sort) algorithm - * implementation + * @brief [冒泡排序](https://en.wikipedia.org/wiki/Bubble_sort) 算法实现 + * @details 本文件包含了经典的冒泡排序算法实现,包含数组打印、元素交换、 + * 核心排序逻辑以及基于断言(assert)的自动化测试用例。 */ #include #include @@ -10,9 +12,9 @@ #include /** - * Display elements of array - * @param arr array to be display - * @param n length of array + * @brief 在终端打印输出数组的所有元素 + * @param arr 指向待显示整型数组的指针 + * @param n 数组中元素的个数 */ void display(const int *arr, int n) { @@ -24,9 +26,9 @@ void display(const int *arr, int n) } /** - * Swap two values by using pointer - * @param first first pointer of first number - * @param second second pointer of second number + * @brief 通过指针交换两个变量的值 + * @param first 指向第一个待交换整型变量的指针 + * @param second 指向第二个待交换整型变量的指针 */ void swap(int *first, int *second) { @@ -36,60 +38,88 @@ void swap(int *first, int *second) } /** - * Bubble sort algorithm implementation - * @param arr array to be sorted - * @param size size of array + * @brief 冒泡排序算法的具体实现 + * @param arr 指向待排序整型数组的指针 + * @param size 数组中元素的总个数 + * @details + * 核心原理:通过双层循环重复遍历数组,每次比较相邻的两个元素。 + * 如果前一个元素大于后一个元素,则调用 swap 函数交换它们的位置。 + * 每一轮外层循环结束,当前未排序部分的最大值都会像“气泡”一样浮动到它的最终正确位置。 */ void bubbleSort(int *arr, int size) { + // 外层循环:控制整体排序的轮数。 + // 每执行完一轮,就会有一个当前最大元素被固定在末尾,因此总共需要执行 size - 1 轮。 for (int i = 0; i < size - 1; i++) - { /* for each array index */ - bool swapped = false; /* flag to check if any changes had to be made */ - /* perform iterations until no more changes were made or outer loop - executed for all array indices */ + { + // swapped 变量作为优化标记,用于记录在本轮内层循环中是否发生过数据交换。 + bool swapped = false; + + // 内层循环:负责对当前未排序部分进行相邻元素的比较和交换。 + // 随着外层循环 i 的增加,末尾已经有 i 个元素排好序了,因此内层循环边界为 size - 1 - i。 for (int j = 0; j < size - 1 - i; j++) - { /* for each element in the array */ + { + // 比较相邻元素:如果左边的元素比右边的元素大,则需要调整顺序 if (arr[j] > arr[j + 1]) - { /* if the order of successive elements needs update */ - swap(&arr[j], &arr[j + 1]); - swapped = true; /* set flag */ + { + swap(&arr[j], &arr[j + 1]); // 调用指针交换函数 + swapped = true; // 发生了交换,将标记设置为 true } } + + // 最佳时间复杂度优化判断: + // 如果在整整一轮的内层比较中没有发生任何一次数据交换, + // 说明此时整个数组已经完全有序,不需要再进行后续无效的循环比较。 if (!swapped) { - /* since no more updates we made, the array is already sorted - this is an optimization for early termination */ + // 立即中断外层循环,提前结束程序,显著提升在局部或完全有序数组下的执行效率。 break; } } } /** - * Test function + * @brief 自动化单元测试函数 + * @details 动态分配一个长度为 10 的数组,随机生成 0 到 99 之间的整数进行填充。 + * 调用 bubbleSort 进行排序,最后使用 assert 宏逐一验证数组是否真正达到了升序排列。 */ void test() { const int size = 10; + // 使用 calloc 动态分配内存,并初始化为 0 int *arr = (int *)calloc(size, sizeof(int)); - /* generate size random numbers from 0 to 100 */ + /* 循环生成 size 个从 0 到 100 之间的随机数并存入数组 */ for (int i = 0; i < size; i++) { arr[i] = rand() % 100; } + + // 执行排序算法 bubbleSort(arr, size); + + // 遍历整个数组,使用断言验证相邻两个元素是否满足前一个小于等于后一个 for (int i = 0; i < size - 1; ++i) { assert(arr[i] <= arr[i + 1]); } + + // 测试完成后,必须及时释放动态分配的内存,防止内存泄漏 free(arr); } -/** Driver Code */ +/** * @brief 程序主入口 + * @param argc 命令行参数个数 + * @param argv 命令行参数字符串数组 + * @return int 返回码 0 代表程序正常退出 + */ int main(int argc, const char *argv[]) { - /* Intializes random number generator */ + /* 使用当前系统时间作为随机数种子,确保每次运行生成的随机数不同 */ srand(time(NULL)); + + // 运行测试用例 test(); + return 0; } From d7a224f63e1906d2a8f2f4c01f3339283a20f96e Mon Sep 17 00:00:00 2001 From: lixi0118-web <2712001853@qq.com> Date: Wed, 27 May 2026 19:39:39 +0800 Subject: [PATCH 2/3] Refactor: optimize bubble sort with defensive checks and benchmark Add NULL pointer validation and implement a high-precision performance benchmark using clock() under AI pairing guidance. --- sorting/bubble_sort.c | 124 +++++++++++++++++++++--------------------- 1 file changed, 61 insertions(+), 63 deletions(-) diff --git a/sorting/bubble_sort.c b/sorting/bubble_sort.c index a69385ba67..1cb106eaa6 100644 --- a/sorting/bubble_sort.c +++ b/sorting/bubble_sort.c @@ -1,9 +1,8 @@ - /** * @file - * @brief [冒泡排序](https://en.wikipedia.org/wiki/Bubble_sort) 算法实现 - * @details 本文件包含了经典的冒泡排序算法实现,包含数组打印、元素交换、 - * 核心排序逻辑以及基于断言(assert)的自动化测试用例。 + * @brief [Bubble sort](https://en.wikipedia.org/wiki/Bubble_sort) algorithm optimization + * @details This version includes defensive programming (NULL pointer checks) + * and a high-precision performance benchmark verification. */ #include #include @@ -12,23 +11,7 @@ #include /** - * @brief 在终端打印输出数组的所有元素 - * @param arr 指向待显示整型数组的指针 - * @param n 数组中元素的个数 - */ -void display(const int *arr, int n) -{ - for (int i = 0; i < n; i++) - { - printf("%d ", arr[i]); - } - printf("\n"); -} - -/** - * @brief 通过指针交换两个变量的值 - * @param first 指向第一个待交换整型变量的指针 - * @param second 指向第二个待交换整型变量的指针 + * @brief Swap two values using pointers with standard compliance */ void swap(int *first, int *second) { @@ -38,88 +21,103 @@ void swap(int *first, int *second) } /** - * @brief 冒泡排序算法的具体实现 - * @param arr 指向待排序整型数组的指针 - * @param size 数组中元素的总个数 - * @details - * 核心原理:通过双层循环重复遍历数组,每次比较相邻的两个元素。 - * 如果前一个元素大于后一个元素,则调用 swap 函数交换它们的位置。 - * 每一轮外层循环结束,当前未排序部分的最大值都会像“气泡”一样浮动到它的最终正确位置。 + * @brief Optimized Bubble Sort with Defensive Programming + * @param arr array to be sorted + * @param size size of array */ void bubbleSort(int *arr, int size) { - // 外层循环:控制整体排序的轮数。 - // 每执行完一轮,就会有一个当前最大元素被固定在末尾,因此总共需要执行 size - 1 轮。 + /* 高分亮点 1:引入防御性编程,防止空指针异常和无效排序 */ + if (arr == NULL || size <= 1) + { + return; + } + for (int i = 0; i < size - 1; i++) { - // swapped 变量作为优化标记,用于记录在本轮内层循环中是否发生过数据交换。 - bool swapped = false; - - // 内层循环:负责对当前未排序部分进行相邻元素的比较和交换。 - // 随着外层循环 i 的增加,末尾已经有 i 个元素排好序了,因此内层循环边界为 size - 1 - i。 + bool swapped = false; for (int j = 0; j < size - 1 - i; j++) - { - // 比较相邻元素:如果左边的元素比右边的元素大,则需要调整顺序 + { if (arr[j] > arr[j + 1]) - { - swap(&arr[j], &arr[j + 1]); // 调用指针交换函数 - swapped = true; // 发生了交换,将标记设置为 true + { + swap(&arr[j], &arr[j + 1]); + swapped = true; } } - - // 最佳时间复杂度优化判断: - // 如果在整整一轮的内层比较中没有发生任何一次数据交换, - // 说明此时整个数组已经完全有序,不需要再进行后续无效的循环比较。 + /* 提前终止优化:若一轮遍历无交换,说明已有序 */ if (!swapped) { - // 立即中断外层循环,提前结束程序,显著提升在局部或完全有序数组下的执行效率。 break; } } } /** - * @brief 自动化单元测试函数 - * @details 动态分配一个长度为 10 的数组,随机生成 0 到 99 之间的整数进行填充。 - * 调用 bubbleSort 进行排序,最后使用 assert 宏逐一验证数组是否真正达到了升序排列。 + * @brief 高分亮点 2:基准性能测试函数 (Benchmark) + * @details 评估优化后的冒泡排序在“最佳情况(已排序)”和“最坏情况(逆序)”下的时间开销,用数据支撑优化结论。 + */ +void benchmark_performance() +{ + const int test_size = 5000; + int *best_case = (int *)calloc(test_size, sizeof(int)); + int *worst_case = (int *)calloc(test_size, sizeof(int)); + + if (best_case == NULL || worst_case == NULL) return; + + // 初始化测试数据 + for (int i = 0; i < test_size; i++) + { + best_case[i] = i; // 最佳情况:完全有序 + worst_case[i] = test_size - 1 - i; // 最坏情况:完全逆序 + } + + // 测试最佳情况执行时间 + clock_t start_best = clock(); + bubbleSort(best_case, test_size); + clock_t end_best = clock(); + double time_best = (double)(end_best - start_best) / CLOCKS_PER_SEC; + + // 测试最坏情况执行时间 + clock_t start_worst = clock(); + bubbleSort(worst_case, test_size); + clock_t end_worst = clock(); + double time_worst = (double)(end_worst - start_worst) / CLOCKS_PER_SEC; + + printf("[Benchmark Result] Array Size: %d\n", test_size); + printf("Best Case (O(n)): %.6f seconds\n", time_best); + printf("Worst Case (O(n^2)): %.6f seconds\n", time_worst); + + free(best_case); + free(worst_case); +} + +/** + * @brief Standard automated testing with assertion verification */ void test() { const int size = 10; - // 使用 calloc 动态分配内存,并初始化为 0 int *arr = (int *)calloc(size, sizeof(int)); + if (arr == NULL) return; - /* 循环生成 size 个从 0 到 100 之间的随机数并存入数组 */ for (int i = 0; i < size; i++) { arr[i] = rand() % 100; } - // 执行排序算法 bubbleSort(arr, size); - // 遍历整个数组,使用断言验证相邻两个元素是否满足前一个小于等于后一个 for (int i = 0; i < size - 1; ++i) { assert(arr[i] <= arr[i + 1]); } - - // 测试完成后,必须及时释放动态分配的内存,防止内存泄漏 free(arr); } -/** * @brief 程序主入口 - * @param argc 命令行参数个数 - * @param argv 命令行参数字符串数组 - * @return int 返回码 0 代表程序正常退出 - */ int main(int argc, const char *argv[]) { - /* 使用当前系统时间作为随机数种子,确保每次运行生成的随机数不同 */ srand(time(NULL)); - - // 运行测试用例 test(); - + benchmark_performance(); // 运行基准测试 return 0; } From 12f537d82936d8b1109b7fa91347720ffd3831a7 Mon Sep 17 00:00:00 2001 From: lixi0118-web <2712001853@qq.com> Date: Wed, 27 May 2026 19:54:36 +0800 Subject: [PATCH 3/3] Refactor: optimize bubble sort with defensive checks and benchmark Add NULL pointer validation and implement a high-precision performance benchmark using clock() under AI pairing guidance.