Sorting Algorithms
Why Bother Putting Things in Order?
A dictionary with words in random order would be useless. A leaderboard that ignored scores would be pointless. Order is what makes information usable β and it is what makes fast searching algorithms like binary search possible at all.
A sorting algorithm rearranges a list into a defined order: numbers smallest to largest, names alphabetically, files newest first. Computers sort constantly β your inbox by date, your photos by time, search results by relevance β so people have invented many sorting algorithms, each with different strengths. Here are two classics.
Bubble Sort β Swap Your Neighbours
Bubble sort is the simplest sort to understand. Its only move: compare two neighbouring items, and swap them if they are in the wrong order.
The full algorithm:
- Start at the beginning of the list
- Compare item 1 with item 2 β swap if out of order
- Compare item 2 with item 3 β swap if out of orderβ¦ continue to the end (that is one pass)
- Repeat whole passes until a pass makes no swaps β then the list is sorted
Watch it sort 5, 2, 8, 1 (smallest to largest):
Pass 1: 5,2,8,1 β 2,5,8,1 β 2,5,8,1 β 2,5,1,8 (8 reaches the end)
Pass 2: 2,5,1,8 β 2,5,1,8 β 2,1,5,8 (5 in place)
Pass 3: 2,1,5,8 β 1,2,5,8 (swap!)
Pass 4: 1,2,5,8 (no swaps β done)
Notice the pattern: each pass carries the largest remaining value to the end, like a bubble rising to the surface β hence the name. The whole thing is just a loop inside a loop, with a condition deciding each swap. You could draw it as a flowchart in ten boxes.
The catch is speed. Every pass walks nearly the whole list, and a badly shuffled list of n items can need almost n passes. Sorting 10 items β instant. Sorting a million β roughly a trillion comparisons. Far too slow.
Merge Sort β Divide and Conquer
Merge sort takes a cleverer approach built on decomposition: if a list is hard to sort, split it.
- Split the list in half, and keep halving, until every piece has just one item (a one-item list is already sorted!)
- Merge pairs of sorted pieces into bigger sorted pieces: repeatedly compare the front items of the two pieces and take the smaller
- Keep merging until one fully sorted list remains
Sorting 5, 2, 8, 1:
Split: [5,2,8,1] β [5,2] [8,1] β [5] [2] [8] [1]
Merge: [5]+[2] β [2,5] [8]+[1] β [1,8]
Merge: [2,5]+[1,8] β [1,2,5,8]
Merging is fast because both pieces are already sorted β you only ever compare the two front items. And since the list is halved again and again (the same halving power that makes binary search fast), merge sort handles a million items in roughly 20 million comparisons, not a trillion. That difference is why real systems use divide-and-conquer sorts, not bubble sort.
Choosing and Comparing
Two algorithms, one job, very different characters:
| Bubble sort | Merge sort | |
|---|---|---|
| Idea | Swap out-of-order neighbours | Split, sort halves, merge |
| Easy to understand? | Very | Moderately |
| Speed on large lists | Slow | Fast |
| Best use | Learning; tiny lists | Serious sorting |
This is the deepest lesson of the topic: correct is not the same as good. Both algorithms produce a perfectly sorted list. But one of them finishes before you release the mouse button and the other might run for hours. Comparing algorithms β not just writing them β is a core computing skill, and it is also how you will approach searching, debugging slow programs, and designing your own solutions.
Try It Yourself
Take seven playing cards, shuffle them, and sort them strictly by the bubble sort rules β only neighbouring cards may be compared and swapped. Count your comparisons. Then shuffle again and try merge sort: deal into single cards, then merge pairs. Which felt more mechanical? Which needed fewer comparisons? You have just benchmarked two algorithms by hand.
Worked Example β A Slightly Trickier Merge
Merging two sorted lists of different lengths is worth tracing once, since real data rarely splits into neat equal halves. Merge [3, 7, 9] with [1, 4, 5, 8]:
Compare 3 and 1 β take 1 result: [1] remaining: [3,7,9] [4,5,8]
Compare 3 and 4 β take 3 result: [1,3] remaining: [7,9] [4,5,8]
Compare 7 and 4 β take 4 result: [1,3,4] remaining: [7,9] [5,8]
Compare 7 and 5 β take 5 result: [1,3,4,5] remaining: [7,9] [8]
Compare 7 and 8 β take 7 result: [1,3,4,5,7] remaining: [9] [8]
Compare 9 and 8 β take 8 result: [1,3,4,5,7,8] remaining: [9] []
one list empty β take remaining: [1,3,4,5,7,8,9]
Notice the final rule this trace reveals: once one list runs out, the rest of the other list is already sorted, so it can simply be copied across without further comparisons β no special case needs writing beyond "if one side is empty, take everything from the other side." This is exactly the kind of boundary behaviour that a trace table or test plan would deliberately check before trusting an implementation.
Key Words
- Sorting algorithm β a method for rearranging a list into a defined order
- Bubble sort β repeatedly swap out-of-order neighbours until a pass makes no swaps
- Pass β one full trip along the list, comparing each pair of neighbours
- Merge sort β split the list into halves, sort them, then merge the sorted halves
- Divide and conquer β solving a problem by splitting it into smaller versions of itself