Insertion Sort
Insertion Sort is one of the oldest sorting methods known — it is essentially what most people do instinctively when sorting a hand of playing cards. The algorithm has been used in computing since the earliest days of digital computers and is described in some of the first textbooks on programming from the 1950s. Despite being decades old, it remains one of the fastest algorithms available for small arrays or data that is already close to sorted order. Many modern, highly optimised sort routines switch to Insertion Sort automatically when the remaining unsorted portion drops below about 10–20 elements.
How it works
Start with the second element in the list. Pick it up. Compare it with the element immediately to its left. If the left element is bigger, slide that left element one step to the right to open a gap. Keep sliding elements rightward, one at a time, until you reach a position where the element you picked up is bigger than the element to its left (or you have reached the beginning of the list). Drop it into that gap. Now the first two elements are sorted.
Next, pick up the third element and repeat — this time, the sorted region on the left has two elements, and you may need to slide up to two elements to the right to find the correct insertion point. Keep going until every element has been picked up and inserted into its correct position within the growing sorted region on the left.
🃏 Real-world analogy: This is exactly how most people sort a hand of playing cards. You pick up each new card from the table, compare it to the cards you already hold (which are in order), and slide it into the right spot. You have been doing Insertion Sort your whole life without knowing it had a name.
Why already-sorted data is so fast
If the list is already sorted, every element you pick up is already bigger than everything to its left. You never need to slide a single element to the right — you just confirm "yep, this one's already in the right place" and move on immediately. That means one single left-to-right pass through the list is enough: O(n). Insertion Sort is called adaptive because it automatically takes advantage of existing order in the data.
🧠 Key insight: Insertion Sort is the algorithm that Shell Sort is built on top of. Understanding every detail of how Insertion Sort works — especially why nearly-sorted data is so cheap — is the key to understanding why Shell Sort's gap strategy is so clever. Think of Shell Sort as "Insertion Sort with a warm-up phase."
Complexity:
Best O(n)
Average O(n²)
Worst O(n²)
Writes: O(n²) shifts (but only one logical "move" per element)
Selection Sort
Selection Sort is another algorithm with ancient roots — the idea of "find the smallest item, move it to the front, then repeat on the rest" is intuitive enough that it was used long before computers existed. It appeared in the earliest computing literature alongside Bubble and Insertion Sort and is often taught as a contrast to Insertion Sort: same Big O on paper, but very different behaviour in practice.
How it works
Scan the entire list from beginning to end and find the smallest element. Swap it into position 0 (the far left). Now scan the remaining list — positions 1 through the end — and find the smallest element there. Swap it into position 1. Repeat: each time, scan one fewer element from the right and fill one more position from the left. After n−1 passes, every element has been moved into its final position and the list is sorted.
What makes Selection Sort unique
Every other algorithm on this page makes its number of swaps depend on how shuffled the data is — more disorder means more swaps. Selection Sort is different: it always makes exactly n−1 swaps — one per pass — no matter what order the data started in. This means it performs the fewest data writes of any algorithm here. In systems where writing data is very expensive — like flash memory in embedded devices, where each write shortens the life of the storage chip — this predictable, minimal-write behaviour can matter enormously.
The cost: no early exit
Because Selection Sort always scans the entire remaining unsorted region to find the minimum value, it always performs O(n²) comparisons — even if the list is already perfectly sorted. It can never exit early the way Bubble Sort and Insertion Sort can. This makes it non-adaptive: it treats sorted data exactly the same as random data.
🧍 Real-world analogy: Imagine picking the shortest person from a group of people and putting them at the very front of a line. Then picking the next shortest from everyone still standing in the group and putting them second. You always have to look at everyone still in the group to be sure you have found the shortest — even if they are already standing in a reasonable order.
🧠 Key insight: Selection Sort is the only algorithm on this page that is not adaptive — it treats a sorted list exactly the same as a random one, always doing the same amount of work. Paradoxically, that predictability can be an advantage in systems programming, where you sometimes need guaranteed worst-case behaviour rather than an algorithm whose performance varies with input. Knowing exactly how many writes will occur before you start is sometimes more valuable than being fast on easy inputs.
Complexity:
Best O(n²)
Average O(n²)
Worst O(n²)
Swaps: Always O(n) — fewest of all six algorithms
Shell Sort
Shell Sort was invented by Donald Shell (1924–2015), an American computer scientist, and published in 1959 in Communications of the ACM — one of the most prestigious computer science journals in the world. It was the first sorting algorithm ever published that broke the O(n²) barrier for average-case performance, and it launched decades of follow-on research into what gap sequences produce the best results. Shell's original paper is just two pages long, but its ideas are still being studied today.
How it works
Shell Sort is Insertion Sort with a crucial twist: instead of always comparing elements that are 1 position apart, it starts by comparing elements that are many positions apart. For our 8 ducks, the first gap is 4 (half of 8). Duck at position 0 is compared with duck at position 4; duck at position 1 with duck at position 5; and so on. Any pairs that are out of order get swapped. After this first pass, ducks that were very far from their correct positions have moved much closer. Then the gap shrinks to 2 and the process repeats. Finally, the gap reaches 1 and a full Insertion Sort pass runs to finish the job.
Why the final pass is so cheap
Here is the genius of the approach. When the list is nearly sorted — just a few elements slightly out of place — Insertion Sort is extremely fast, almost O(n). Shell Sort's large-gap passes are a way of making the list nearly sorted before the final gap-1 pass ever runs. The heavy lifting happens in the large-gap passes. The final Insertion Sort pass barely has to do any work because everything is already close to where it belongs. This is the core insight that makes Shell Sort so much faster than plain Insertion Sort on most real data.
Gap sequence
Shell's original rule: start the gap at ⌊n/2⌋ (n divided by 2, rounded down) and halve it each time until it reaches 1. For 8 ducks: 4 → 2 → 1. Since 1959, researchers have found that other gap sequences perform even better. Knuth's sequence (1, 4, 13, 40, 121, … — each term is 3× the previous plus 1) and Ciura's empirically-tuned sequence (1, 4, 10, 23, 57, 132, …) are two well-known improvements. For a teaching visualisation with 8 elements, Shell's original halving sequence is the clearest to explain and follow.
🔍 Real-world analogy: Imagine organising a large, messy library. Instead of immediately shelving every book in its exact final spot (slow!), you first sort books by rough category — fiction vs. non-fiction. Then within each category you sort by author's first letter. Then within each letter group you sort by exact title. Each pass is quick because you are working with much smaller decisions; by the time you need to be precise, each section is already nearly right.
The connection to Comb Sort
Shell Sort and Comb Sort are often described as cousins. Both use the same core idea: start with a large gap between compared elements, shrink the gap over time, and finish with gap = 1. The difference is in the operation they perform at each gap. Comb Sort performs swaps (like Bubble Sort). Shell Sort performs Insertion Sort passes (picks up and shifts). Because Insertion Sort is more efficient than Bubble Sort at the small-gap stage, Shell Sort generally outperforms Comb Sort on large data sets. But Comb Sort is conceptually simpler to explain, which is why it is taught alongside the swap family.
🧠 Key insight: Shell Sort is a beautiful example of a general technique called coarse-to-fine refinement. Instead of solving a hard problem directly, you first solve a rough version of it (large gaps make big improvements cheaply), and then the fine-detail solution (gap = 1 Insertion Sort) is nearly free. This pattern — solve a coarse problem first, use it to warm up the fine solution — appears over and over throughout computer science, from graphics rendering to machine learning.
Complexity:
Best O(n log n)
Average O(n log² n) (with Shell's halving sequence)
Worst O(n²)