public class ArrayTest {
public static void main(String[] args) {
Mytools Arr = new Mytools();
int[] a = {3,2,4,6,8,7,9,5,1};
Arr.bubble(a);
for(int i = 0 ; i < a.length ; i++){
System.out.print(a[i] + "\t");
}
}
}
class Mytools{
public void bubble(int[] arr){
//冒泡排序
int temp;
//外层循环表示排序趟数length - 1趟
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;
}
}
}
}
}
07-04
822
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
07-03
07-15
162
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
“相关推荐”对你有帮助么?
-
非常没帮助
-
没帮助
-
一般
-
有帮助
-
非常有帮助
提交