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 the input size increases?
Analyze the time complexity of the following code snippet.
void sortColors(int* nums, int numsSize) {
int low = 0, mid = 0, high = numsSize - 1;
while (mid <= high) {
if (nums[mid] == 0) {
int temp = nums[low];
nums[low++] = nums[mid];
nums[mid++] = temp;
} else if (nums[mid] == 1) {
mid++;
} else {
int temp = nums[mid];
nums[mid] = nums[high];
nums[high--] = 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: At most once per element, so up to n times where n is the array size.
As the input size grows, the number of steps grows roughly in direct proportion.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 steps |
| 100 | About 100 steps |
| 1000 | About 1000 steps |
Pattern observation: The steps increase linearly as the input size increases.
Time Complexity: O(n)
This means the time to sort grows in a straight line with the number of elements.
[X] Wrong: "Because there are nested ifs and swaps, the time must be quadratic O(n²)."
[OK] Correct: Each element is visited at most once by the mid pointer, and swaps do not cause repeated passes, so the total steps stay linear.
Understanding this linear time sorting method shows you can handle efficient in-place algorithms, a skill valued in many coding challenges.
"What if we changed the array to contain more than three colors? How would the time complexity change?"
Practice
Sort Colors Two Pointer Dutch Flag algorithm?Solution
Step 1: Understand the problem context
The algorithm is designed specifically for arrays with only three distinct values: 0, 1, and 2.Step 2: Identify the sorting goal
The goal is to sort these values so that all 0s come first, then all 1s, and finally all 2s, in one pass without extra space.Final Answer:
To sort an array containing only 0s, 1s, and 2s in a single pass -> Option AQuick Check:
Sort Colors = Sort 0,1,2 in one pass [OK]
- Thinking it sorts any integer array
- Confusing it with general sorting algorithms
- Assuming it reverses the array
Solution
Step 1: Recall pointer roles
Low starts at 0 to track the boundary of 0s, mid starts at 0 to scan elements, high starts at n-1 to track boundary of 2s.Step 2: Match initialization
Only int low = 0, mid = 0, high = n - 1; correctly initializes low and mid at 0 and high at n-1.Final Answer:
int low = 0, mid = 0, high = n - 1; -> Option AQuick Check:
Pointers start low=0, mid=0, high=end [OK]
- Setting high to 0 instead of n-1
- Starting mid at n-1
- Confusing low and high initialization
arr = {2, 0, 2, 1, 1, 0}?Solution
Step 1: Trace the algorithm on input
Initial array: {2, 0, 2, 1, 1, 0}. After sorting: all 0s first, then 1s, then 2s.Step 2: Final sorted array
Sorted array is [0, 0, 1, 1, 2, 2].Final Answer:
[0, 0, 1, 1, 2, 2] -> Option CQuick Check:
Sorted colors = 0s, then 1s, then 2s [OK]
- Not sorting 0s and 2s correctly
- Mixing order of 1s and 0s
- Outputting original array
void sortColors(int* arr, int n) {
int low = 0, mid = 0, high = n - 1;
while (mid <= high) {
if (arr[mid] == 0) {
int temp = arr[low]; arr[low] = arr[mid]; arr[mid] = temp;
low++;
mid++;
} else if (arr[mid] == 1) {
mid++;
} else {
int temp = arr[mid]; arr[mid] = arr[high]; arr[high] = temp;
high--;
}
}
}Solution
Step 1: Analyze pointer increments
When swapping arr[mid] with arr[low] (for 0), low is incremented but mid is not incremented, causing infinite loop or wrong behavior.Step 2: Correct pointer movement
After swapping with low, both low and mid should be incremented to move forward.Final Answer:
Missing increment of mid after swapping with low -> Option BQuick Check:
Increment mid after swapping with low [OK]
- Not incrementing mid after swapping with low
- Incrementing mid after swapping with high
- Incorrect pointer initialization
arr = {1, 2, 0, 1, 2, 0, 1}, how many swaps will the Dutch National Flag algorithm perform to sort it?Solution
Step 1: Trace swaps during sorting
Initial array: {1, 2, 0, 1, 2, 0, 1}
- Swap 1: mid=1 (2) with high=6 (1) -> [1,1,0,1,2,0,2], high=5
mid=1 now 1 -> mid=2
- Swap 2: mid=2 (0) with low=0 (1) -> [0,1,1,1,2,0,2], low=1, mid=3
mid=3 (1) -> mid=4
- Swap 3: mid=4 (2) with high=5 (0) -> [0,1,1,1,0,2,2], high=4
mid=4 now 0 ->
- Swap 4: mid=4 (0) with low=1 (1) -> [0,0,1,1,1,2,2], low=2, mid=5 (stop)Step 2: Count total swaps
Total swaps counted are 4.Final Answer:
4 -> Option DQuick Check:
Count swaps carefully during pointer moves [OK]
- Counting pointer increments as swaps
- Missing swaps when swapping with high
- Undercounting swaps for consecutive swaps on same mid
Ready to prove it?
Pass the test and this pattern moves unknown
