Sort Colors
Master this topic with zero to advance depth.
Sort Colors
Given an array nums with n objects colored red, white, or blue, sort them in-place so that objects of the same color are adjacent, in the order red, white, and blue. We use the integers 0, 1, and 2 to represent the color red, white, and blue, respectively.
Visual Representation
Array: [2, 0, 2, 1, 1, 0]
Step 1: Move 0s to the front (Left side).
Step 2: Move 2s to the end (Right side).
Step 3: 1s will naturally be in the middle.
Result: [0, 0, 1, 1, 2, 2]Examples
The array is sorted in-place to group 0s, 1s, and 2s.
Level I: Two Pass (Counting)
Intuition
Since there are only three possible values (0, 1, 2), we can count the frequency of each and then overwrite the original array.
Detailed Dry Run
Input: [2, 0, 2, 1, 1, 0]
- Count:
0: 2, 1: 2, 2: 2. - Overwrite:
nums[0..1]=0, nums[2..3]=1, nums[4..5]=2. Result:[0, 0, 1, 1, 2, 2]
⚠️ Common Pitfalls & Tips
Requires two passes over the data.
Level II: Better (Two Pass - Two Pointers)
Intuition
Instead of counting all colors at once, we can use two passes with two pointers. In the first pass, we move all 0s to the front. In the second pass, we move all 1s from where the 0s ended.
- Pass 1: Pointer
pat start. Iterate, ifnums[i] == 0, swap withp++. - Pass 2: Iterate starting from
p. Ifnums[i] == 1, swap withp++.
Detailed Dry Run
Input: [2, 0, 1]
- Pass 1 (for 0):
[0, 2, 1],p=1. - Pass 2 (for 1):
[0, 1, 2],p=2.
⚠️ Common Pitfalls & Tips
Still O(N) but takes two passes. Standard Dutch National Flag is better.
Level III: Optimal (One Pass - Dutch National Flag)
Intuition
Use three pointers to partition the array: low for 0s, high for 2s, and curr for the current element. This allows sorting in a single pass.
Detailed Dry Run
Input: [2,0,1]
low=0, curr=0, high=2.nums[0]=2: Swap withhigh. Array:[1,0,2], high=1.curr=0.nums[0]=1:curr++. Array:[1,0,2], curr=1.curr=1.nums[1]=0: Swap withlow. Array:[0,1,2], low=1, curr=2.- DONE.
⚠️ Common Pitfalls & Tips
Be careful not to increment curr when swapping with high, as the newly swapped element needs to be checked.
Found an issue or have a suggestion?
Help us improve! Report bugs or suggest new features on our Telegram group.