From d9686e57dd2d502accda521a4f03666120df9dc7 Mon Sep 17 00:00:00 2001 From: krahets Date: Mon, 18 Dec 2023 22:52:00 +0800 Subject: [PATCH] Fix bubble_sort.c --- codes/c/chapter_sorting/bubble_sort.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/codes/c/chapter_sorting/bubble_sort.c b/codes/c/chapter_sorting/bubble_sort.c index 7de9483b7..223c2d016 100644 --- a/codes/c/chapter_sorting/bubble_sort.c +++ b/codes/c/chapter_sorting/bubble_sort.c @@ -11,7 +11,7 @@ void bubbleSort(int nums[], int size) { // 外循环:未排序区间为 [0, i] for (int i = size - 1; i > 0; i--) { // 内循环:将未排序区间 [0, i] 中的最大元素交换至该区间的最右端 - for (int j = 0; j < size - 1 - i; j++) { + for (int j = 0; j < i; j++) { if (nums[j] > nums[j + 1]) { int temp = nums[j]; nums[j] = nums[j + 1]; @@ -27,7 +27,7 @@ void bubbleSortWithFlag(int nums[], int size) { for (int i = size - 1; i > 0; i--) { bool flag = false; // 内循环:将未排序区间 [0, i] 中的最大元素交换至该区间的最右端 - for (int j = 0; j < size - 1 - i; j++) { + for (int j = 0; j < i; j++) { if (nums[j] > nums[j + 1]) { int temp = nums[j]; nums[j] = nums[j + 1];