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: Cut the array in half, sort each half (by cutting those in half, all the way down), then zip the two sorted halves back together with one linear pass. The sort is easy because the merge is easy โ and the merge is easy because both inputs are already sorted.
Problem, rephrased
You're handed an unsorted list of numbers and asked to return it in ascending order โ but with two non-negotiable guarantees that rule out the easy answers: the running time must be O(n log n) in the worst case (no quicksort-style O(nยฒ) blowups on adversarial input), and the sort must be stable (equal elements keep their original relative order).
The classic divide-and-conquer recipe nails both:
- Divide โ split the array into two halves at the midpoint.
- Conquer โ recursively sort the left half and the right half.
- Combine โ merge the two now-sorted halves into one sorted array with a two-pointer linear walk.
| Input | Output | Note |
|---|---|---|
[21, 214, 2, 44, 22, 54] |
[2, 21, 22, 44, 54, 214] |
the running example below |
[5, 1, 1, 2] |
[1, 1, 2, 5] |
duplicates preserved, original order kept |
[] |
[] |
empty input is already sorted |
[42] |
[42] |
one element is already sorted (base case) |
Output is the same multiset of values, re-ordered ascending; nothing added, nothing dropped.
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