1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
|
public class InsertSort implements Sort { public static void main(String[] args) { InsertSort bubbleSort = new InsertSort(); System.out.println("InsertSort"); bubbleSort.test();
}
@Override public void sort(int[] arr) { for (int i = 1; i < arr.length; i++) { print(arr); int insertVal = arr[i]; int index = i - 1; while (index >= 0 && insertVal < arr[index]) { arr[index + 1] = arr[index]; index--; } arr[index + 1] = insertVal; print(arr); } }
}
|