Introduction
The Sort 0s, 1s, and 2s problem involves sorting an array containing only:
- 0s
- 1s
- 2s
Given an array arr[], the task is to sort the array in ascending order without using built-in sorting methods.
This problem introduces the famous:
Dutch National Flag AlgorithmThis problem helps in understanding:
- partitioning techniques
- pointer manipulation
- in-place sorting
- efficient traversal strategies
Example
Input: arr[] = [2,0,2,1,1,0]Output: [0,0,1,1,2,2]
Explanation:
After sorting:
0s come first
1s come next
2s come last
Input: arr[] = [2,1,0,1,2,0]
Output: [0,0,1,1,2,2]
Explanation:
Array is rearranged in sorted order.
Constraints
1 <= n <= 10^5arr[i] ∈ {0,1,2}
Approach 1 : Brute Force (Counting Method)
Explanation
The simplest way to solve this problem is:
- Count:
- number of 0s
- number of 1s
- number of 2s
- Refill the array using counts.
This approach is easy to understand but requires multiple traversals.
Steps
- Initialize counters:
- zero
- one
- two
- Traverse array and count frequencies.
- Fill:
- 0s first
- then 1s
- then 2s
- Return sorted array.
Dry Run
Input Array:[2,0,2,1,1,0]
Count values:
0 count = 2
1 count = 2
2 count = 2
Refill array:
[0,0,1,1,2,2]
Final Result:
[0,0,1,1,2,2]
Brute Force Code
Complexity Analysis
Time Complexity: O(n)Explanation:
Array is traversed multiple times.
Space Complexity: O(1)
Explanation:
Only counters are used.
Approach 2 : Optimized Solution (Dutch National Flag Algorithm)
Explanation
The optimized solution uses three pointers:
- low
- mid
- high
The idea is:
- 0s belong to left side
- 1s stay in middle
- 2s belong to right side
This sorts the array in a single traversal.
Steps
- Initialize:
- low = 0
- mid = 0
- high = n - 1
- Traverse while mid ≤ high.
- If arr[mid] == 0:
- swap low and mid
- increment both pointers
- If arr[mid] == 1:
- move mid pointer
- If arr[mid] == 2:
- swap mid and high
- decrement high
Dry Run
Input Array:[2,0,2,1,1,0]
Initially:
low = 0
mid = 0
high = 5
Traverse 2:
Swap arr[mid] and arr[high]
Array:
[0,0,2,1,1,2]
high = 4
Traverse 0:
Swap arr[low] and arr[mid]
low = 1
mid = 1
Traverse next 0:
Swap again
low = 2
mid = 2
Traverse 2:
Swap with high
Array:
[0,0,1,1,2,2]
Final Result:
[0,0,1,1,2,2]
Optimized Code
Complexity Analysis
Time Complexity: O(n)Explanation:
Array is traversed only once.
Space Complexity: O(1)
Explanation:
Sorting is done in-place.
Time Complexity: O(n)Explanation: Array is traversed only once. Space Complexity: O(1)
Explanation: Sorting is done in-place.
Edge Cases
- Array contains only 0s
- Array contains only 1s
- Array contains only 2s
- Array already sorted
- Array sorted in reverse order
Why This Problem is Important
This problem helps in understanding:
- Dutch National Flag Algorithm
- In-place sorting
- Pointer manipulation
- Partitioning techniques
- Efficient traversal
It is one of the most important array sorting interview problems.
Real-World Applications
Partitioning techniques are used in:
- Quick Sort
- Data categorization
- Memory partitioning
- Traffic classification
- Resource grouping systems
Common Mistakes
- Incorrect pointer updates
- Forgetting mid pointer handling
- Infinite loops
- Wrong swap conditions
Interview Tips
Interviewers often expect:
- Dutch National Flag Algorithm
- O(n) single traversal solution
- In-place sorting explanation
Always explain the role of low, mid, and high pointers clearly.
Related Questions
- Merge Sorted Arrays
- Partition Array
- Quick Sort
- Move Zeroes
- Remove Duplicates
Final Takeaway
The Sort 0s, 1s, and 2s problem is a fundamental array partitioning problem that teaches the Dutch National Flag Algorithm and in-place sorting techniques. Understanding this problem builds a strong foundation for advanced sorting and partition-based interview problems