TOPICS
Search

Insertion Sort


Insertion sort is a sorting algorithm that maintains a sorted prefix of a list. At each step, the next element is removed from the unsorted suffix and inserted into its proper position in the prefix, shifting larger elements one place to the right.

For a list of n elements, insertion sort uses O(n^2) comparisons and moves in the average and worst cases, but only O(n) comparisons on an already sorted list. In big-O notation, these bounds mean that the corresponding operation counts are bounded by constant multiples of n^2 and n, respectively, for all sufficiently large n. It is an in-place stable sorting algorithm and is effective for small or nearly sorted inputs.


See also

Bubble Sort, Sorting

Explore with Wolfram|Alpha

References

Cormen, T. H.; Leiserson, C. E.; Rivest, R. L.; and Stein, C. Introduction to Algorithms, 2nd ed. Cambridge, MA: MIT Press, 2001.Knuth, D. E. The Art of Computer Programming, Vol. 3: Sorting and Searching, 2nd ed. Reading, MA: Addison-Wesley, 1998.

Cite this as:

Weisstein, Eric W. "Insertion Sort." From MathWorld--A Wolfram Resource. https://mathworld.wolfram.com/InsertionSort.html

Subject classifications