Insertion Sort
Lesson notes
Overview
Grow a sorted prefix by inserting each new item where it belongs, like sorting playing cards. This topic is rated beginner and is number 3 of 117 in Computer Science. Core ideas and first intuitions. No prior knowledge needed.
What you are seeing
The panel above shows an algorithm working step by step on a list, with comparisons and swaps highlighted. The animation is drawn live from the model, so everything you see responds to the sliders. Press Space to pause, use Restart to replay from the start, and change the speed to slow down quick events.
In depth
Insertion sort builds a sorted prefix one item at a time: take the next item and slide it left past larger items until it sits in the correct place, like sorting a hand of playing cards. It is O(n²) in general but O(n) when the data is nearly sorted, and it works well on small lists, so real libraries use it to finish off faster algorithms. It is stable and sorts in place.
Reading the relationship
The expression "O(n²), O(n) if nearly sorted" is a compact summary of the idea. It uses the symbols O, n, i, f, e, a; name what each one stands for before you start. Notice that it contains a squared term, so doubling that quantity changes the result by a factor of four. Then use the sliders to see how changing one quantity changes the others.
A guided exploration
1. Run the algorithm on the default list and count the comparisons. 2. Change the list size and see how the work grows. 3. Compare the result with another sorting method on the same input.
Try this
Start by only moving one slider at a time. Say aloud what you expect, then check.
Common pitfalls
Test small inputs by hand and then the largest ones. Off-by-one errors and forgetting how cost grows with input size are the most common traps.
How it connects
It builds on “Selection Sort”, the topic before it in Computer Science. Once you are comfortable, continue with “Merge Sort”.
Why it matters
Computing ideas decide how fast, how safely and how well software can solve problems.
Where you might use it
- Revising “Insertion Sort” before a class, exam or certification
- Choosing the right algorithm or data structure when writing software or apps
- Debugging programs and judging how fast or how safely code will run at scale
- Understanding how search engines, games, phones and AI systems make decisions
Frequently asked questions
What is Insertion Sort?
Grow a sorted prefix by inserting each new item where it belongs, like sorting playing cards. It is a beginner topic in Computer Science.
How do I use the Insertion Sort simulation?
The panel at the top shows an algorithm working step by step on a list, with comparisons and swaps highlighted. Move the sliders one at a time, then press Space to pause the animation and look closer.
What does the formula mean?
The expression O(n²), O(n) if nearly sorted packs the idea into symbols. Name each symbol first, then change one quantity with the sliders and see which others respond.
Who is this topic for?
Core ideas and first intuitions. No prior knowledge needed. If the quiz feels hard, reread the lesson notes and try the previous topic first.
What should I learn before this?
Start with “Selection Sort”, the previous topic in Computer Science. Each topic in a subject builds on the ones before it.
How do I track my progress?
Press “Mark as learned” on this page. Progress is stored only in this browser and is never sent anywhere. The Browse page can show topics you have learned or still need to learn.