Oh My Algorithm
Algorithm Guidecomplexity: O(n²)

Insertion Sort

Compares each element against the already-sorted portion and inserts it into the right position. Remarkably fast when the data is nearly sorted.

01 Explore How It Works

Interactive Step-by-Step
Insertion Sort
45
12
89
34
67
23
56
10

삽입 정렬을 시작합니다. 첫 번째 요소(45)는 이미 정렬된 상태로 간주합니다.

Logic Node1 / 13

02 Understand It Simply

For Everyone
🔑Analogy

Like holding a hand of cards and slotting each new card into its correct place.

💡In Plain Words

Treats the front as already sorted and pushes each new value into position.

Very fast on nearly-sorted data.

📍Where It's Used
  • Small or nearly-sorted data
  • the finishing step of other sorts

03 Python Implementation

A clean, readable reference implementation of the core logic of Insertion Sort.

core_implementation.py
def insertion_sort(arr):
    for i in range(1, len(arr)):
        key = arr[i]
        j = i - 1
        while j >= 0 and arr[j] > key:
            arr[j + 1] = arr[j]
            j -= 1
        arr[j + 1] = key
    return arr

04 Frequently Asked Questions

FAQ
What is Insertion Sort?+

Compares each element against the already-sorted portion and inserts it into the right position. Remarkably fast when the data is nearly sorted.

What is the time complexity of Insertion Sort?+

The time complexity of Insertion Sort is O(n²). Follow the step-by-step visualization to see exactly why.

Where is Insertion Sort used?+

Small or nearly-sorted data, the finishing step of other sorts.

What's a simple analogy for Insertion Sort?+

Like holding a hand of cards and slotting each new card into its correct place.

Guide Progress0%