From 34ad07bfed3b09accc30b5328a82e74865b5b7d5 Mon Sep 17 00:00:00 2001 From: Yudong Jin Date: Tue, 27 Dec 2022 20:11:22 +0800 Subject: [PATCH] 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; // 记录交换元素 } }