@Test public void testInsertionSort() { StringBuilder strBuilder; { int[] unSort = {3, 2, 1}; int[] sort = insertionSort.insertionSort(unSort); strBuilder = new StringBuilder(); for (int i : sort) strBuilder.append(i); assertEquals("123", strBuilder.toString()); } { int[] unSort = {1, 1, 1}; int[] sort = insertionSort.insertionSort(unSort); strBuilder = new StringBuilder(); for (int i : sort) strBuilder.append(i); assertEquals("111", strBuilder.toString()); } { int[] unSort = {1, 2, 3}; int[] sort = insertionSort.insertionSort(unSort); strBuilder = new StringBuilder(); for (int i : sort) strBuilder.append(i); assertEquals("123", strBuilder.toString()); } }
public int[] insertionSort(int[] unSort) { for (int j = 1; j < unSort.length; j++) { int key = unSort[j]; int i = j - 1; while (i >= 0 && unSort[i] > key) { unSort[i + 1] = unSort[i]; i--; } unSort[i + 1] = key; } return unSort; }
InsertionSort { public int[] insertionSort(int[] unSort) { for (int j = 1; j < unSort.length; j++) { int key = unSort[j]; int i = j - 1; while (i >= 0 && unSort[i] > key) { unSort[i + 1] = unSort[i]; i--; } unSort[i + 1] = key; } return unSort; } }
InsertionSort { public int[] insertionSort(int[] unSort) { for (int j = 1; j < unSort.length; j++) { int key = unSort[j]; int i = j - 1; while (i >= 0 && unSort[i] > key) { unSort[i + 1] = unSort[i]; i--; } unSort[i + 1] = key; } return unSort; }  }
InsertionSort { public int[] insertionSort(int[] unSort) { for (int j = 1; j < unSort.length; j++) { int key = unSort[j]; int i = j - 1; while (i >= 0 && unSort[i] > key) { unSort[i + 1] = unSort[i]; i--; } unSort[i + 1] = key; } return unSort; }  int[] insertionSort(int[] unSort); }
InsertionSort { public int[] insertionSort(int[] unSort) { for (int j = 1; j < unSort.length; j++) { int key = unSort[j]; int i = j - 1; while (i >= 0 && unSort[i] > key) { unSort[i + 1] = unSort[i]; i--; } unSort[i + 1] = key; } return unSort; }  int[] insertionSort(int[] unSort);  }