Insertion Sort
- In Turkish
- eklemeli sıralama
- Pronunciation
- in-SUR-shun SORT
In short
Insertion sort builds a sorted list one element at a time, putting each new one in its place among those already sorted, like sorting cards in your hand.
What is insertion sort?
The algorithm keeps the left part of the list sorted. It takes the next element, compares it with the sorted elements from right to left, shifts the larger ones one position to the right and drops the new element into the gap. Repeating this for every element produces a fully sorted list.
In the worst case, a list sorted in reverse, every element moves all the way left, giving O(n²) time. But when the input is already nearly sorted, each element moves only a little, and the running time approaches O(n). It sorts in place with O(1) extra memory, is stable, and can sort data as it arrives, one item at a time.
That efficiency on small and nearly sorted inputs makes insertion sort a building block of fast real-world sorts. Timsort, Python's sorting algorithm since 2002 and used by Java for sorting objects, uses insertion sort on short runs, and introsort implementations, used in many C++ standard libraries, switch to it for small partitions.
A common misconception is that every O(n²) sort is equally useless. Insertion sort has low overhead and excellent behavior on nearly sorted data, which is why it outperforms O(n log n) algorithms on small arrays of a few dozen elements and survives inside the best general-purpose sorts.
Key takeaways
- Insertion sort inserts each element into place within a sorted prefix.
- It is O(n²) in the worst case but close to O(n) on nearly sorted data.
- It is in-place, stable and can sort items as they arrive.
- Timsort and introsort use it for small pieces.
- On small arrays it often beats O(n log n) algorithms.
Example
def insertion_sort(items):
items = list(items)
for i in range(1, len(items)):
current = items[i]
j = i - 1
while j >= 0 and items[j] > current: # shift larger elements right
items[j + 1] = items[j]
j -= 1
items[j + 1] = current # drop the element into the gap
return items
print(insertion_sort([12, 11, 13, 5, 6])) # [5, 6, 11, 12, 13]Readers ask
When is insertion sort a good choice?
For small arrays, data that is already almost sorted, or items arriving one by one that must be kept in order. For large, random data, an O(n log n) algorithm is far faster.
Is insertion sort stable?
Yes. It only shifts elements that are strictly greater than the one being inserted, so equal elements keep their original relative order.
Why do fast sorting algorithms use insertion sort internally?
Because for small subarrays its simple loop and low overhead beat the bookkeeping of divide-and-conquer algorithms. Hybrid sorts like Timsort and introsort switch to it below a size threshold.
See also
- Sorting AlgorithmData Structures, p. 30A sorting algorithm is a step-by-step method for arranging items in a defined order, such as numbers from smallest to largest or names alphabetically.
- Bubble SortData Structures, p. 9Bubble sort is a simple sorting algorithm that keeps swapping out-of-order neighbors, so each pass carries the largest remaining value to the end.
- Merge SortData Structures, p. 24Merge sort is a divide and conquer sorting algorithm that splits a list in half, sorts each half recursively, and merges the sorted halves in O(n log n) time.
- QuicksortData Structures, p. 27Quicksort is a divide and conquer sorting algorithm that partitions items around a chosen pivot, then sorts the smaller and larger groups the same way.
- Big O NotationProgramming Fundamentals, p. 6Big O notation describes how an algorithm's running time or memory use grows as its input gets larger, focusing on the growth rate rather than exact speed.
- ArrayProgramming Fundamentals, p. 3An array is an ordered collection of values stored under one name, where each item is accessed by its numeric position, called an index, usually starting at 0.
Spotted a mistake or something missing on this page?Suggest an edit