Insertion Sorting

With insertion sorting, we add the items to a list, keeping the list in order.
Each pass, choose an arbitrary unsorted element and add it to the correct place in the sorted list.

Insertion sort has a couple of advantages over selection sort;

Unfortunately, inserting an element into the middle of a sorted list usually takes O(N) time,
because you need to move half the elements over 1 place.
So it takes fewer compares, but more moves.
Inserting into a linked list is easier,
but finding the correct place to insert an item into a linked list is harder, typically O(N)

Pass 1 Pass 2 Pass 3 Pass 4 5 3 1 6 4 2 Start Pass 5 Pass 6 Done 5 3 1 6 4 2 5 3 1 6 4 2 5 3 1 6 4 2 6 4 2 4 2 5 3 1 3 1 6 5 4 3 1 6 5 2

There are many tweaks that can improve performance of insertion sorting.
But fundamentally insertion sorting is an O(N2) sort.
This is because either finding the place to insert an element is O(N),
or making room for the element is O(N), and you must do one or the other N times.


^ UP ^    < Back <    > Next >