📚 College Credit Guide ✓ UPI Study 🕐 12 min read

What Is Insertion Sort and How Does It Work?

This article explains insertion sort step by step, shows why it works well on nearly sorted arrays, and breaks down its time complexity and real uses.

US
UPI Study Team Member
📅 August 07, 2026
📖 12 min read
US
About the Author
The UPI Study team works directly with students on credit transfer, degree planning, and course selection. We've helped thousands of students figure out what counts toward their degree and how to finish faster without paying more than they have to. This post is written the way we'd explain it to you directly.
🦉

Insertion sort builds a sorted section one item at a time. You take the next element, slide it left until it lands in the right spot, and keep going until the array is done. That simple move is why students often get it wrong at first: they picture a lot of big swaps, when the real action is usually shifting items one position at a time. This matters because insertion sort is one of the first sorting methods students meet in a data structure and algorithms course, and it teaches a clean idea: keep the left side sorted, then place the next value where it belongs. The algorithm works well on small arrays and on data that already sits close to sorted order. It struggles when the list starts out in reverse, where each new value has to cross many positions. If you are asking what is insertion sort and how does it work, the short answer is that it compares a new value against the sorted part, moves larger values right, and inserts the new one into the hole it opens. That pattern shows up in simple code, in exam questions, and in real programs that need a stable, easy-to-read sort. Once you see the sorted prefix grow from 1 item to 2, then 3, then 4, the whole thing clicks fast.

Data Structures and Algorithms
College credit · ACE & NCCRS reviewed · self-paced
View course
Vibrant and engaging code displayed on a computer screen, showcasing programming concepts — UPI Study

What Is Insertion Sort in Arrays?

Insertion sort is a simple array sort that grows a sorted prefix of size 1, then 2, then 3, by taking the next item and placing it where it belongs. In a 6-element array like [2, 5, 9, 1, 4, 6], the left side stays ordered while the next value gets tested against that ordered block.

The catch: The algorithm does not keep swapping the whole array over and over; it usually shifts larger items right by 1 position and drops the new value into the open slot. That detail matters in practice and in a data structure and algorithms course, because the code looks closer to “move, move, insert” than “swap, swap, swap.”

Students also miss the fact that insertion sort is stable in its standard form, so equal values keep their original order. That makes it a good fit for records where tie order matters, like a list of 3 exam scores or 20 names with the same grade. I like this algorithm because it is plain and honest; it does one job without fancy tricks.

The common misconception is that insertion sort must scan the whole array again after every step. It does not. It only scans left through the sorted prefix, which starts at length 1 and grows by 1 on each pass. On a nearly sorted 10-item list, that inner scan may stop after 1 or 2 checks, which is why the method feels so quick on friendly input.

How Does Insertion Sort Work Step by Step?

Insertion sort starts at the second element because the first element already counts as a sorted list of size 1. From there, each pass lifts out one key value, walks left through the sorted prefix, and leaves the key in the first open spot.

  1. Start with the second element. In [5, 2, 4, 6, 1, 3], the first pass looks at 2 while 5 already forms a sorted prefix of length 1.
  2. Store the key and compare leftward. If the key is 2, compare it with 5 and move 5 right by 1 spot.
  3. Shift larger elements right. On the second pass, 4 moves past 5, but it stops before 2 because 2 is smaller and needs no move.
  4. Insert the key in the open slot. After the first 3 passes, the array becomes [2, 4, 5, 6, 1, 3], and the sorted prefix has grown to 4 items.
  5. Repeat for each next value until the array ends. With 1 and 3 still left, the algorithm keeps doing the same leftward scan, and a short array like this can finish in under 10 simple comparisons after the early passes.

What this means: The whole routine is boring in the best way: one key, one left scan, one insert. A 6-element example shows the pattern clearly, but the same steps work on 600 values too.

A small downside shows up right away. If the key belongs near the front, the algorithm may shift 4 or 5 items just to place 1 value, and that cost adds up fast on larger arrays.

