Sorting Algorithms
What is this?
These are the step-by-step recipes a computer follows to put things in order. Bubble Sort is the simplest โ keep swapping neighbours that are out of order, like nudging books one slot at a time. Merge Sort is smarter โ split the pile in half, sort each half, then zip them back together. Working through them by hand shows why one is slow and the other fast.
๐ก Fun fact: Merge sort was described by John von Neumann back in 1945, making it one of the oldest algorithms still in everyday use.
๐ Sign in free to read the deep dives. Sign in with Google or Microsoft to start solving.
Core idea: Before you reach for a library
sort, understand what one is doing. Two algorithms anchor the spectrum: Bubble Sort, the simplest possible idea โ repeatedly swap out-of-order neighbours until nothing moves โ and Merge Sort, the canonical divide-and-conquer sort that splits the array in half, sorts each half, and merges them back in order. One is O(nยฒ); the other is O(n log n). Seeing why is the whole lesson.
The pattern
Every comparison sort is a strategy for moving each element past the ones it should sit behind. Bubble Sort does it one tiny adjacent swap at a time, so a single far-out element can take a full pass to drift home โ that quadratic cost is the price of only ever looking at neighbours. Merge Sort instead divides the work: recursively halve until pieces are trivially sorted, then merge sorted runs by walking two pointers and always taking the smaller front. Merging n elements is linear, the recursion is log n deep, so the total is O(n log n). The merge step also never reorders equal keys โ that is what makes Merge Sort stable.
The problems
These are algorithm walkthroughs, not LeetCode problems โ implement each by hand to feel the cost.
- Bubble Sort โ repeatedly sweep the array swapping adjacent out-of-order pairs; after pass k the largest k elements are parked at the end. Add an "early exit if no swaps" check to handle nearly-sorted input in one pass. O(nยฒ) time, O(1) space, stable.
- Merge Sort โ split the array at the midpoint, recursively sort both halves, then merge the two sorted halves with a linear two-pointer scan. O(n log n) time, O(n) auxiliary space, stable. It is the reference example of divide-and-conquer.
Key takeaways
- Bubble Sort = adjacent swaps; intuitive but O(nยฒ) โ useful only as a teaching baseline.
- Merge Sort = divide-and-conquer; split, sort halves, merge โ a guaranteed stable O(n log n).
- The merge step is the heart of it โ linearly combining two sorted runs reappears everywhere, including the next chapter.
- Why it matters: knowing how sorting works lets you reason about stability, custom orderings, and when a built-in sort is or isn't enough.
Order: Bubble Sort โ Merge Sort.
Core idea: Walk down the list comparing each pair of neighbours; whenever the left one is bigger than the right, swap them. After one full walk the biggest value has "bubbled" all the way to the end. Repeat until a whole walk makes no swaps โ then you're sorted.
Problem, rephrased
You're handed a row of numbered tiles laid out left to right, and your only legal move is to compare two tiles sitting next to each other and swap them if they're out of order. You can't pick a tile up and drop it somewhere far away; you can only nudge neighbours. Sort the row into ascending order using nothing but these adjacent swaps.
This is exactly bubble sort: scan left to right, swap any adjacent pair where left > right, and keep doing full passes until a pass goes by with zero swaps. Each pass guarantees the largest unsorted tile floats to its final resting place at the right end โ like the biggest air bubble rising fastest to the surface.
| Input | Output | Why |
|---|---|---|
[5, 1, 4, 2, 8] |
[1, 2, 4, 5, 8] |
ascending order after adjacent swaps |
[3, 3, 1] |
[1, 3, 3] |
duplicates keep their original relative order |
[1, 2, 3, 4] |
[1, 2, 3, 4] |
already sorted โ one clean pass, no swaps |
[4, 3, 2, 1] |
[1, 2, 3, 4] |
reverse order โ the worst case, maximal swapping |
The output is the same list, reordered in place โ we don't allocate a new array, we rearrange the one we were given.
Sign in to continue reading
The rest of this lesson is available with a free account. Signing in with Google or Microsoft is free.
Sign in to read the full lesson