Python Tutorial

Insertion Sort

Build a sorted list one element at a time by inserting each new value into its correct place.

How Insertion Sort Works

Insertion sort works the way you sort playing cards in your hand. Starting from the second element, take each value ("the key") and shift larger elements to its left one step right, then drop the key into the gap. The left part stays sorted at every step.

It is O(n²) in the worst case but O(n) on nearly-sorted data, stable, in-place, and fast for small inputs — which is why Timsort uses it for small runs.

Implementation

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]      # shift larger element right
            j -= 1
        arr[j + 1] = key             # drop key into place
    return arr

print(insertion_sort([12, 11, 13, 5, 6]))
# [5, 6, 11, 12, 13]

Step-by-Step

Sorting [12, 11, 13, 5, 6]:

KeyArray after insertion
11[11, 12, 13, 5, 6]
13[11, 12, 13, 5, 6]
5[5, 11, 12, 13, 6]
6[5, 6, 11, 12, 13]

Complexity

CaseTime
Best (already sorted)O(n)
AverageO(n²)
Worst (reverse sorted)O(n²)
SpaceO(1) in place

Insertion sort is stable and adaptive — its running time shrinks toward O(n) as the input becomes more sorted.

Where It Shines

  • Small arrays (roughly n < 20), where its low overhead beats fancier sorts.
  • Nearly-sorted data, or data arriving one item at a time (online sorting).
  • As the base case inside hybrid sorts like Timsort and introsort.

Python's built-in Timsort splits data into small runs and sorts each with insertion sort, then merges them — combining insertion sort's speed on small/sorted data with merge sort's O(n log n) guarantee.

Best Practices

  • Choose insertion sort for small or nearly-sorted inputs.
  • Prefer it over bubble and selection sort as the simple sort to keep in your toolkit.
  • For anything large or general-purpose, use sorted() / list.sort().

Try It Yourself

Exercise 1: On already-sorted data, what is insertion sort's time complexity?

Show solution

O(n) — each element is already in place, so the inner loop never shifts.

Exercise 2: Why is insertion sort used inside hybrid sorts like Timsort?

Show solution

It is very fast on small or nearly-sorted runs and is stable, making it ideal for sorting the small chunks that Timsort then merges.

📘 Real-World Deep Dive

Knowing <strong>DSA Insertion Sort (algorithms & data structures)</strong> well is what turns algorithms & data structures from a curiosity into a daily tool — you'll reach for it in nearly every real project.

Real-Life Scenario

An end-to-end usage of DSA Insertion Sort that you'd actually see in a data pipeline or analytics notebook.

Real-Life Example

def insertion(xs):
    for i in range(1, len(xs)):
        cur = xs[i]; j = i - 1
        while j >= 0 and xs[j] > cur:
            xs[j+1] = xs[j]; j -= 1
        xs[j+1] = cur
    return xs

print(insertion([12, 11, 13, 5, 6]))

Expected Output

(see source)

Common mistakes

  • Off-by-one errors in binary-search: the standard idiom is while lo <= hi with mid = (lo + hi) // 2.
  • Recursive algorithms blow the stack for n > ~10⁴; convert to iterative with an explicit stack.
  • Comparison algorithms (sorted(iterable)) are stable by default in Python — surprising for Java/C++ users.
  • Treating DSA Insertion Sort as a black box without reading the docs — the API has subtle defaults that bite when you scale.

🚀 Performance & Best Practices

  • Use bisect.bisect_left / bisect_right instead of writing your own binary search.
  • Convert a sorted search into a tuple-access pattern with numpy.searchsorted for huge arrays.
  • Use heapq for priority queues instead of maintaining a sorted list manually.
  • When working with algorithms & data structures, prefer vectorised / batched operations over Python loops.

🧪 Try It Yourself

  1. Reproduce the snippet on a representative slice of your own data.
  2. Profile the snippet with cProfile or timeit and find the single biggest improvement.
  3. Generalise the snippet into a small, reusable function you can drop into future projects.

FAQ: Insertion Sort

Common questions about this page.

What is Insertion Sort?

Insertion Sort is a DSA lesson that explains insertion sort in Python. Build a sorted list one element at a time by inserting each new value into its correct place. Copy the samples and run them in the Python editor. It is written for beginners who want a clear definition and working examples.

Should I run insertion sort examples locally for better learning?

Yes. Use the browser editor on StudyGrid for a quick check, then Download the example and run it on your computer. Local runs show real errors and the real toolchain, which is one of the fastest ways to learn insertion sort in this DSA Python lesson (Insertion Sort).

How do I use insertion sort in Python?

To use insertion sort in Python, follow the examples on this StudyGrid page. Copy a snippet, run it in the browser, then Download and run it locally for better learning. Change the values and compare the output.

What is the syntax of insertion sort?

This Insertion Sort tutorial shows insertion sort syntax with short Python examples. Use the code blocks in this lesson for the exact statements, then try them in your editor.

Insertion Sort example for beginners

Yes. This page includes a beginner insertion sort example you can copy and run. It is designed for searches such as "insertion sort for beginners", "insertion sort example", and "how to use insertion sort".

What are common mistakes with insertion sort?

Common insertion sort mistakes include wrong syntax, mixing types, and skipping practice. Work through this DSA chapter in order, run every example, and check the output before moving on.

Why should I learn insertion sort?

Insertion Sort is used in real Python work. Learning insertion sort helps you write clearer programs and continue the DSA tutorial on StudyGrid.

Is Insertion Sort free to learn online?

Yes. You can learn insertion sort free on StudyGrid (studygrid.in). This chapter is part of the DSA path and includes examples, syntax, and next-step links.