Introduction
The Sort Colors problem involves sorting an array containing only:
0s, 1s, and 2swhere:
- 0 represents Red
- 1 represents White
- 2 represents Blue
The task is to:
- sort the array in-place
without using built-in sorting
This problem is one of the most important applications of:
Dutch National Flag AlgorithmThis problem helps in understanding:
- partitioning techniques
- two pointers
- three pointers
- in-place sorting
Example
Input:arr = [2, 0, 2, 1, 1, 0]
Output:
[0, 0, 1, 1, 2, 2]
Explanation:
All 0s come first,
then 1s,
then 2s.
Constraints
1 <= arr.length <= 10^5arr[i] is either:
0, 1, or 2
Approach 1 : Counting Method
Explanation
The simplest way to solve this problem is:
- Count:
- number of 0s
- number of 1s
- number of 2s
- Rewrite array using counts
This works efficiently because:
- only three unique values exist
Steps
- Count frequencies.
- Fill array with 0s.
- Fill array with 1s.
- Fill array with 2s.
- Return sorted array.
Dry Run
Input:[2, 0, 2, 1, 1, 0]
Count values:
0 → 2 times
1 → 2 times
2 → 2 times
Rebuild array:
[0, 0, 1, 1, 2, 2]
Final Result:
[0, 0, 1, 1, 2, 2]
Counting Method Code
Complexity Analysis
Time Complexity: O(n)Explanation:
Array is traversed twice.
Space Complexity: O(1)
Explanation:
Only counters are used.
Explanation
The optimized solution uses:
Three PointersPointers:
- low
- mid
- high
Rules:
- 0 goes left
- 1 stays middle
- 2 goes right
This sorts array in a single traversal.
Steps
- Initialize:
- low = 0
- mid = 0
- high = n - 1
- Traverse while mid <= high.
- If value is 0:
- swap with low
- If value is 1:
- move mid
- If value is 2:
- swap with high
Dry Run
Input:[2, 0, 2, 1, 1, 0]
Initial:
low = 0
mid = 0
high = 5
arr[mid] = 2
Swap with high
Array:
[0, 0, 2, 1, 1, 2]
high--
arr[mid] = 0
Swap with low
Array:
[0, 0, 2, 1, 1, 2]
low++
mid++
Continue traversal...
Final Result:
[0, 0, 1, 1, 2, 2]
Dutch National Flag Algorithm Code
Complexity Analysis
Time Complexity: O(n)Explanation:
Array is traversed only once.
Space Complexity: O(1)
Explanation:
Sorting is performed in-place.
Edge Cases
- Empty array
- All values same
- Already sorted array
- Reverse sorted array
- Only two unique values present
Why This Problem is Important
Sort Colors helps in understanding:
- Partitioning techniques
- Three pointer approach
- In-place sorting
- Dutch National Flag Algorithm
- Efficient traversal
It is one of the most important partitioning interview problems.
Real-World Applications
Partitioning concepts are used in:
- Quick Sort
- Data segregation
- Memory optimization
- Stream processing
- Task scheduling systems
Common Mistakes
- Incorrect pointer movement
- Swapping with wrong index
- Incrementing mid incorrectly
- Wrong loop condition
Interview Tips
Interviewers often expect:
- Dutch National Flag explanation
- O(n) in-place optimization
- Three pointer understanding
Always explain:
- role of low pointer
- role of mid pointer
- role of high pointer
Related Questions
- Partition Array
- Quick Sort
- Move Zeroes
- Sort Array By Parity
- Kth Largest Element
Final Takeaway
The Sort Colors problem is a fundamental partitioning problem that teaches the Dutch National Flag Algorithm and efficient in-place sorting techniques. Understanding this problem builds a strong foundation for advanced partitioning and sorting interview problems.