1. #include <iostream>
    2. using namespace std;
    3. int main() {
    4. // 利用冒泡排序,实现升序的序列
    5. int arr[9] = {4, 2, 8, 0, 5, 7, 1, 3, 9};
    6. cout << "排序前的结果" << endl;
    7. for (int i = 0; i < 9; i++) {
    8. cout << arr[i] << "";
    9. }
    10. cout << endl;
    11. // 开始冒泡排序
    12. // 总共排序轮数 总元素个数 - 1
    13. for (int i = 0; i < 9 - 1; i++) {
    14. // 内侧循环 次数 = 元素个数 - 当前的轮数 - 1
    15. for (int j = 0; j < 9 - i - 1; j++) {
    16. // 如果第一个元素比第二个元素大 就交换它们
    17. if (arr[j] > arr[j + 1]) {
    18. int temp = arr[j];
    19. arr[j] = arr[j + 1];
    20. arr[j + 1] = temp;
    21. }
    22. }
    23. }
    24. cout << "排序后的结果" << endl;
    25. for (int i = 0; i < 9; i++) {
    26. cout << arr[i] << "";
    27. }
    28. cout << endl;
    29. return 0;
    30. }

    截图_选择区域_20220106220241.png