InsertSort { public static void mysort(Comparable[] a) { int sortedSize = 1; int length = a.length; for (int i = 1; i < length; i++) { Comparable insertObject = a[i]; int insertIndex = sortedSize; for (int j = 0; j < sortedSize; j++) { if (Utils.less(insertObject, a[j])) { insertIndex = j; break; } } for (int m = i - 1; m >= insertIndex; m--) { a[m + 1] = a[m]; } a[insertIndex] = insertObject; sortedSize++; } }  static void mysort(Comparable[] a); static void sort(Comparable[] a);  }
@Test public void mysort() { Integer[] sortIntegerArray = { 1, 2, 5, 4, 3 }; Integer[] resultArray = { 1, 2, 3, 4, 5 }; InsertSort.mysort(sortIntegerArray); Assert.assertArrayEquals(sortIntegerArray, resultArray); }
@Test public void mysort2() { Integer[] sortIntegerArray = { 7, 6, 5, 4, 3 }; Integer[] resultArray = { 3, 4, 5, 6, 7 }; InsertSort.mysort(sortIntegerArray); Assert.assertArrayEquals(sortIntegerArray, resultArray); }