Sort Colors easy
Description
Given an array nums containing only the integers 0, 1, and 2, sort them in-place so that they appear in the order 0, 0, …, 1, 1, …, 2, 2, ….
You must solve this without using the library’s sort function, and ideally in a single pass with O(1) extra space.
Examples
> Case 1:
Input: nums = [2, 0, 2, 1, 1, 0]
Output: [0, 0, 1, 1, 2, 2]
> Case 2:
Input: nums = [2, 0, 1]
Output: [0, 1, 2]Constraints
1 <= nums.length <= 300nums[i]is0,1, or2.
Approach 1: Counting sort (two passes)
Count how many 0s, 1s, 2s, then overwrite the array. Two passes, O(n) time, O(1) space. Correct and easy — but uses two passes.
def sort_colors(nums):
cnt = [0, 0, 0]
for x in nums:
cnt[x] += 1
i = 0
for v in range(3):
for _ in range(cnt[v]):
nums[i] = v; i += 1This is counting sort, which we cover in detail on Day 12. It works perfectly when the value range is tiny.
Approach 2: Dutch National Flag (one pass)
The famous one-pass algorithm, invented by Edsger Dijkstra. Maintain three pointers:
low— the boundary of the0s region. Everything strictly belowlowis0.high— the boundary of the2s region. Everything strictly abovehighis2.mid— the cursor scanning forward.
For each element at mid:
- If it’s
0, swap withlow; advance bothlowandmid. - If it’s
2, swap withhigh; decrementhigh(but don’t advancemid— the swapped-in element hasn’t been examined). - If it’s
1, advancemid.
Stop when mid passes high.
nums = [2, 0, 2, 1, 1, 0]
↑ ↑
low high
mid
mid=0, val=2 → swap nums[0], nums[5]; high--
nums = [0, 0, 2, 1, 1, 2] low=0, mid=0, high=4
mid=0, val=0 → swap nums[0], nums[0]; low++, mid++
nums = [0, 0, 2, 1, 1, 2] low=1, mid=1, high=4
mid=1, val=0 → swap; low++, mid++
nums = [0, 0, 2, 1, 1, 2] low=2, mid=2, high=4
mid=2, val=2 → swap nums[2], nums[4]; high--
nums = [0, 0, 1, 1, 2, 2] low=2, mid=2, high=3
mid=2, val=1 → mid++
mid=3, val=1 → mid++
mid=4 > high=3 → stop.
Result: [0, 0, 1, 1, 2, 2] ✓The most common bug: advancing mid after the nums[mid] == 2 case. Don’t! You haven’t seen the value that just got swapped in from the high end — it could be a 0 that needs to go to the front. Only the 0 case advances both low and mid (because you swapped in a value from the already-checked low region).
Analysis
| Approach | Time | Space | Passes |
|---|---|---|---|
| Counting (two-pass) | O(n) | O(1) | 2 |
| Dutch National Flag | O(n) | O(1) | 1 |
Why this is a sorting interview classic
The Dutch National Flag algorithm generalizes the partition step of quicksort to three regions instead of two. It’s the bridge between simple sorts and the 3-way quicksort you’ll see on Day 12.