public class ArrayTest {
public static void main(String[] args) {
int[] arrays = {10,90,20,40,50,65,25,77,58,69};
//定义中间变量
int temp = 0;
//外层循环控制排序次数,没循环一次少一次
for (int i = 0; i <= arrays.length-1; i++) {
//内循环编写业务代码
for (int j = 0; j < arrays.length - 1 - i; j++) {
if (arrays[j] < arrays[j + 1]) {
//排序的业务代码,进行变量交换,数组变量交换下标
temp = arrays[j];
arrays[j] = arrays[j + 1];
arrays[j + 1] = temp;
}
}
}
System.out.println(Arrays.toString(arrays));
}
}
java面试题--冒泡排序
最新推荐文章于 2024-03-14 15:21:08 发布