Dutch National Flag Three Way Partition
📖 Scenario: You are organizing colored balls represented by numbers 0, 1, and 2 in a line. You want to group all balls of the same color together in order: all 0s first, then all 1s, and finally all 2s.
🎯 Goal: Build a program that rearranges a list of numbers containing only 0s, 1s, and 2s so that all 0s come first, then all 1s, and then all 2s. This is called the Dutch National Flag problem.
📋 What You'll Learn
Create a list called
balls with the exact values: [2, 0, 2, 1, 1, 0]Create three variables called
low, mid, and high with the exact values: 0, 0, and len(balls) - 1 respectivelyUse a
while loop with the condition mid <= high to rearrange the list using the Dutch National Flag algorithmPrint the
balls list after rearrangement💡 Why This Matters
🌍 Real World
Sorting colored objects or grouping items by categories quickly without using extra space.
💼 Career
Understanding this algorithm helps in coding interviews and problems involving sorting or partitioning data efficiently.
Progress0 / 4 steps