Three-way partitioning algorithm to sort elements into three groups.
The Dutch National Flag problem, proposed by Edsger W. Dijkstra in his 1976 book "A Discipline of Programming," involves sorting an array of elements into three groups -- analogous to the three colored stripes (red, white, and blue) of the Dutch national flag. The algorithm partitions elements into low, middle, and high sections in a single pass using three pointers, achieving O(n) time with O(1) extra space.
Dijkstra introduced this problem as an exercise in program correctness and invariant-based reasoning. He was interested not just in the algorithm itself, but in demonstrating how to reason formally about programs. The problem became a classic example in computer science education for teaching loop invariants -- the idea that certain conditions remain true before and after each iteration of a loop. Dijkstra's formulation showed how maintaining precise invariants about the three pointers leads to an elegant and provably correct solution.
low pointer at the start, a mid pointer that scans through the array, and a high pointer at the end.low belong to the first group, elements between low and mid belong to the second group, elements after high belong to the third group, and elements between mid and high are unclassified.mid:
low and advance both low and mid.mid.high and decrement high. Do not advance mid, because the swapped element from high has not yet been examined.mid passes high, every element has been classified and the array is fully partitioned.The algorithm processes each element at most twice (once when examined, potentially once when swapped back), so it performs at most 2n operations total.
| Case | Time | Space |
|---|---|---|
| All | O(n) | O(1) |
The algorithm always runs in linear time regardless of input distribution, and it sorts in-place using only a constant number of extra variables for the three pointers and a temporary swap variable.
The Dutch National Flag algorithm is most prominently used as the partitioning scheme in three-way Quick Sort (also called fat partition Quick Sort). In standard Quick Sort, the partition step divides elements into two groups: less than the pivot and greater than the pivot. When the array contains many duplicate values, this two-way partition can degrade performance because equal elements end up on one side and must be further partitioned. Three-way partitioning using the Dutch National Flag algorithm divides the array into three groups: less than pivot, equal to pivot, and greater than pivot. All elements equal to the pivot are placed in their final positions in a single pass, and only the less-than and greater-than groups need further sorting. This optimization makes Quick Sort run in O(n) time on arrays where all elements are identical, rather than the O(n^2) that naive Quick Sort would require.
Beyond Quick Sort, the Dutch National Flag algorithm appears in many practical contexts. It solves the classic interview problem of sorting an array containing only 0s, 1s, and 2s in a single pass. It is useful in any scenario requiring three-way classification: separating negative numbers, zeros, and positive numbers; grouping items into low-priority, medium-priority, and high-priority categories; or partitioning data for parallel processing.
Use the Dutch National Flag algorithm whenever you need to partition elements into exactly three groups in linear time and constant space. It is essential for implementing efficient Quick Sort on data with many duplicates, and it serves as an elegant example of invariant-based algorithm design in computer science education.