Insertion sort

From Wikipedia

HomePage | Recent changes | View source | Discuss this page | Page history | Log in |

Printable version | Disclaimers | Privacy policy

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 into the correct sequence in the result, until no elements are left in the input. The choice of which element to remove from the input is arbitrary.

The implementation of the result has the greatest impact on the time taken, so an ordered tree data structure with efficient inserts and in-order traversal will give good results. Specific implementations of insertion sort using these data structures may have their own names.

Straight insertion sort =

Straight insertion sort is a very simple implementation of insertion sort. Sorting is done in-place. The result array after n iterations is the first n entries of the input array, where the first remaining entry of the input is removed each time, and the result extends into the input when this entry is inserted:

   _____ result ______ ______ input ______
  |   < x   |   > x   | x |      ...      |

becomes:

   ________ result _______ ____ input ____
  |   < x   | x |   > x   |      ...      |

The algorithm can be written as follows:

def straightinsertionsort(array):
    # Remove elements from start, extending result part of array.
    for removed_index in range(1, len(array)):
        removed_value = array[removed_index]
        # Search result part of array from top for index at which to insert.
        insert_index = removed_index
        while insert_index > 0 and array[insert_index - 1] > removed_value:
            # Move entry up to make room to insert value below.
            array[insert_index] = array[insert_index - 1]
            insert_index = insert_index - 1
        # Insert removed value at correct location in sorted result.
        array[insert_index] = removed_value

Straight insertion sort is very similar to bubble sort. In bubble sort, after N passes through the array, the N largest elements have bubbled to the top. (Or the N smallest elements have bubbled to the bottom, depending on which way you do it.) In insertion sort, after N passes through the array, you have a run of N 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 takes more time than the previous one.

In the best case of an already sorted array, this implementation of straight insertion sort takes O(n) time: 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, the inner loop is very fast, which often makes it one of the fastest algorithms around for sorting small numbers of elements, typically less than 10 or so. Quicksort works by dividing the array to be sorted into smaller runs to be sorted separately; highly optimized implementations of Quicksort often use insertion or bubble sort to sort these runs once they get small enough.

--- /Talk