冒泡排序:
从小到大排序:
package com.etime.test019;
import java.util.Arrays;
public class Test13 {
public static void main(String[] args) {
int[] array = { 6, 1, 2, 3, 8, 5, 4, 9, 7 };
int len = array.length;
System.out.println("排序前的数组为:"+Arrays.toString(array));
int temp = 0;
for (int i = 0; i < len - 1; i++) {
for (int j = 0; j < len - 1 - i; j++) {
if (array[j] > array[j + 1]) {
temp = array[j + 1];
array[j + 1] = array[j];
array[j] = temp;
}
}
}
System.out.println("排序后的数组为:"+Arrays.toString(array));
}
}
从大到小排序:
package com.etime.test019;
import java.util.Arrays;
public class Test12 {
public static void main(String[] args) {
int[] array = { 2, 1, 6, 3, 9, 5, 8, 4, 7 };
int len = array.length;
System.out.println("排序前的数组为:"+Arrays.toString(array));
int temp = 0;
for (int i = 0; i < len - 1; i++) {
for (int j = 0; j < len - 1 - i; j++) {
if (array[j] < array[j + 1]) {
temp = array[j + 1];
array[j + 1] = array[j];
array[j] = temp;
}
}
}
System.out.print("排序后的数组为:"+Arrays.toString(array));
}
}
从小到大排序效果图:
从大到小排序效果图: