public class Demo04 {
public static void main(String[] args) {
// 冒泡排序
int[] arr = {9, 2, 4, 7, 1};
// 外层循环控制比较的次数,n个数比较n-1次
for (int i = 0; i < arr.length - 1; i++) {
// 每次比较从头开始,次数逐步递减
for (int j = 0; j < arr.length - 1 - i; j++) {
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
System.out.println(Arrays.toString(arr));
}
}
06-28
294
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
11-11
2098
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
02-15
123
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)