Sort Colors Two Pointer Dutch Flag in DSA C# - Time & Space Complexity
We want to understand how the time needed to sort colors using two pointers changes as the list grows.
How does the number of steps grow when we have more colors to sort?
Analyze the time complexity of the following code snippet.
public void SortColors(int[] nums) {
int low = 0, mid = 0, high = nums.Length - 1;
while (mid <= high) {
if (nums[mid] == 0) {
Swap(nums, low++, mid++);
} else if (nums[mid] == 1) {
mid++;
} else {
Swap(nums, mid, high--);
}
}
}
void Swap(int[] arr, int i, int j) {
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
This code sorts an array containing 0s, 1s, and 2s by moving pointers and swapping elements.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The while loop that moves the mid pointer through the array.
- How many times: Each element is visited at most once by mid, so up to n times where n is array length.
As the array size grows, the number of steps grows roughly the same amount.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 steps |
| 100 | About 100 steps |
| 1000 | About 1000 steps |
Pattern observation: The steps grow linearly with input size, doubling input roughly doubles steps.
Time Complexity: O(n)
This means the time to sort grows in direct proportion to the number of elements.
[X] Wrong: "Because there are nested ifs and swaps, the time must be quadratic O(n²)."
[OK] Correct: The loop runs once per element, and swaps happen at most once per element, so total steps grow linearly, not squared.
Understanding this linear time sorting method shows you can handle problems efficiently by using pointers smartly, a skill valued in many coding challenges.
"What if we used three separate loops to sort instead of two pointers? How would the time complexity change?"
Practice
Sort Colors Two Pointer Dutch Flag algorithm?Solution
Step 1: Understand the problem
The algorithm sorts an array containing only 0s, 1s, and 2s representing colors.Step 2: Use three pointers
It uses three pointers: low, mid, and high to separate the colors in one pass efficiently.Final Answer:
Use three pointers to separate 0s, 1s, and 2s in one pass -> Option DQuick Check:
Three pointers = A [OK]
- Using built-in sort instead of pointers
- Counting colors then rewriting array
- Swapping elements without pointers
Solution
Step 1: Identify pointer roles
Low and mid start at 0, high starts at last index (length - 1).Step 2: Check initialization correctness
int low = 0, mid = 0, high = nums.Length - 1; correctly sets low and mid to 0, high to nums.Length - 1.Final Answer:
int low = 0, mid = 0, high = nums.Length - 1; -> Option AQuick Check:
Correct pointer init = C [OK]
- Setting high to 0 instead of last index
- Starting mid at length - 1
- Using invalid pointer values
nums = new int[] {2, 0, 2, 1, 1, 0};?Solution
Step 1: Start with pointers low=0, mid=0, high=5
Initial array: [2, 0, 2, 1, 1, 0]Step 2: Process elements using Dutch Flag logic
Swap 2 with 0 at high, move high left; swap 2 with 0 at low; move mid and low accordingly until sorted.Final Answer:
[0, 0, 1, 1, 2, 2] -> Option CQuick Check:
Sorted colors = A [OK]
- Not swapping correctly
- Moving pointers incorrectly
- Confusing order of colors
int low = 0, mid = 0, high = nums.Length - 1;
while (mid <= high) {
if (nums[mid] == 0) {
Swap(nums, low, mid);
low++;
} else if (nums[mid] == 1) {
mid++;
} else {
Swap(nums, mid, high);
high--;
}
}Solution
Step 1: Check pointer increments after swaps
When nums[mid] == 0, after swapping with low, mid must also increment to avoid infinite loop.Step 2: Identify missing mid++
The code increments low but misses mid++, causing mid to stay and loop endlessly.Final Answer:
Missing increment of mid after swapping 0 -> Option BQuick Check:
Increment mid after 0 swap = D [OK]
- Forgetting mid++ after swapping 0
- Decrementing high incorrectly
- Incorrect swap parameters
nums containing only 0s, 1s, and 2s, how would you modify the Dutch Flag algorithm to sort an array that also contains 3s, placing all 3s at the end?Solution
Step 1: Understand limitation of three pointers
The original algorithm handles only three values (0,1,2). Adding 3 requires extra handling.Step 2: Introduce a fourth pointer
Use four pointers to separate 0s, 1s, 2s, and 3s, extending the logic to place 3s at the end.Final Answer:
Add a fourth pointer and extend the logic to handle 3s at the end -> Option AQuick Check:
Four pointers for four colors = B [OK]
- Treating 3s as 2s causing wrong order
- Using built-in sort instead of pointers
- Ignoring 3s placement
Ready to prove it?
Pass the test and this pattern moves unknown
