Dutch National Flag Three Way Partition in DSA Java - Time & Space Complexity
We want to understand how the time needed to sort an array into three parts grows as the array gets bigger.
Specifically, how many steps does the Dutch National Flag algorithm take as input size increases?
Analyze the time complexity of the following code snippet.
public void threeWayPartition(int[] arr, int lowVal, int highVal) {
int start = 0, end = arr.length - 1;
int i = 0;
while (i <= end) {
if (arr[i] < lowVal) {
int temp = arr[start];
arr[start] = arr[i];
arr[i] = temp;
start++;
i++;
} else if (arr[i] > highVal) {
int temp = arr[end];
arr[end] = arr[i];
arr[i] = temp;
end--;
} else {
i++;
}
}
}
This code sorts the array into three parts: less than lowVal, between lowVal and highVal, and greater than highVal.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The while loop that moves through the array elements.
- How many times: Each element is visited at most once or twice, but the loop runs until the index crosses the end pointer.
As the array size grows, the number of steps grows roughly in direct proportion to the number of elements.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 to 20 steps |
| 100 | About 100 to 200 steps |
| 1000 | About 1000 to 2000 steps |
Pattern observation: The steps grow roughly linearly with input size, sometimes a bit more due to swaps, but never more than a constant factor times n.
Time Complexity: O(n)
This means the time to complete the partition grows in a straight line as the array gets bigger.
[X] Wrong: "Because there are nested ifs and swaps, the time must be quadratic like O(n²)."
[OK] Correct: The loop moves forward or backward carefully so each element is handled only a few times, not repeatedly nested.
Knowing this linear time partition helps you solve many sorting and grouping problems efficiently, a skill valued in coding challenges and interviews.
"What if we used a simple for-loop without adjusting the end pointer when swapping? How would the time complexity change?"
Practice
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 BQuick Check:
Partitioning by pivot = D [OK]
- Confusing partitioning with sorting
- Thinking it finds max or min values
- Assuming it reverses the array
arr[i] and arr[j] in an integer array?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 AQuick Check:
Use temp variable for swap = C [OK]
- Overwriting one element before saving it
- Using a non-existent swap function
- Swapping without a temporary variable
arr = [2, 0, 2, 1, 1, 0] with pivot = 1?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 DQuick Check:
Partitioned array = A [OK]
- Not grouping equal elements in the middle
- Mixing order of groups
- Ignoring pivot value placement
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--;
}
}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 CQuick Check:
Swap needs indices, not values = A [OK]
- Passing values instead of indices to swap
- Wrong loop boundary condition
- Incorrect pivot comparison operators
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?Solution
Step 1: Trace swaps during partition
Initial: low=0, mid=0, high=6 - mid=0 (3 == pivot): mid++ (0 swaps) - mid=1 (5 > pivot): swap arr[mid] and arr[high] -> swap arr[1] and arr[6] (swap 1), high--=5 - mid=1 (now 2 < pivot): swap arr[low] and arr[mid] -> swap arr[0] and arr[1] (swap 2), low++=1, mid++=2 - mid=2 (2 < pivot): swap arr[low] and arr[mid] -> swap arr[1] and arr[2] (swap 3), low++=2, mid++=3 - mid=3 (1 < pivot): swap arr[low] and arr[mid] -> swap arr[2] and arr[3] (swap 4), low++=3, mid++=4 - mid=4 (4 > pivot): swap arr[mid] and arr[high] -> swap arr[4] and arr[5] (swap 5), high--=4 - mid=4 (now 3 == pivot): mid++=5 Loop ends as mid > high.Step 2: Count total swaps
Total swaps counted = 5.Final Answer:
5 -> Option AQuick Check:
Count swaps carefully during partition = B [OK]
- Counting swaps incorrectly when pivot equals element
- Ignoring swaps after high pointer moves
- Assuming one swap per iteration
Ready to prove it?
Pass the test and this pattern moves unknown
