42
0
17
1
88
2
33
3
71
4
9
5
56
6
25
7
64
8
12
9
AlgoPlus//sorting / insertion sort
Read the theory

Insertion Sort · Cards in Hand

Each new element slides backward into its sorted slot.

Stability
Stable
In-Place
Yes
Space Complexity
O(1)
Avg Time
O(n²)
Size10
Legend
Comparing
Swapping / moving
Tracked (min / key)
Pivot
Sorted / found
Out of focus
AI Tutor Workspace
In a nutshell
Insertion sort builds the sorted list one item at a time: it takes the next value and slides it backward over any larger values until it sits in the right spot. It's fast on data that's already nearly sorted and can sort items as they arrive, which is why it's the go-to for small or almost-ordered lists.
Ready
Press play to begin the cinematic walkthrough.
Like sorting playing cards in your hand — each new card slides backward into its correct slot.
Key terms
Go deeper in the lesson
Read the full theory, intuition & complexity for Insertion Sort · Cards in Hand.
Code Simulator
1
def insertion_sort(arr):
2
    for i in range(1, len(arr)):
3
        key = arr[i]
4
        j = i - 1
5
        while j >= 0 and key < arr[j]:
6
            arr[j + 1] = arr[j]
7
            j -= 1
8
        arr[j + 1] = key
Why Python? · Readable first, fast second

Dynamically typed and interpreted — every comparison and swap is dispatched by the interpreter at run time, so tight loops run roughly 10–100× slower than compiled C/C++. Unbeatable for learning the idea with the least code; not what you reach for when the inner loop is the bottleneck.