diff --git a/pset10/sort.c b/pset10/sort.c new file mode 100644 index 0000000..cb26834 --- /dev/null +++ b/pset10/sort.c @@ -0,0 +1,38 @@ +#include + +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]); +}