1
0
mirror of https://github.com/renbaoshuo/202401-programming-assignments.git synced 2024-11-23 15:48:42 +00:00
202401-programming-assignments/【实践课内】7.数组1/7-4 冒泡法排序.c

32 lines
556 B
C
Raw Normal View History

2024-11-06 02:32:30 +00:00
#include <stdio.h>
int n, k, a[200];
int main() {
scanf("%d%d", &n, &k);
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
}
for (int i = 1; i <= k; i++) {
for (int j = 2; j <= n; j++) {
if (a[j] < a[j - 1]) {
int t = a[j];
a[j] = a[j - 1];
a[j - 1] = t;
}
}
}
for (int i = 1; i <= n; i++) {
if (i < n) {
printf("%d ", a[i]);
} else {
printf("%d\n", a[i]);
}
}
return 0;
}