commit 8bea706d6b5f103ba125f89844b2c2851e729084 Author: lucius Date: Wed Sep 3 11:22:15 2025 +0800 新增:冒泡升序排序 diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..e33435e --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +bubble_sort \ No newline at end of file diff --git a/Makefile b/Makefile new file mode 100755 index 0000000..ec183b7 --- /dev/null +++ b/Makefile @@ -0,0 +1,19 @@ +# 目标文件 +TARGET = bubble_sort + +# 源文件 +SRC = bubble_sort.c + +# 编译器 +CC = gcc + +# 编译选项 +CFLAGS = -Wall -O2 + +# 默认目标 +$(TARGET): $(SRC) + $(CC) $(CFLAGS) -o $(TARGET) $(SRC) + +# 清理目标文件 +clean: + rm -f $(TARGET) diff --git a/README.md b/README.md new file mode 100644 index 0000000..117a1cd --- /dev/null +++ b/README.md @@ -0,0 +1,5 @@ +## V1.1.0 + +### 新增: + +冒泡升序排序 diff --git a/bubble_sort.c b/bubble_sort.c new file mode 100644 index 0000000..df8a0d5 --- /dev/null +++ b/bubble_sort.c @@ -0,0 +1,47 @@ +#include +#define N 8 + +/* 打印数组 */ +void bubble_sort_display(int *pInputArr) +{ + for (int i = 0; i < N; i++) + { + printf("%d ", pInputArr[i]); + } + printf("\n"); +} + +/* 冒泡排序(升序)*/ +void bubble_sort(int *pInputArr) +{ + int tTempI = 0, tTempJ = 0, tTemp = 0; + + for (tTempI = 0; tTempI < N - 1; tTempI++) + { + for (tTempJ = 0; tTempJ < N - tTempI - 1; tTempJ++) + { + if (pInputArr[tTempJ] > pInputArr[tTempJ + 1]) + { + /* 交换相邻元素 */ + tTemp = pInputArr[tTempJ]; + pInputArr[tTempJ] = pInputArr[tTempJ + 1]; + pInputArr[tTempJ + 1] = tTemp; + } + } + printf("第 %d 趟排序结果: ", tTempI + 1); + bubble_sort_display(pInputArr); + } +} + +int main(int argc, char **argv) +{ + int tTempArr[N] = {50, 36, 66, 76, 95, 70, 25, 37}; + + printf("排序前: "); + bubble_sort_display(tTempArr); + bubble_sort(tTempArr); + printf("排序后: "); + bubble_sort_display(tTempArr); + + return 0; +}