1 public class Main {
2 // 插入排序,从大到小
3 //插入排序类似于摸扑克牌时,将摸到的牌插入到已经排好序的手牌中
4 public static void main(String[] args) {
5 int[] a = { 3, 5, 2, 4, 7, 8, 1, 6
};
6 for (
int i = 0; i < a.length; i++) {
//摸到的牌(下一个要插入的数据)
7 for (
int j = i; j > 0; j--) {
//将摸到的牌与之前的一个数比较,若不符合顺序则换位,否则结束
8 if (a[j] > a[j - 1
]) {
9 int temp =
a[j];
10 a[j] = a[j - 1
];
11 a[j - 1] =
temp;
12 }
13 }
14 }
15 for (
int i = 0; i < a.length; i++
)
16 System.out.println(a[i]);
17 }
18 }
转载于:https://www.cnblogs.com/superxinyu/p/8533689.html