Sorting playground

Insertion Sort Visualizer

Take one value at a time and slide it left into the correct place inside an already-sorted prefix.

Live operation

sorted

Step 1 / 21
A value has reached a confirmed sorted position.
5
[0]
3
[1]
8
[2]
4
[3]
2
[4]
CompareShift / swapSorted prefix

Comparisons

0

Moves / writes

0

Confirmed sorted

1 / 5

Algorithm notes

What to watch for

Insertion Sort treats the left side of the array like a growing hand of cards. Each new value compares backward and shifts left until the prefix is ordered.

Insertion Sort Complete Info Card

Online AlgorithmAdaptive

Insertion Sort builds the final sorted array one element at a time by repeatedly taking the next element and inserting it into its correct position within the sorted portion.

Algorithm Characteristics

Time Complexity (Best)

When array is already sorted

O(n)

Time Complexity (Average)

Typical case with random data

O(n²)

Time Complexity (Worst)

When array is reverse sorted

O(n²)

Space Complexity

In-place sorting algorithm

O(1)

Stable

Maintains relative order of equal elements

Yes

Adaptive

Efficient for nearly-sorted data

Yes

Sorting Process Steps

1

Start with second element as key

2

Compare key with sorted elements to its left

3

Shift elements greater than key right

4

Insert key in correct position

5

Repeat for all elements

6

Early termination in sorted portions

Comparison with Other O(n²) Sorts

AlgorithmSwaps (Worst Case)Best Case
Bubble SortO(n²)O(n)
Selection SortO(n)O(n²)
Insertion SortO(n²)O(n)

Optimal Use Cases

  • Small datasets (n ≤ 50)
  • Nearly-sorted data (adaptive)
  • Online sorting (data arriving sequentially)

When to Avoid

  • Large random datasets
  • Performance-critical applications
  • When memory is not constrained (consider Merge Sort)
Pro Tip: Insertion Sort is often used as the base case in hybrid sorts (like Timsort) for small subarrays.
AdaptiveStableIn-place