点击查看代码
public static void main(String[] args) {
int[] arr = {9, 4, 3, 7, 8, 2};
int temp;
// 从小到大排序, 每次把最小的移到最前
for (int i = 0; i < arr.length; i++) {
for (int j = i + 1; j < arr.length; j++) {
if (arr[i] > arr[j]) {
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
System.err.println(i + "--" + j + "===" + Arrays.toString(arr));
}
}
}
System.err.println(Arrays.toString(arr));
// 从小到大排序, 每次把最大的移到最后
int[] array = {9, 4, 3, 7, 8, 2};
for (int i = 1; i < array.length; i++) {
for (int j = 0; j < array.length - i; j++) {
if (array[j] > array[j + 1]) {
temp = array[j];
array[j] = array[j + 1];
array[j + 1] = temp;
System.err.println(i + "--" + j + "===" + Arrays.toString(array));
}
}
}
System.err.println(Arrays.toString(array));
}