1. package com.atguigu.sort;
    2. import java.util.Arrays;
    3. /**
    4. * 冒泡排序及其推演过程
    5. *
    6. * @author Dxkstart
    7. * @create 2021-10-11-14:55
    8. */
    9. public class BubbleSort {
    10. public static void main(String[] args) {
    11. int arr[] = {3, 9, -1, 10, -2};
    12. System.out.println("原始数组:");
    13. System.out.println(Arrays.toString(arr));
    14. System.out.println();
    15. //冒泡排序 的时间复杂度O(n^2)
    16. int temp = 0;//临时变量
    17. for (int i = 0; i < arr.length - 1; i++) {
    18. for (int j = 0; j < arr.length - i - 1; j++) {
    19. if (arr[j] > arr[j + 1]) {
    20. temp = arr[j];
    21. arr[j] = arr[j + 1];
    22. arr[j + 1] = temp;
    23. }
    24. }
    25. System.out.println("第" + (i + 1) + "趟排序后的结果:");
    26. System.out.println(Arrays.toString(arr));
    27. System.out.println();
    28. }
    29. }
    30. }

    image.png