原文: https://beginnersbook.com/2015/02/c-program-to-arrange-numbers-in-ascending-order/

    以下程序提示用户输入n个数字,一旦用户输入这些数字,该程序将按升序排序并显示它们。这里我们为排序目的创建了一个用户定义的函数sort_numbers_ascending()

    1. /*
    2. * C program to accept numbers as an input from user
    3. * and to sort them in ascending order.
    4. */
    5. #include <stdio.h>
    6. void sort_numbers_ascending(int number[], int count)
    7. {
    8. int temp, i, j, k;
    9. for (j = 0; j < count; ++j)
    10. {
    11. for (k = j + 1; k < count; ++k)
    12. {
    13. if (number[j] > number[k])
    14. {
    15. temp = number[j];
    16. number[j] = number[k];
    17. number[k] = temp;
    18. }
    19. }
    20. }
    21. printf("Numbers in ascending order:\n");
    22. for (i = 0; i < count; ++i)
    23. printf("%d\n", number[i]);
    24. }
    25. void main()
    26. {
    27. int i, count, number[20];
    28. printf("How many numbers you are gonna enter:");
    29. scanf("%d", &count);
    30. printf("\nEnter the numbers one by one:");
    31. for (i = 0; i < count; ++i)
    32. scanf("%d", &number[i]);
    33. sort_numbers_ascending(number, count);
    34. }

    输出:

    C 程序:按升序排列数字 - 图1