BUPT-homework/pset10/1-sort.c

39 lines
740 B
C
Raw Normal View History

2021-12-01 21:05:19 +08:00
#include <stdio.h>
void bubbleSort(int a[], int n);
//输出数组中所有元素
void outputData(int data[], int elementCount);
int main() {
int n, i, num[10010];
scanf("%d", &n);
for (i = 0; i < n; i++)
scanf("%d", &num[i]);
bubbleSort(num, n);
outputData(num, n);
return 0;
}
/* 请在这里填写答案 */
void bubbleSort(int a[], int n) {
int tmp;
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - 1; j++) {
if (a[j] > a[j + 1]) {
tmp = a[j];
a[j] = a[j + 1];
a[j + 1] = tmp;
}
}
}
}
void outputData(int data[], int elementCount) {
for (int i = 0; i < elementCount - 1; i++) {
printf("%d ", data[i]);
}
printf("%d\n", data[elementCount - 1]);
}