package com.atguigu.sort;
import java.util.Arrays;
/**
* 冒泡排序及其推演过程
*
* @author Dxkstart
* @create 2021-10-11-14:55
*/
public class BubbleSort {
public static void main(String[] args) {
int arr[] = {3, 9, -1, 10, -2};
System.out.println("原始数组:");
System.out.println(Arrays.toString(arr));
System.out.println();
//冒泡排序 的时间复杂度O(n^2)
int temp = 0;//临时变量
for (int i = 0; i < arr.length - 1; i++) {
for (int j = 0; j < arr.length - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
System.out.println("第" + (i + 1) + "趟排序后的结果:");
System.out.println(Arrays.toString(arr));
System.out.println();
}
}
}