Home DSA Dutch National Flag Algorithm Explained — Sort 3 Values in One Pass

Dutch National Flag Algorithm Explained — Sort 3 Values in One Pass

In Plain English 🔥
Imagine you have a pile of red, white, and blue marbles all mixed together. You want to sort them so all reds are on the left, all whites are in the middle, and all blues are on the right — but you only want to touch each marble once. The Dutch National Flag algorithm is exactly that sorting strategy, named after the three-coloured flag of the Netherlands. It uses three 'hands' that crawl toward each other, swapping marbles into the right zone as they go.
⚡ Quick Answer
Imagine you have a pile of red, white, and blue marbles all mixed together. You want to sort them so all reds are on the left, all whites are in the middle, and all blues are on the right — but you only want to touch each marble once. The Dutch National Flag algorithm is exactly that sorting strategy, named after the three-coloured flag of the Netherlands. It uses three 'hands' that crawl toward each other, swapping marbles into the right zone as they go.

Most sorting problems give you a free pass to reach for Arrays.sort() and move on. But every so often you hit a constraint that makes the generic approach feel wasteful: you only have three distinct values, you can't afford extra memory, and the input could be a million elements long. This is the exact situation the Dutch National Flag algorithm was designed for — and interviewers love it precisely because it rewards people who think about constraints before they think about code.

The classic version of this problem is LeetCode 75 'Sort Colors', where the values are 0, 1, and 2 representing red, white, and blue. A naive two-pass counting sort works, but it reads the array twice. A comparison sort is O(n log n). The Dutch National Flag solves it in a single pass — O(n) time, O(1) space — by maintaining three pointers that carve the array into four live regions simultaneously as they converge.

By the end of this article you'll understand exactly why three pointers are needed (not two, not four), you'll be able to draw the invariant on a whiteboard, write the algorithm from scratch without looking anything up, spot the two bugs that trip up nearly everyone on their first attempt, and answer every follow-up an interviewer can throw at you.

What is Dutch National Flag Algorithm?

Dutch National Flag Algorithm is a core concept in DSA. Rather than starting with a dry definition, let's see it in action and understand why it exists.

ForgeExample.java · DSA
12345678
// TheCodeForgeDutch National Flag Algorithm example
// Always use meaningful names, not x or n
public class ForgeExample {
    public static void main(String[] args) {
        String topic = "Dutch National Flag Algorithm";
        System.out.println("Learning: " + topic + " 🔥");
    }
}
▶ Output
Learning: Dutch National Flag Algorithm 🔥
🔥
Forge Tip: Type this code yourself rather than copy-pasting. The muscle memory of writing it will help it stick.
ConceptUse CaseExample
Dutch National Flag AlgorithmCore usageSee code above

🎯 Key Takeaways

  • You now understand what Dutch National Flag Algorithm is and why it exists
  • You've seen it working in a real runnable example
  • Practice daily — the forge only works when it's hot 🔥

⚠ Common Mistakes to Avoid

  • Memorising syntax before understanding the concept
  • Skipping practice and only reading theory

Frequently Asked Questions

What is Dutch National Flag Algorithm in simple terms?

Dutch National Flag Algorithm is a fundamental concept in DSA. Think of it as a tool — once you understand its purpose, you'll reach for it constantly.

🔥
TheCodeForge Editorial Team Verified Author

Written and reviewed by senior developers with real-world experience across enterprise, startup and open-source projects. Every article on TheCodeForge is written to be clear, accurate and genuinely useful — not just SEO filler.

← PreviousKadane's AlgorithmNext →String Manipulation Patterns
Forged with 🔥 at TheCodeForge.io — Where Developers Are Forged