import java.util.Arrays;
public class Main {
public static void Bubble(int[] arr){
for (int i = 0; i < arr.length - 1; i++) {
//设置flg变量,检测数组是否在某次排序后已变成顺序,简化程序
boolean flg = false;
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;
flg = true;
}
}
if(flg == false){
break;
}
}
}
public static void main(String[] args) {
int[] arr = {5,3,7,9,6,3,4,1,2};
Bubble(arr);
System.out.println(Arrays.toString(arr));
}
}
05-26
03-23
04-11
821
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
03-20
297
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交