The Dutch National Flag Algorithm, also known as the DNF algorithm or the ThreeWay Partitioning Algorithm, is a simple and efficient approach to sorting an array containing three distinct elements.This algorithm gained popularity for its elegant design and impressive time complexity, making it a valuable tool in various applications.
The Dutch National Flag Problem
The Dutch National Flag Problem, also known as the 3way partitioning problem, is a computational problem that involves sorting an array consisting of three unique elements. The problem is named after the Dutch national flag due to its resemblance to the three horizontal bands of colors on the Dutch flag: red, white, and blue.
It was devised by Edsger W. Dijkstra, a renowned Dutch computer scientist, and is named after the Dutch national flag due to its ability to sort elements into three partitions resembling the colors of the Dutch flag: red, white, and blue.
The problem statement is as follows: Given an array containing elements that can take three distinct values (for example, 0, 1, and 2), the task is to rearrange the elements in such a way that all occurrences of the first element come before all occurrences of the second element, which, in turn, come before all occurrences of the third element.
The goal is to solve this problem in linear time complexity, i.e., with a single pass through the array, and with constant space complexity, i.e., without using additional data structures proportional to the input size.
The Dutch National Flag Problem originated from the need to efficiently sort arrays in situations where there are multiple duplicate elements and a specific order needs to be maintained.
One classic application of this problem is sorting an array of 0s, 1s, and 2s, representing different colors or categories, where the desired output is an array with all 0s at the beginning, followed by all 1s, and finally, all 2s.
The Dutch National Flag Algorithm
The Dutch National Flag Algorithm is particularly useful when dealing with arrays that contain three unique elements that need to be sorted. The algorithm arranges the elements in a specific order: all occurrences of the first element (usually denoted as '0') come first, followed by all occurrences of the second element ('1'), and finally, all occurrences of the third element ('2').
The algorithm employs a twopointer approach, using three indices: low, mid, and high.
The low pointer keeps track of the boundary between the first and second partitions, the mid pointer represents the current element being inspected, and the high pointer separates the second and third partitions. By iterating through the array and swapping elements, the algorithm gradually partitions the array into the desired order.
Here is the stepbystep algorithm:
 Initialize low = 0, mid = 0, and high = n1, where n is the size of the array.
 While mid <= high, repeat steps 35.
 If the element at index mid is 0, swap it with the element at index low, increment low and mid by 1.
 If the element at index mid is 1, leave it in place, and increment mid by 1.
 If the element at index mid is 2, swap it with the element at index high, decrement high by 1.
 Repeat steps 25 until mid exceeds high.
Implementation in C++
Here's a C++ code implementation of the Dutch National Flag Algorithm to sort an array containing 0s, 1s, and 2s:
#include<iostream> using namespace std; void dutchFlagSort(int arr[], int n) { int low = 0; // Pointer for the region of 0s int mid = 0; // Pointer for the region of 1s int high = n  1; // Pointer for the unprocessed region while (mid <= high) { if (arr[mid] == 0) { swap(arr[low], arr[mid]); low++; mid++; } else if (arr[mid] == 1) { mid++; } else { // arr[mid] == 2 swap(arr[mid], arr[high]); high; } } } int main() { int arr[] = {1, 2, 0, 2, 1, 0, 1, 2, 0}; int n = sizeof(arr) / sizeof(arr[0]); cout << "Original Array: "; for (int i = 0; i < n; i++) { cout << arr[i] << " "; } cout << endl; dutchFlagSort(arr, n); cout << "Sorted Array: "; for (int i = 0; i < n; i++) { cout << arr[i] << " "; } cout << endl; return 0; }
Output:
Original Array: 1 2 0 2 1 0 1 2 0 Sorted Array: 0 0 0 1 1 1 2 2 2
In this code, we define the dutchFlagSort
a function that takes an array (arr
) and its size (n
) as parameters. The function uses threepointers (low
, mid
, and high
) to partition the array while iterating through it.
It swaps elements accordingly based on their values: if the element is 0, it is swapped with the element at the low
index and both low
and mid
are incremented; if the element is 1, mid
is incremented; if the element is 2, it is swapped with the element at the high
index, and high
is decremented. The process continues until mid
exceeds high
.
In the main
function, we demonstrate the usage of the dutchFlagSort
function by creating an array of integers. We print the original array, call the dutchFlagSort
function to sort it, and then print the sorted array.
Running this code will output the original array followed by the sorted array, demonstrating the effectiveness of the Dutch National Flag Algorithm in sorting an array of 0s, 1s, and 2s.
Time and space Complexity
The time complexity of the Dutch National Flag Algorithm is O(n), where n is the size of the input array. This is because the algorithm performs a single pass through the array, examining each element once. The number of operations grows linearly with the size of the input, making it highly efficient for large arrays.
The space complexity of the algorithm is O(1), meaning it uses constant space regardless of the input size. The algorithm only requires a few additional integer variables (low
, mid
, and high
) to keep track of the pointers during the partitioning process. It performs the sorting in place, modifying the original array without using any additional data structures proportional to the input size.
Applications
The Dutch National Flag Algorithm has proven to be a versatile tool with numerous applications in various fields, including:

Sorting: The algorithm is primarily used for sorting arrays that contain three distinct elements. It efficiently rearranges the elements into the desired order, allowing subsequent algorithms or processes to operate on the sorted data more effectively.

Partitioning: The DNF algorithm is useful for partitioning data based on specific criteria. For instance, it can be employed to partition an array of students based on their grades, separating them into categories such as "fail," "pass," and "excellent."

Data Cleaning: In data preprocessing tasks, the algorithm can assist in cleaning and organizing datasets. It can be used to separate and group data points based on specific attributes or features.

String Manipulation: The algorithm can be adapted to handle string manipulation tasks where three distinct patterns or characters need to be ordered. It enables efficient rearrangement of strings or substrings based on specific conditions.
Conclusion
The Dutch National Flag Algorithm, devised by Edsger W. Dijkstra, offers an elegant and efficient approach to sorting arrays containing three unique elements. With its linear time complexity and minimal space requirements, it has become a valuable tool in various applications ranging from sorting and partition