# Binary Insertion Sort

Binary Insertion Sort is a more performant Insertion Sort implementation.

You put a Binary Search in your Insertion Sort so you can Binary Search where to insert.

Given an unsorted array `arr`

, insertion sort builds up an array of sorted elements “in-place”, checking *every* previous value in the sorted section of the array to determine where to place a new element.

Envision the partially-sorted array: `[1,3,4,0]`

.

Imagine that we are placing `0`

, the last element. Insertion sort would have us compare 0 with 4–nope, still bigger!–then with 3, and then with 1.

We can use a binary search technique with this sorted part of the array, `[1,3,4]`

in order to divide-and-conquer to find the index of insertion so that we do not have to make as many comparisons.

### Tiny Modification to Binary Search Algorithm

The code is nearly identical. We rewrite our binary search algorithm slightly.

Previously, our search returned `false`

if the element was missing from the array:

```
if (L>R) { return false; }
```

Now, we return the `index`

of *where the element would exist, if it did exist*:

```
if (L>=R) { return (target > arr[L]) ? L+1 : L; }
```

### Tiny Modification to Insertion Sort Algorithm

We rewire our insertion sort to use the index returned by our new Binary Search function.

Previously, we checked every preceding value to find the index.

```
while (j>=0 && arr[j]>key) {
arr[j+1]=arr[j];
j--;
}
arr[j+1]=key;
```

Now, we use the index provided by binary search:

```
let idx=binarySearch(arr, key, 0, j)
while (j>=idx) {
arr[j+1]=arr[j];
j--;
}
arr[j+1]=key;
```

This might look like the same number of loops. While it is true that we still have to *shift each element forward* by one until we get to the index of insertion for `key`

, we do not pay for the additional comparison of `arr[j]>key`

in each while loop iteration.

This might seem trivial in a small case, but if you imagine sorting a very large array, or an array of items that are larger and more expensive to compare than numbers, it is worth the time savings.

### Let’s walk through step by step.

`arr=[1,3,4,0]`

. Where do we insert 0?

`target=0`

. `L=0`

, `R=2`

.
Midpoint is 1 (`Math.floor((0+2)/2)`

). `arr[1]===3`

.

0 is less than 3, so `L=0`

& `R=0`

.

Now `L>R`

is true. We evaluate the last statement:

`return (target > arr[L]) ? L+1 : L;`

`arr[0]`

is 1, and `0>1`

is false. We insert `0`

at index `0`

, *before 1*, instead of at index `1`

.

We return the sorted array `[0,1,3,4]`

.

#### This last line confuses me.

Me too, at first. `L>R`

is true when the item does not exist in the binary search space. However, we know that if the element *were* in the search space, it would be *adjacent* to `arr[L]`

.

**We need to know on which side of arr[L] to insert target so that the array is sorted.**

Consider if we were placing `2`

in the array `[1,3,4,2]`

.

`target=2`

`L=0`

, `R=2`

.
Midpoint is 1 (`Math.floor((0+2)/2)`

). `arr[1]===3`

.

2 is less than 3, so L is 0 and R is 0.

Now `L>R`

is true. We evaluate the last statement:

`return (target > arr[L]) ? L+1 : L;`

`arr[0]`

is 1, and `2>1`

is false. We insert `2`

at index `1`

, *after 1*, instead of at index `0`

.

We return the sorted array `[1,2,3,4]`

.

### codepen

See the Pen Binary Insertion Sort by Thomas (@thmsdnnr) on CodePen.