Contents    Page-10    Prev    Next    Page+10    Index   

Insertion Sort Performance

Since insertion sort has a triangle of an outer loop and an inner loop that grows to the index of the outer loop, it is O(n2). This would be unacceptable unless n is small.

However, if the input is almost sorted, insertion sort can run quickly, O(n + d), where d is the number of inversions or pairs of items that are not in correct order. The reason for this is that the hole is usually in the right place already, so the inner loop terminates quickly. Real applications often have almost-sorted input, e.g. the telephone book with a few new customers.

Insertion sort is: