插入排序法

@Test
public void insertSort() {
int[] array = {7, 3, 2, 9, 15, 1, 14};
for (int i = 1; i < array.length; i++) { //i从1开始,因为第一分数已经是排好的了
for (int j = i; j > 0; j--) {
if (array[j] < array[j - 1]) {
int temp = array[j]; //交换元素
array[j] = array[j - 1];
array[j - 1] = temp;
}
}
}
for (int i = 0; i < array.length; i++) { //循环输出重新排序的结果
System.out.println(array[i]);
}
}

原文地址:https://www.cnblogs.com/f-s-q/p/7430322.html