From 770e3ca4ce052cba5936a9a0874cc2b64a1876e4 Mon Sep 17 00:00:00 2001 From: L-Super <40905056+L-Super@users.noreply.github.com> Date: Mon, 26 Dec 2022 10:17:35 +0800 Subject: [PATCH 1/5] Update bubble_sort.md MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit C++使用std::swap()交换数组,同时添加C语言代码,作为原始C++代码的补充 --- docs/chapter_sorting/bubble_sort.md | 49 ++++++++++++++++++++++++----- 1 file changed, 41 insertions(+), 8 deletions(-) diff --git a/docs/chapter_sorting/bubble_sort.md b/docs/chapter_sorting/bubble_sort.md index f44b11924..522f17af5 100644 --- a/docs/chapter_sorting/bubble_sort.md +++ b/docs/chapter_sorting/bubble_sort.md @@ -85,9 +85,7 @@ comments: true 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; + std::swap(nums[j], nums[j+1]); } } } @@ -170,7 +168,24 @@ comments: true === "C" ```c title="bubble_sort.c" - + /* 冒泡排序 */ + void bubble_sort(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#" @@ -250,9 +265,7 @@ comments: true 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; + std::swap(nums[j], nums[j+1]); flag = true; // 记录交换元素 } } @@ -352,7 +365,27 @@ comments: true === "C" ```c title="bubble_sort.c" - + /* 冒泡排序 */ + void bubble_sort(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(!falg) break; + } + } ``` === "C#" From 75be76cebeaa6f6458349d69c5e1885cff613646 Mon Sep 17 00:00:00 2001 From: L-Super <40905056+L-Super@users.noreply.github.com> Date: Mon, 26 Dec 2022 12:11:21 +0800 Subject: [PATCH 2/5] Update bubble_sort.md fixed `falg` to `flag` --- docs/chapter_sorting/bubble_sort.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/chapter_sorting/bubble_sort.md b/docs/chapter_sorting/bubble_sort.md index 522f17af5..f71dce77d 100644 --- a/docs/chapter_sorting/bubble_sort.md +++ b/docs/chapter_sorting/bubble_sort.md @@ -383,7 +383,7 @@ comments: true flag = true; } } - if(!falg) break; + if(!flag) break; } } ``` From 2ee6fcfef114070012cdb5bb35cdaed25bd7af7a Mon Sep 17 00:00:00 2001 From: Listening <120311070@qq.com> Date: Mon, 26 Dec 2022 12:43:37 +0800 Subject: [PATCH 3/5] add bubble sort in C code --- codes/c/chapter_sorting/bubble_sort.c | 72 +++++++++++++++++++++++++++ codes/c/include/include.h | 2 + 2 files changed, 74 insertions(+) create mode 100644 codes/c/chapter_sorting/bubble_sort.c create mode 100644 codes/c/include/include.h diff --git a/codes/c/chapter_sorting/bubble_sort.c b/codes/c/chapter_sorting/bubble_sort.c new file mode 100644 index 000000000..3d23a5420 --- /dev/null +++ b/codes/c/chapter_sorting/bubble_sort.c @@ -0,0 +1,72 @@ +/** + * @file bubble_sort.c + * @author Listening (https://github.com/L-Super) + * @brief + * @date 2022-12-26 + * + */ + +#include "../include/include.h" + +/* 冒泡排序 */ +void bubble_sort(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; + } + } + } +} + +/* 冒泡排序(标志优化)*/ +void bubble_sort_with_flag(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; + } +} + +int main() +{ + int nums[6] = {4, 1, 3, 1, 5, 2}; + printf("冒泡排序后:\n"); + bubble_sort(nums, 6); + for (int i = 0; i < 6; i++) + { + printf("%d ", nums[i]); + } + + printf("优化版冒泡排序后:\n"); + bubble_sort_with_flag(nums, 6); + for (int i = 0; i < 6; i++) + { + printf("%d ", nums[i]); + } + printf("\n"); + + return 0; +} \ No newline at end of file diff --git a/codes/c/include/include.h b/codes/c/include/include.h new file mode 100644 index 000000000..44843254e --- /dev/null +++ b/codes/c/include/include.h @@ -0,0 +1,2 @@ +#include +#include \ No newline at end of file From edf1029ac4e59aa923f59ba0508bbea4eda5185d Mon Sep 17 00:00:00 2001 From: Listening <120311070@qq.com> Date: Mon, 26 Dec 2022 13:34:50 +0800 Subject: [PATCH 4/5] fixed the format of the file header --- codes/c/chapter_sorting/bubble_sort.c | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/codes/c/chapter_sorting/bubble_sort.c b/codes/c/chapter_sorting/bubble_sort.c index 3d23a5420..f59b81d00 100644 --- a/codes/c/chapter_sorting/bubble_sort.c +++ b/codes/c/chapter_sorting/bubble_sort.c @@ -1,9 +1,7 @@ /** - * @file bubble_sort.c - * @author Listening (https://github.com/L-Super) - * @brief - * @date 2022-12-26 - * + * File: bubble_sort.c + * Created Time: 2022-12-26 + * Author: Listening (https://github.com/L-Super) */ #include "../include/include.h" From 34ad07bfed3b09accc30b5328a82e74865b5b7d5 Mon Sep 17 00:00:00 2001 From: Yudong Jin Date: Tue, 27 Dec 2022 20:11:22 +0800 Subject: [PATCH 5/5] Fine tune --- codes/c/chapter_sorting/bubble_sort.c | 2 ++ codes/cpp/chapter_sorting/bubble_sort.cpp | 10 ++++------ docs/chapter_sorting/bubble_sort.md | 6 ++++-- 3 files changed, 10 insertions(+), 8 deletions(-) diff --git a/codes/c/chapter_sorting/bubble_sort.c b/codes/c/chapter_sorting/bubble_sort.c index f59b81d00..6b67bd727 100644 --- a/codes/c/chapter_sorting/bubble_sort.c +++ b/codes/c/chapter_sorting/bubble_sort.c @@ -48,6 +48,8 @@ void bubble_sort_with_flag(int nums[], int size) } } + +/* Driver Code */ int main() { int nums[6] = {4, 1, 3, 1, 5, 2}; diff --git a/codes/cpp/chapter_sorting/bubble_sort.cpp b/codes/cpp/chapter_sorting/bubble_sort.cpp index 071827455..87ca305d1 100644 --- a/codes/cpp/chapter_sorting/bubble_sort.cpp +++ b/codes/cpp/chapter_sorting/bubble_sort.cpp @@ -14,9 +14,8 @@ void bubbleSort(vector& nums) { 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; + // 这里使用了 std::swap() 函数 + swap(nums[j], nums[j + 1]); } } } @@ -31,9 +30,8 @@ void bubbleSortWithFlag(vector& nums) { 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; + // 这里使用了 std::swap() 函数 + swap(nums[j], nums[j + 1]); flag = true; // 记录交换元素 } } diff --git a/docs/chapter_sorting/bubble_sort.md b/docs/chapter_sorting/bubble_sort.md index f71dce77d..336f3c2d5 100644 --- a/docs/chapter_sorting/bubble_sort.md +++ b/docs/chapter_sorting/bubble_sort.md @@ -85,7 +85,8 @@ comments: true for (int j = 0; j < i; j++) { if (nums[j] > nums[j + 1]) { // 交换 nums[j] 与 nums[j + 1] - std::swap(nums[j], nums[j+1]); + // 这里使用了 std::swap() 函数 + swap(nums[j], nums[j + 1]); } } } @@ -265,7 +266,8 @@ comments: true for (int j = 0; j < i; j++) { if (nums[j] > nums[j + 1]) { // 交换 nums[j] 与 nums[j + 1] - std::swap(nums[j], nums[j+1]); + // 这里使用了 std::swap() 函数 + swap(nums[j], nums[j + 1]); flag = true; // 记录交换元素 } }