⒈ 从第一个元素开始,该元素可以认为已经被排序
⒉ 取出下一个元素,在已经排序的元素序列中从后向前扫描
⒊ 如果该元素(已排序)大于新元素,将该元素移到下一位置
⒋ 重复步骤3,直到找到已排序的元素小于或者等于新元素的位置
⒌ 将新元素插入到下一位置中
⒍ 重复步骤2~5
public class Test { public static void main(String[] args) { System.out.println("插入排序"); int[] numbers = new int[5]; numbers[0] = 5; numbers[1] = 10; numbers[2] = 1; numbers[3] = 8; numbers[4] = 2; sort(numbers); for(int i : numbers) { System.out.print(i + " "); } } public static void sort(int[] numbers) { for (int i = 1; i < numbers.length; i++) { int temp = numbers[i]; for (int j = i - 1; j >= 0 && temp < numbers[j]; j--) { numbers[j + 1] = numbers[j]; numbers[j] = temp; } } } }