diff --git a/src/IntSorting.java b/src/IntSorting.java index 2532869..22b06db 100644 --- a/src/IntSorting.java +++ b/src/IntSorting.java @@ -108,7 +108,32 @@ public class IntSorting { * array to be sorted */ public static void binaryInsertionSort(int[] a) { - // TODO!!! Your method here! + for (int i = 1; i < a.length; i++) { + int low = 0; + int high = i; + int middle = i / 2; + + // Locate insertion point + while (low < high) { + if (a[i] > a[middle]) { + low = middle + 1; + } else if (a[i] < a[middle]) { + high = middle; + } else { + break; + } + + middle = low + ((high - low) / 2); + }; + + //Move array and insert value + if (middle < i) { + int tmp = a[i]; + for (int j = i - 1; j >= middle; j--) + a[j + 1] = a[j]; + a[middle] = tmp; + } + } } /**