We rearrange an array so that all elements less than a pivot come first, then elements equal to the pivot, then elements greater than the pivot.
Analogy: Imagine sorting colored balls into three bins: red balls go to the first bin, white balls to the second, and blue balls to the third, all in one pass without extra space.
Goal: Rearrange array so that all 0s come first, then all 1s, then all 2s
Step 1: mid=0, value=2 > pivot, swap with high=5 (value=0), decrement high
[0, 0, 2, 1, 1, 2]
low=0, mid=0, high=4
Why: Move larger elements to the end to separate them
Step 2: mid=0, value=0 < pivot, swap with low=0, increment low and mid
[0, 0, 2, 1, 1, 2]
low=1, mid=1, high=4
Why: Move smaller elements to the front
Step 3: mid=1, value=0 < pivot, swap with low=1, increment low and mid
[0, 0, 2, 1, 1, 2]
low=2, mid=2, high=4
Why: Continue moving smaller elements to the front
Step 4: mid=2, value=2 > pivot, swap with high=4 (value=1), decrement high
[0, 0, 1, 1, 2, 2]
low=2, mid=2, high=3
Why: Move larger elements to the end
Step 5: mid=2, value=1 == pivot, increment mid
[0, 0, 1, 1, 2, 2]
low=2, mid=3, high=3
Why: Keep equal elements in the middle
Step 6: mid=3, value=1 == pivot, increment mid
[0, 0, 1, 1, 2, 2]
low=2, mid=4, high=3
Why: All elements processed, done
Result:
Final array: [0, 0, 1, 1, 2, 2]
Annotated Code
DSA Java
public class DutchNationalFlag {
public static void threeWayPartition(int[] arr, int pivot) {
int low = 0, mid = 0, high = arr.length - 1;
while (mid <= high) {
if (arr[mid] < pivot) {
int temp = arr[low];
arr[low] = arr[mid];
arr[mid] = temp;
low++;
mid++;
} elseif (arr[mid] == pivot) {
mid++;
} else { // arr[mid] > pivot
int temp = arr[mid];
arr[mid] = arr[high];
arr[high] = temp;
high--;
}
}
}
public static void main(String[] args) {
int[] arr = {2, 0, 2, 1, 1, 0};
int pivot = 1;
threeWayPartition(arr, pivot);
for (int num : arr) {
System.out.print(num + " ");
}
System.out.println();
}
}
while (mid <= high) {
loop until mid passes high to process all elements
if (arr[mid] < pivot) {
if current element is smaller than pivot, swap to front
low++; mid++;
advance low and mid pointers after placing smaller element
else if (arr[mid] == pivot) {
if equal to pivot, just move mid forward
mid++;
advance mid pointer to keep equal elements in middle
else { // arr[mid] > pivot
if greater than pivot, swap with high and decrement high
high--;
decrement high pointer after moving larger element to end
OutputSuccess
0 0 1 1 2 2
Complexity Analysis
Time: O(n) because we scan the array once with mid pointer
Space: O(1) because swaps happen in place without extra storage
vs Alternative: Compared to sorting the whole array O(n log n), this is faster and uses no extra space
Edge Cases
empty array
function completes immediately without changes
DSA Java
while (mid <= high) {
all elements equal to pivot
mid pointer moves through all elements without swaps
DSA Java
elseif (arr[mid] == pivot) {
no elements less than pivot
low pointer stays at 0, swaps only move larger elements to end
DSA Java
if (arr[mid] < pivot) {
When to Use This Pattern
When you need to separate an array into three groups around a pivot in one pass, use Dutch National Flag partition to do it efficiently.
Common Mistakes
Mistake: Incrementing mid pointer after swapping with high without checking the swapped element Fix: Do not increment mid after swapping with high because the swapped element needs to be checked
Summary
Rearranges array into three parts: less than, equal to, and greater than a pivot.
Use when you want to partition data quickly without full sorting.
The key is to use three pointers and swap elements in one pass without extra space.
Practice
(1/5)
1. What is the main goal of the Dutch National Flag Three Way Partition algorithm?
easy
A. To reverse the elements of an array
B. To rearrange an array into three parts based on a pivot value
C. To find the maximum element in an array
D. To sort an array in ascending order
Solution
Step 1: Understand the algorithm's purpose
The Dutch National Flag algorithm divides an array into three parts: elements less than, equal to, and greater than a pivot.
Step 2: Compare with other options
Sorting or finding max are different tasks; reversing is unrelated.
Final Answer:
To rearrange an array into three parts based on a pivot value -> Option B
Quick Check:
Partitioning by pivot = D [OK]
Hint: Remember: It partitions into three groups around a pivot [OK]
Common Mistakes:
Confusing partitioning with sorting
Thinking it finds max or min values
Assuming it reverses the array
2. Which of the following is the correct Java syntax to swap two elements arr[i] and arr[j] in an integer array?
easy
A. int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp;
B. arr[i] = arr[j]; arr[j] = arr[i];
C. swap(arr[i], arr[j]);
D. int temp = arr[i]; arr[i] = arr[j]; arr[j] = arr[i];
Solution
Step 1: Understand swapping logic
Swapping requires a temporary variable to hold one value while assigning the other.
Step 2: Check each option
swap(arr[i], arr[j]); assumes a non-existent swap function; arr[i] = arr[j]; arr[j] = arr[i]; overwrites arr[i] without saving; int temp = arr[i]; arr[i] = arr[j]; arr[j] = arr[i]; loses original arr[i] value by assigning arr[j] = arr[i] after changing arr[i]; int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; correctly uses temp.
Final Answer:
int temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; -> Option A
Quick Check:
Use temp variable for swap = C [OK]
Hint: Use a temp variable to swap two array elements safely [OK]
Common Mistakes:
Overwriting one element before saving it
Using a non-existent swap function
Swapping without a temporary variable
3. What will be the output array after applying the Dutch National Flag algorithm on arr = [2, 0, 2, 1, 1, 0] with pivot = 1?
medium
A. [0, 1, 2, 0, 1, 2]
B. [2, 2, 1, 1, 0, 0]
C. [1, 1, 0, 0, 2, 2]
D. [0, 0, 1, 1, 2, 2]
Solution
Step 1: Partition array by pivot 1
Elements less than 1: 0,0; equal to 1: 1,1; greater than 1: 2,2.
Step 2: Arrange in order less, equal, greater
So the array becomes [0, 0, 1, 1, 2, 2].
Final Answer:
[0, 0, 1, 1, 2, 2] -> Option D
Quick Check:
Partitioned array = A [OK]
Hint: Group elements less, equal, then greater than pivot [OK]
Common Mistakes:
Not grouping equal elements in the middle
Mixing order of groups
Ignoring pivot value placement
4. Identify the error in this Java code snippet for Dutch National Flag partition:
int low = 0, mid = 0, high = arr.length - 1;
while (mid <= high) {
if (arr[mid] < pivot) {
swap(arr[low], arr[mid]);
low++;
mid++;
} else if (arr[mid] == pivot) {
mid++;
} else {
swap(arr[mid], arr[high]);
high--;
}
}
medium
A. The pivot comparison should use > instead of <
B. The loop condition should be mid < high
C. swap function is called incorrectly with array elements instead of indices
D. The variables low, mid, high are not initialized properly
Solution
Step 1: Check swap function usage
swap(arr[low], arr[mid]) passes values, not indices; swap needs indices to swap elements in array.
Step 2: Verify other parts
Loop condition and variable initialization are correct; pivot comparisons are standard.
Final Answer:
swap function is called incorrectly with array elements instead of indices -> Option C
Quick Check:
Swap needs indices, not values = A [OK]
Hint: Swap array elements by indices, not by passing values [OK]
Common Mistakes:
Passing values instead of indices to swap
Wrong loop boundary condition
Incorrect pivot comparison operators
5. Given an array arr = [3, 5, 2, 1, 4, 3, 2] and pivot = 3, how many swaps will the Dutch National Flag algorithm perform to partition the array?