@Test public void insertionSort() { Integer[] a = {5,9,3,4,2,1,6,8,10,7,65,54,85,32,15,94,75,62,34,76,45,32,85}; for(int i = 1; i<a.length; i++){ int key = a[i]; int j = i - 1;
//循环不变式 while (j >= 0 && a[j]>key){ a[j+1] = a[j]; a[j] = key; j = j-1; } } System.out.print(Arrays.toString(a)); }
插入算法的重点:循环不变式、适合少量数据