新增:冒泡升序排序

This commit is contained in:
2025-09-03 11:22:15 +08:00
commit 8bea706d6b
4 changed files with 72 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@ -0,0 +1 @@
bubble_sort

19
Makefile Executable file
View File

@ -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)

5
README.md Normal file
View File

@ -0,0 +1,5 @@
## V1.1.0
### 新增:
冒泡升序排序

47
bubble_sort.c Normal file
View File

@ -0,0 +1,47 @@
#include <stdio.h>
#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;
}