原始方法
public class Main {
public static void main(String[] args) {
int[] array = {12, 11, 5, 18, 34, 21, 100, 999};
int temp = 0;
for (int i = 0; i < array.length - 1; i++) {
for (int j = 0; j < array.length - 1 - i; j++) {
if (array[j] > array[j + 1]) {
temp = array[j];
array[j] = array[j + 1];
array[j + 1] = temp;
}
}
}
//输出排序之后的数组
for (int i : array) {
System.out.print(i+"\t");
}
}
}
工具类方法:
public static void main(String[] args) {
int[] array = {12, 11, 5, 18, 34, 21, 100, 999};
Arrays.sort(array); //使用工具类,默认是升序
for (int i = array.length - 1; i >= 0; i--) {
System.out.print(array[i] + "\t");
}
}