π― Dutch National Flag Algorithm: The Ultimate Sorting Technique for Three-Way Partitioning

Table of contents
- DNF Intro
- π What is the Dutch National Flag Algorithm?
- π Problem Statement
- π§ Intuition Behind the Algorithm
- π Naive / Brute-Force Approach
- π Optimized Approach: Dutch National Flag (In-place)
- π Visual Representation
- π Related Problems
- π§ Real-World Applications
- π Open Source Contribution
- βοΈ Final Words

DNF Intro
Sorting problems are everywhere in the world of DSA. One common category is sorting arrays with only a few distinct elements. Thatβs where the Dutch National Flag Algorithm shines!
This post is a complete guide to mastering this algorithm β packed with examples, code, visual intuition, and real-world use cases. Whether you're preparing for your next tech interview or contributing to open source, this guide has you covered.
π What is the Dutch National Flag Algorithm?
The Dutch National Flag Algorithm (DNF) was proposed by Edsger Dijkstra. It solves the problem of sorting an array containing only three types of elements, such as:
0s, 1s, and 2s
red, white, and blue
low, medium, and high
The name "Dutch National Flag" comes from the three-color flag of the Netherlands (red, white, and blue).
π Problem Statement
Given an array nums
consisting of only 0s, 1s, and 2s, sort the array in linear time and constant space without using any library sort function.
cppCopyEditInput: nums = [2, 0, 2, 1, 1, 0]
Output: [0, 0, 1, 1, 2, 2]
π§ Intuition Behind the Algorithm
We want to partition the array into three parts:
All 0s on the left
All 1s in the middle
All 2s on the right
We use three pointers:
low
β position for next 0mid
β current elementhigh
β position for next 2
Based on the value at nums[mid]
, we take the following actions:
If
nums[mid] == 0
, swap withnums[low]
and move bothlow
andmid
forward.If
nums[mid] == 1
, move onlymid
forward.If
nums[mid] == 2
, swap withnums[high]
and movehigh
backward.
π Naive / Brute-Force Approach
πΈ Count the frequency of 0s, 1s, and 2s
πΈ Overwrite the array in that order
cppCopyEditvoid sortColors(vector<int>& nums) {
int count0 = 0, count1 = 0, count2 = 0;
for (int num : nums) {
if (num == 0) count0++;
else if (num == 1) count1++;
else count2++;
}
for (int i = 0; i < count0; i++) nums[i] = 0;
for (int i = count0; i < count0 + count1; i++) nums[i] = 1;
for (int i = count0 + count1; i < nums.size(); i++) nums[i] = 2;
}
β± Time Complexity: O(N)
π§ Space Complexity: O(1)
Not in-place optimized. Requires multiple passes.
π Optimized Approach: Dutch National Flag (In-place)
cppCopyEditvoid sortColors(vector<int>& nums) {
int low = 0, mid = 0;
int high = nums.size() - 1;
while (mid <= high) {
if (nums[mid] == 0) {
swap(nums[low++], nums[mid++]);
} else if (nums[mid] == 1) {
mid++;
} else {
swap(nums[mid], nums[high--]);
}
}
}
β± Time Complexity: O(N)
π§ Space Complexity: O(1)
βοΈ In-place, one-pass
π Visual Representation
Imagine three buckets:
csharpCopyEditlow mid high
β β β
[0s | 1s | unsorted | 2s]
The mid pointer decides which element goes to which bucket (left for 0s, right for 2s, center for 1s).
π Related Problems
Sort Colors (Leetcode 75)
Sort Array by Parity
Wiggle Sort
Three-way Partitioning (GFG)
π§ Real-World Applications
Sorting arrays with limited distinct elements
Segregating low/medium/high priority tasks
Preprocessing buckets in radix/bucket sort
π Open Source Contribution
π» I'm building an open-source DSA in C++ GitHub Repository.
This includes categorized folders like Arrays β Dutch National Flag β Code files.
Feel free to contribute and explore!
π Let's grow and learn together through Open Source.
βοΈ Final Words
The Dutch National Flag Algorithm is a powerful tool in your DSA toolbox β simple yet elegant. Understanding its in-place partitioning logic is essential for cracking a wide range of interview questions.
π Happy Coding & Keep Solving!
π§ Follow me for more algorithm deep dives!
#DSA #Algorithms #Coding #Cpp #InterviewPrep #TechBlog
#DataStructuresAndAlgorithms #LeetCode #OpenSource #Hashnode
#DevCommunity #Programming #SoftwareEngineering #SortingAlgorithms
#DutchNationalFlag #CodeNewbie #100DaysOfCode #LearnToCode
Subscribe to my newsletter
Read articles from Muhammad Umair Ullah directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
