public class Arr {
public static void main(String[] args) {
Random rand = new Random();
int[] num = new int[10];//定义一个长度为10的整形数组
//初始化数组
for (int i = 0; i < num.length; i++) {
num[i] = rand.nextInt(1, 101);
}
System.out.println("初始化后的数组" + Arrays.toString(num));
//冒泡排序
for (int i = 0; i < num.length; i++) {
for (int j = 0; j < num.length - 1 - i; j++) {
//升序(>) 降序(<)
if (num[j] > num[j + 1]) {
int t = num[j];
num[j] = num[j + 1];
num[j + 1] = t;
}
}
}
System.out.println("升序数组" + Arrays.toString(num));
}
}
Java 冒泡排序的简单实现
最新推荐文章于 2024-08-24 22:01:16 发布