Overview
Insertion Sort is a simple sorting algorithm that builds the final sorted array one item at a time. It is much less efficient on large lists than more advanced algorithms such as quicksort, heapsort, or merge sort. However, it has several advantages including simplicity, efficiency on small data sets, and being adaptive to already sorted data.Implementation
How It Works
- Start from Second Element: Begin at index 1 (second element), treating the first element as already sorted
- Save Current Element: Store the current element in a temporary variable
- Shift Larger Elements: Move elements greater than the current element one position to the right
- Insert Element: Place the current element in its correct position
- Repeat: Continue this process for all remaining elements
Insertion Sort works similarly to how you might sort playing cards in your hands - you pick up cards one at a time and insert each into its proper position.
Complexity Analysis
Time Complexity
- Best Case: O(n) - when array is already sorted
- Average Case: O(n²)
- Worst Case: O(n²) - when array is reverse sorted
Space Complexity
- Space: O(1) - sorts in place
- Auxiliary: O(1) - only uses a few variables
Usage Example
Visual Example
Let’s trace through sorting[12, 11, 13, 5, 6]:
Characteristics
Stability
Stability
Insertion Sort is stable - equal elements maintain their relative order.
In-Place
In-Place
Yes, Insertion Sort is in-place. It only requires O(1) additional memory.
Adaptive
Adaptive
Yes, Insertion Sort is adaptive. It runs in O(n) time when the array is already sorted or nearly sorted.
Online
Online
Yes, Insertion Sort is online. It can sort a list as it receives new elements.
When to Use
Insertion Sort is often the algorithm of choice for small datasets or nearly sorted data.
- Small datasets (typically n < 20-30)
- Nearly sorted data (performs in near-linear time)
- Online sorting (elements arrive one at a time)
- As the final step in more complex sorting algorithms (e.g., Timsort, Introsort)
- When simplicity and low overhead are important
- Working with large random datasets
- Data is in reverse order
- O(n log n) performance is required
Performance Insights
Comparison with Other Sorts
| Algorithm | Best Case | Average Case | Worst Case | Space | Stable | Adaptive | Online |
|---|---|---|---|---|---|---|---|
| Insertion Sort | O(n) | O(n²) | O(n²) | O(1) | Yes | Yes | Yes |
| Bubble Sort | O(n) | O(n²) | O(n²) | O(1) | Yes | Yes | No |
| Selection Sort | O(n²) | O(n²) | O(n²) | O(1) | No | No | No |
| Merge Sort | O(n log n) | O(n log n) | O(n log n) | O(n) | Yes | No | No |