1.
https://www.jyshare.com/compile/10/
简单的冒泡排序
public class BubbleSort {
public static void bubbleSort(int[] array) {
if (array == null || array.length == 0) {
return;
}
int n = array.length;
boolean swapped;
for (int i = 0; i < n - 1; i++) {
swapped = false;
for (int j = 0; j < n - i - 1; j++) {
if (array[j] > array[j + 1]) {
// 交换 array[j] 和 array[j + 1]
int temp = array[j];
array[j] = array[j + 1];
array[j + 1] = temp;
swapped = true;
}
}
// 如果在这一轮排序中没有交换过,说明数组已经有序,可以提前结束
if (!swapped) {
break;
}
}
}
public static void main(String[] args) {
int[] array = {64, 34, 25, 12, 22, 11, 90};
bubbleSort(array);
System.out.println("Sorted array:");
for (int value : array) {
System.out.print(value + ",");
}
}
}
测试另外一个
jie
public class NestedForLoops {
public static void main(String[] args) {
// 定义一个三维数组
int[][][] threeDimArray = {
{
{1, 2},
{3, 4}
},
{
{5, 6},
{7, 8}
},
{
{9, 10},
{11, 12}
}
};
// 三层嵌套的for循环
for (int i = 0; i < threeDimArray.length; i++) { // 第一层:数组的维度1
for (int j = 0; j < threeDimArray[i].length; j++) { // 第二层:维度2
for (int k = 0; k < threeDimArray[i][j].length; k++) { // 第三层:维度3
System.out.print(threeDimArray[i][j][k] + " ");
}
System.out.println(); // 每完成第三层循环后换行
}
}
}
}