[AppleScript] 纯文本查看 复制代码
// 类名看心情起的,想咋起就咋起
public class Demo {
public static void main(String[] args) {
// 冒泡排序
int[] arr = {12, 21, 2, 1, 30, -10, -100, 100, 1000, 23, 11, 0};
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr.length - 1; j++) {
// 从小到大排序
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
// 遍历打印排序后的元素
for (int a : arr) {
System.out.print(a + " ");
}
}
}
[AppleScript] 纯文本查看 复制代码
public class Demo {
public static void main(String[] args) {
// 冒泡排序
int[] arr = {12, 21, 2, 1, 30, -10, -100, 100, 1000, 23, 11, 0};
for (int i = 0; i < arr.length; i++) {
boolean isSort = true; // 是否排序
for (int j = 0; j < arr.length - 1; j++) {
int temp = 0;
// 从大到小排序
if (arr[j] < arr[j + 1]) {
temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
isSort = false;
}
}
if (isSort) {
break;
}
}
System.out.println(Arrays.toString(arr));
}
}