# 29.4 Insertion Sort

## Naive Insertion Sort

In insertion sort, we start with an empty output sequence. Then, we select an item from the input, inserting into the output array at the correct location.

Naively, we can do this by creating a completely seperate output array and simply putting items from the input there. You can see a demo of this algorithm [here](http://goo.gl/bVyVCS).

<figure><img src="https://2316889115-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FCLYj7ccqvV6l4Pt9R0w5%2Fuploads%2F9VFlMN2LcKDjSI9FdOjZ%2Fimage.png?alt=media&#x26;token=bef2ccaa-7332-4ac3-ab9e-79f3d669d60f" alt=""><figcaption><p>Naive insertion sort with a seperate output array</p></figcaption></figure>

For the naive approach, if the output sequence contains $$k$$ items so far, then an insertion takes $$O(k)$$ time (shifting every item over in the output array).

## In-place Insertion Sort

We can improve the time and space complexity of insertion sort by using in-place swaps instead of building a seperate output array.&#x20;

You can see a demo of this algorithm [here](https://docs.google.com/presentation/d/10b9aRqpGJu8pUk8OpfqUIEEm8ou-zmmC7b_BE5wgNg0/edit#slide=id.g463de7561_042).

Essentially, we move from left to right in the array, selecting an item to be swapped each time. Then, we swap that item as far to the front as it can go. The front of our array gradually becomes sorted until the entire array is sorted.

<figure><img src="https://2316889115-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FCLYj7ccqvV6l4Pt9R0w5%2Fuploads%2F5r5BqnkjNwBr8g0yDqOq%2Fimage.png?alt=media&#x26;token=22afa9a3-e2b9-4938-87f5-361a5424cd6b" alt=""><figcaption><p>Insertion sort algorithm. Purple items are the selected item being swapped to the front, and black items are the ones with which the purple items are being swapped.</p></figcaption></figure>

## Insertion Sort Runtime

The best-case runtime of insertion sort is $$\Theta(N)$$--when there are no swaps, we simply do a linear scan through the array. The worst-case runtime of insertion sort is $$\Theta(N^2)$$--in a reverse-sorted array, we have to swap every item all the way to the front.&#x20;

### Insertion Sort Advantages

Note that on sorted or almost-sorted arrays, insertion sort does very little work. In fact, the number of swaps that it does is equal to the number of inversions in the array.

<figure><img src="https://2316889115-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FCLYj7ccqvV6l4Pt9R0w5%2Fuploads%2FaloSvhMKKvHo4YJdDJqz%2Fimage.png?alt=media&#x26;token=9d614fb0-6334-4587-b4f4-bc85b4a23459" alt=""><figcaption><p>The left array has only 5 inversions, and the right array has only 3. Insertion sort does very little work.</p></figcaption></figure>

In other words, on arrays with a small number of inversions, insertion sort is probably the fastest sorting algorithm. The runtime is $$\Theta(N + K)$$, where $$K$$ is the number of inversions in the array. If we define an almost-sorted array as one where the number of inversions $$K < cN$$ for some constant $$c$$, then insertion sort runs in linear time.

A less obvious empirical fact is that insertion sort is extremely fast on small arrays, usually of size 15 or less. The analysis of this is beyond the scope of the course, but the general idea is that divide-and-conquer algorithms (like heapsort and mergesort) spend too much time on the "dividing" phase, whereas insertion sort starts sorting immediately. In fact, the Java [implementation](http://grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/6-b14/java/util/Arrays.java#Arrays.mergeSort%28java.lang.Object%5B%5D%2Cjava.lang.Object%5B%5D%2Cint%2Cint%2Cint%29) of mergesort uses insertion sort when the split becomes less than 15 items.&#x20;
