## Encyclopedia > Insertion sort

Article Content

# Insertion sort

Insertion sort is a simple sort algorithm where the result is built up one entry at a time.

In abstract terms, each iteration of an insertion sort removes an element from the input data, inserting it at the correct position in the already sorted list, until no elements are left in the input. The choice of which element to remove from the input is arbitrary.

Sorting is done in-place. The result array after k iterations contains the first k entries of the input array and is sorted. In each step, the first remaining entry of the input is removed, inserted into the result at the right position, thus extending the result:

```  +------ result ------+------ input ------+
|   <= x   |   > x   | x |      ...      |
+--------------------+-------------------+
```
becomes:
```  +-------- result --------+---- input ----+
|   <= x   | x |   > x   |      ...      |
+------------------------+---------------+
```
with each element > x copied to the right as it is compared against x.

The algorithm can be described as:

1. Start with the result being the first element of the input.
2. Loop over the input until it is empty, "removing" the first remaining (leftmost) element.
3. Compare the removed element against the current result, starting from the highest (rightmost) element, and working left towards the lowest element.
4. If the removed input element is lower than the current result element, copy that value into the following element to make room for the new element below, and repeat with the next lowest result element.
5. Otherwise, the new element is in the correct location; save it in the cell left by copying the last examined result up, and start again from (2) with the next input element.

A simple Python implementation of this follows:

```def straightinsertionsort(array):
for removed_index in range(1, len(array)):
removed_value = array[removed_index]
insert_index = removed_index
while insert_index > 0 and array[insert_index - 1] > removed_value:
array[insert_index] = array[insert_index - 1]
insert_index = insert_index - 1
array[insert_index] = removed_value
```

One coding using a functional programming language such as Haskell might be:

```> insert :: Ord a => a -> [a] -> [a]
> insert item []  = [item]
> insert item (h:t) | item <= h = item:h:t
>                   | otherwise = h:(insert item t)

> insertsort :: Ord a => [a] -> [a]
> insertsort []    = []
> insertsort (h:t) = insert h (insertsort t)
```
(sorry about the update - just took me ages to get this working, hope you dont mind)

Insertion sort is very similar to bubble sort. In bubble sort, after k passes through the array, the k largest elements have bubbled to the top. (Or the k smallest elements have bubbled to the bottom, depending on which way you do it.) In insertion sort, after k passes through the array, you have a run of k sorted elements at the bottom of the array. Each pass inserts another element into the sorted run. So with bubble sort, each pass takes less time than the previous one, but with insertion sort, each pass may take more time than the previous one.

In the best case of an already sorted array, this implementation of insertion sort takes O(n) time: in each iteration, the first remaining element of the input is only compared with the last element of the result. It takes O(n2) time in the average and worst cases, which makes it impractical for sorting large numbers of elements. However, insertion sort's inner loop is very fast, which often makes it one of the fastest algorithms for sorting small numbers of elements, typically less than 10 or so.

D.L. Shell made an improvement to the algorithm, called Shellsort, that compares elements separated by a distance that decreases on each pass. Quicksort works by dividing the array to be sorted into smaller runs to be sorted separately; highly optimized implementations of Quicksort often use insertion sort to sort these runs once they get small enough.

All Wikipedia text is available under the terms of the GNU Free Documentation License

Search Encyclopedia
 Search over one million articles, find something about almost anything!

Featured Article
 Shoreham, New York ... West (40.956866, -72.908727)1. According to the United States Census Bureau, the village has a total area of 1.1 km² (0.4 mi²). 1.1 km² (0.4 mi²) of it ...

This page was created in 48.8 ms