Data Structures Algorithms UPI Study Course

Learn Data Structures Algorithms Online for College Credit

This is one topic inside the full Data Structures Algorithms course on UPI Study — a self-paced, online class that earns real college credit. Credits are ACE and NCCRS evaluated and transfer to partner colleges across the US and Canada. Courses start at $250 with no deadlines and lifetime access.

See Data Structures Course →

Why Is Insertion Sort Fast on Nearly Sorted Data?

Insertion sort runs fast on nearly sorted data because most keys stop after 1 or 2 comparisons, so the inner loop stays short. On a list that is already 95% sorted, the algorithm often moves only a few elements, which keeps the work close to linear instead of quadratic.

That is the sweet spot. If you have 50 items and only 2 are out of place, insertion sort barely has to do anything dramatic, and the number of shifts stays low. I prefer it over flashier sorts in this case because the code stays tiny and the runtime feels almost lazy. The downside is just as clear: the method depends on the data shape, not just the data size.

Reality check: Reverse-sorted input is the ugly case. On [6, 5, 4, 3, 2, 1], every new key has to cross almost the full sorted prefix, so the array forces the inner loop to run near its worst possible length on each pass.

That means 1 pass can move 5 elements, the next can move 4, then 3, then 2, then 1. For a 1,000-item reverse-sorted list, those repeated shifts pile up fast, and insertion sort loses its charm. Still, for small or almost sorted inputs, it remains a clean, predictable choice that beats more complex methods on setup overhead alone.

What Time Complexity Should Students Know?

For a data structure and algorithms course, students should memorize 3 facts: insertion sort runs in O(n) best case, O(n^2) average and worst case, and O(1) extra space. The outer loop makes 1 pass per element, but the inner shifting can stretch across most of the sorted prefix, which turns many passes into a square-shaped cost on inputs like 100 reversed numbers.

Bottom line: The linear outer pass does not save you from the nested shifting work, and that is the part students forget on exams.

A 20-item list can still feel fine in practice, but a 20,000-item unsorted list will make insertion sort crawl. That gap between 20 and 20,000 is why complexity matters.

Which Use Cases Make Insertion Sort Useful?

Insertion sort earns its keep on small arrays and almost sorted lists, especially when you only need a simple, stable method with 1 pass at a time. It also fits cases where you add items one by one and want the left side kept in order without re-sorting everything.

Worth knowing: Many programmers keep insertion sort in their toolbox even after they learn merge sort and quicksort, because the simple version is easy to trust and easy to debug.

Data Structures and Algorithms also covers sorting in a way that maps cleanly to code, not just theory.

Frequently Asked Questions about Insertion Sort

Final Thoughts on Insertion Sort

Insertion sort looks simple because it is simple, but that is not the same thing as being weak. It teaches a very clean pattern: keep a sorted prefix, pull the next value, and slide it left until it fits. That idea shows up again and again in sorting, searching, and even in the way people think through code. Students usually trip over one thing: they think the algorithm does a full reshuffle every time. It does not. It shifts a few values, drops the key into place, and moves on. That detail explains why the method feels fast on a 15-item nearly sorted list and slow on a reverse-sorted one. The complexity story matters just as much as the steps. Best case O(n). Average and worst case O(n^2). Extra space O(1). Those numbers tell you when to use the algorithm and when to leave it alone. Keep insertion sort in your head as a baseline. It gives you a fair starting point for comparing faster sorts, and it helps you read exam questions without freezing on the first pass. If you want, code it once on a small array, then test it on a nearly sorted list and a reversed one. That 5-minute trial makes the pattern stick.

How UPI Study credits actually work

Ready to Earn College Credit?

ACE & NCCRS approved · Self-paced · Transfer to colleges · $250/course or $99/month

More on Data Structures Algorithms
© UPI Study. This article and its educational content are solely owned by UPI Study and licensed under CC BY-NC-ND 4.0. It is not free to reuse or modify. Any citation must credit UPI Study with a direct link to this page.