Java冒泡排序
程序员文章站
2022-06-05 12:47:40
...
冒泡排序
import java.util.Arrays
public class BSort{
public static void main(String[] args){
int[] arrays = {5,1,3,8,6,4,7,9,10,2};
for(int i = 0; i<arrays.length; i++) {//外层循环开始
for(int j = 0;j < arrays.length-i-1; j++) {//内层循环第j位始终和j+1位比较大小
//判断第j位与第j+1位的大小并将大的数字换到前面
if(arrays[j] < arrays[j+1]) {
int temp = arrays[j];
arrays[j] = arrays[j+1];
arrays[j+1] = temp;
}
System.out.println("第"+i+"趟排序第"+j+"次结果:"+Arrays.toString(arrays));
}
System.out.println("第"+i+"趟排序结束:"+Arrays.toString(arrays));
}
System.out.println(Arrays.toString(arrays));
}
}
运行结果: