What if you could sort three colors perfectly in just one quick sweep?
Why Sort Colors Two Pointer Dutch Flag in DSA Python?
Imagine you have a box of mixed colored balls: red, white, and blue. You want to arrange them so all reds come first, then whites, then blues. Doing this by picking each ball and placing it in the right spot one by one is tiring and slow.
Manually sorting each ball means checking every ball multiple times and moving them around again and again. This wastes time and can cause mistakes, like mixing colors or missing some balls.
The two-pointer Dutch Flag method sorts the balls in just one pass. It uses two pointers to keep track of where reds and blues should go, swapping balls as needed. This way, you sort all colors quickly and without confusion.
colors = [2,0,2,1,1,0] for i in range(len(colors)): for j in range(i+1, len(colors)): if colors[i] > colors[j]: colors[i], colors[j] = colors[j], colors[i]
colors = [2,0,2,1,1,0] red, white, blue = 0, 0, len(colors) - 1 while white <= blue: if colors[white] == 0: colors[red], colors[white] = colors[white], colors[red] red += 1 white += 1 elif colors[white] == 2: colors[white], colors[blue] = colors[blue], colors[white] blue -= 1 else: white += 1
This method lets you sort three groups in one quick pass, saving time and effort.
Sorting laundry by color: whites, darks, and colors. Instead of sorting each piece multiple times, you quickly separate them in one go.
Manual sorting is slow and error-prone.
Two-pointer Dutch Flag sorts in one pass efficiently.
It uses swapping and pointers to organize colors quickly.
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 algorithm's purpose
It sorts these values in one pass using two pointers, placing all 0s first, then 1s, then 2s.Final Answer:
To sort an array containing only 0s, 1s, and 2s in a single pass -> Option DQuick Check:
Sort colors = sort 0,1,2 in one pass [OK]
- Thinking it sorts any array, not just 0,1,2
- Confusing it with general sorting algorithms
- Assuming it requires multiple passes
Solution
Step 1: Recall pointer roles
Low and mid start at the beginning (index 0), high starts at the end (last index).Step 2: Match initialization
So, low = 0, mid = 0, high = len(nums) - 1 is correct.Final Answer:
low, mid, high = 0, 0, len(nums) - 1 -> Option BQuick Check:
Pointers start low=0, mid=0, high=end [OK]
- Swapping high and mid initialization
- Starting pointers at wrong indices
- Using length instead of length-1 for high
nums = [2, 0, 2, 1, 1, 0]
low, mid, high = 0, 0, len(nums) - 1
while mid <= high:
if nums[mid] == 0:
nums[low], nums[mid] = nums[mid], nums[low]
low += 1
mid += 1
elif nums[mid] == 1:
mid += 1
else:
nums[mid], nums[high] = nums[high], nums[mid]
high -= 1
print(nums)Solution
Step 1: Trace the algorithm on input
Initially nums = [2,0,2,1,1,0]. The algorithm swaps 0s to front, 2s to end, 1s stay in middle.Step 2: Final sorted array
After the loop, nums becomes [0,0,1,1,2,2], sorted by color.Final Answer:
[0, 0, 1, 1, 2, 2] -> Option CQuick Check:
Sorted colors = [0,0,1,1,2,2] [OK]
- Not swapping correctly when nums[mid] == 2
- Incrementing mid after swapping with high
- Confusing order of pointers
nums = [2, 0, 1]
low, mid, high = 0, 0, len(nums) - 1
while mid < high:
if nums[mid] == 0:
nums[low], nums[mid] = nums[mid], nums[low]
low += 1
mid += 1
elif nums[mid] == 1:
mid += 1
else:
nums[mid], nums[high] = nums[high], nums[mid]
high -= 1
print(nums)Solution
Step 1: Analyze loop condition
The loop runs while mid < high, so it stops when mid == high, missing last element check.Step 2: Correct loop condition
It should be mid <= high to process all elements including when mid equals high.Final Answer:
The loop condition should be 'mid <= high' instead of 'mid < high' -> Option AQuick Check:
Loop must cover mid == high [OK]
- Using 'mid < high' misses last element
- Changing pointer initialization unnecessarily
- Incorrect swap logic for 0 or 2
nums = [1, 2, 0, 1, 2, 0, 1], how many swaps will the Dutch Flag algorithm perform to sort it?Solution
Step 1: Trace swaps during sorting
Initial: [1,2,0,1,2,0,1] low=0, mid=0, high=6
- mid=0 (1): mid=1 (no swap)
- mid=1 (2): swap(1,6) -> [1,1,0,1,2,0,2] high=5 (swap 1)
- mid=1 (1): mid=2
- mid=2 (0): swap(0,2) -> [0,1,1,1,2,0,2] low=1 mid=3 (swap 2)
- mid=3 (1): mid=4
- mid=4 (2): swap(4,5) -> [0,1,1,1,0,2,2] high=4 (swap 3)
- mid=4 (0): swap(1,4) -> [0,0,1,1,1,2,2] low=2 mid=5 (swap 4)Step 2: Total swaps counted
There are 4 swaps in total during the process.Final Answer:
4 -> Option AQuick Check:
Count swaps carefully during pointer moves [OK]
- Counting pointer moves as swaps
- Missing swaps when swapping with same element
- Not tracing all pointer changes
Ready to prove it?
Pass the test and this pattern moves unknown
