A topic in the Open Knowledge Graph — a free, open map of 15,290 topics and the order to learn them in.

Insertion Sort Algorithm

College Depth 84 in the knowledge graph ☐ I know this ☆ Set as goal
6topics build on this
401prerequisites beneath it
See this on the map →
Arrays and ListsAlgorithm Design Basics+1 more→→Merge Sort
sorting insertion-sort comparison-sort in-place stable

Core Idea

Insertion sort builds a sorted array by inserting each element into its correct position among the already-sorted prefix. It scans backward to find the position and shifts elements. The algorithm is O(n²) worst-case, O(n) best-case, O(1) space, stable, and efficient for small or nearly-sorted arrays due to low constant factors.

How It's Best Learned

Trace insertion sort by hand on small arrays. Implement and test on sorted, reverse-sorted, and random data. Measure performance and compare to other O(n²) sorts. Understand why it's stable and efficient on small n.

Common Misconceptions

Explainer

Think about how you organize a hand of playing cards. You pick up one card at a time and slide it into the right spot among the cards you're already holding. You never rearrange the whole hand from scratch — you just find where the new card belongs and insert it. That is exactly how insertion sort works. The algorithm maintains a sorted region at the front of the array and grows it one element at a time by taking the next unsorted element and placing it in its correct position within that sorted prefix.

Concretely, insertion sort iterates from the second element to the last. For each element (call it the key), it compares the key against elements in the sorted prefix, moving from right to left. Every element larger than the key gets shifted one position to the right, opening up the correct slot. The key is then placed into that slot. Because elements are shifted rather than swapped, insertion sort performs fewer writes than selection sort or bubble sort, which contributes to its low constant factors. Since elements with equal values are never reordered relative to each other, the sort is stable — a property that matters when sorting by multiple criteria in sequence.

The worst case occurs when the array is in reverse order: every new key must travel all the way to position zero, yielding roughly n²/2 comparisons and shifts — O(n²). The best case occurs when the array is already sorted: each key is compared once against its left neighbor, finds it's already in place, and moves on, giving O(n) total work. This best-case behavior makes insertion sort uniquely well-suited for nearly sorted data, where only a few elements are out of place. It also explains why many real-world sorting libraries (including Python's Timsort and Java's dual-pivot quicksort) switch to insertion sort for small subarrays — typically when n drops below 16 to 64 elements. At that scale, the low overhead of insertion sort beats the higher constant factors of more sophisticated O(n log n) algorithms.

One way to build intuition for the quadratic worst case is to count the number of inversions in the array — pairs (i, j) where i < j but a[i] > a[j]. Each comparison-and-shift in insertion sort fixes exactly one inversion. A reverse-sorted array of n elements has n(n−1)/2 inversions, so insertion sort does O(n²) work. A nearly sorted array has few inversions, so insertion sort runs close to O(n). This inversion-counting perspective connects insertion sort to the broader study of sorting lower bounds: any comparison-based sort that eliminates one inversion per comparison cannot beat O(n²) on worst-case inputs without a fundamentally different strategy like divide-and-conquer.

Practice Questions 5 questions

Prerequisite Chain

Understanding Zero → The Number Zero → Counting to Five → Counting to 10 → Counting to 20 → Counting a Set of Objects Up to 20 → Cardinality: The Last Number Counted → Matching Numerals to Quantities → Subitizing Small Quantities → Addition Within 10 → Number Bonds to 10 → Addition Within 20 → Doubles and Near Doubles → Doubles Facts Within 10 → Near Doubles Facts Within 20 → Mental Math Strategies for Addition → Mental Math: Adding and Subtracting Tens → Addition Within 100 → Repeated Addition as Multiplication → Multiplication as Equal Groups → Multiplication: Arrays → Basic Multiplication Facts (0s, 1s, 2s, 5s, 10s) → Multiplication Facts Within 100 → Division as Equal Sharing → Division as Grouping (Measurement Division) → Division: Grouping (Repeated Subtraction) Model → Division: Fair Sharing Model → Division as Equal Sharing → Division as Grouping → Basic Division Facts → Division Facts Within 100 → Multiplication and Division Fact Families → Relationship Between Multiplication and Division → Division Facts as Inverse of Multiplication → Remainders and Quotients in Division → Division Word Problems → Multi-Step Word Problems → Solving Multi-Step Word Problems → Multiplication Word Problems → Division Word Problems → Introduction to Long Division → Factors and Multiples → Prime and Composite Numbers → Equivalent Fractions → Relating Fractions and Decimals → Decimal Place Value → Integers and the Number Line → Comparing and Ordering Integers → Absolute Value → Adding Integers → Subtracting Integers → Multiplying Integers → Introduction to Exponents → Order of Operations → Integer Order of Operations → Variable Expressions → The Distributive Property → Variables and Expressions Review → Introduction to Polynomials → Adding and Subtracting Polynomials → Multiplying Polynomials → Factorial → Permutations → Combinations → Counting Principles: Addition and Multiplication Rules → Introduction to Graph Theory → Propositional Logic Foundations → Logical Equivalences → Boolean Algebra → Boolean Type and Truth Values → Comparison Operators and Boolean Tests → Logical Operators and Boolean Algebra → Conditional Statements → Defining and Calling Functions → Functions: Decomposing Problems → Function Parameters and Argument Passing → Return Values → Variable Scope → Introduction to Classes → Objects and Instances → Methods and Attributes → Algorithm Design Basics → Counting Sort Algorithm → Radix Sort Algorithm → Insertion Sort Algorithm

Longest path: 85 steps · 401 total prerequisite topics

Prerequisites (3)

Leads To (1)