Sort Colors Using Two Pointer Dutch Flag Algorithm
📖 Scenario: You are organizing colored balls in a line. The balls can be red, white, or blue, represented by numbers 0, 1, and 2 respectively. Your task is to sort them so that all reds come first, then whites, and finally blues.
🎯 Goal: Build a program that sorts a list of colors represented by 0s, 1s, and 2s using the two-pointer Dutch Flag algorithm.
📋 What You'll Learn
Create a list called colors with the exact values [2, 0, 2, 1, 1, 0].
Create two pointer variables called low and high initialized to 0 and len(colors) - 1 respectively.
Use a while loop with a pointer current to sort the list in-place using the Dutch Flag algorithm.
Print the sorted colors list at the end.
💡 Why This Matters
🌍 Real World
Sorting colored objects or items quickly without extra space is useful in manufacturing and organizing tasks.
💼 Career
Understanding in-place sorting algorithms like Dutch Flag helps in software roles involving data processing and optimization.
Progress0 / 4 steps
1
Create the initial list of colors
Create a list called colors with these exact values: [2, 0, 2, 1, 1, 0].
DSA Python
Hint
Use square brackets to create the list and include the numbers exactly as shown.
2
Initialize two pointers for sorting
Create two variables called low and high. Set low = 0 and high = len(colors) - 1.
DSA Python
Hint
Remember, len(colors) gives the number of items in the list.
3
Sort the colors using the Dutch Flag algorithm
Create a variable current and set it to 0. Use a while loop with the condition current <= high. Inside the loop, do the following: - If colors[current] == 0, swap colors[current] and colors[low], then increment low and current. - Else if colors[current] == 2, swap colors[current] and colors[high], then decrement high. - Else, increment current.
DSA Python
Hint
Use swapping to move 0s to the front and 2s to the end. Increase or decrease pointers accordingly.
4
Print the sorted colors list
Print the colors list to show the sorted order.
DSA Python
Hint
Use print(colors) to display the sorted list.
Practice
(1/5)
1. What is the main goal of the Sort Colors Two Pointer Dutch Flag algorithm?
easy
A. To reverse the elements of an array
B. To sort any array of integers in ascending order
C. To find the maximum element in an array
D. To sort an array containing only 0s, 1s, and 2s in a single pass
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 D
Quick Check:
Sort colors = sort 0,1,2 in one pass [OK]
Hint: Remember: Dutch Flag sorts 0,1,2 in one pass [OK]
Common Mistakes:
Thinking it sorts any array, not just 0,1,2
Confusing it with general sorting algorithms
Assuming it requires multiple passes
2. Which of the following is the correct initialization of pointers in the Dutch Flag algorithm in Python?
easy
A. low, mid, high = 0, len(nums) - 1, 0
B. low, mid, high = 0, 0, len(nums) - 1
C. low, mid, high = len(nums) - 1, 0, 0
D. low, mid, high = len(nums) - 1, len(nums) - 1, 0
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 B
Quick Check:
Pointers start low=0, mid=0, high=end [OK]
Hint: Start low and mid at 0, high at last index [OK]
Common Mistakes:
Swapping high and mid initialization
Starting pointers at wrong indices
Using length instead of length-1 for high
3. What will be the output of the following code snippet?