Dutch National Flag Three Way Partition in DSA Javascript - Time & Space Complexity
We want to understand how the time needed to sort an array into three parts grows as the array gets bigger.
How does the number of steps change when we divide the array into three groups using the Dutch National Flag method?
Analyze the time complexity of the following code snippet.
function dutchFlagPartition(arr, pivot) {
let low = 0, mid = 0, high = arr.length - 1;
while (mid <= high) {
if (arr[mid] < pivot) {
[arr[low], arr[mid]] = [arr[mid], arr[low]];
low++; mid++;
} else if (arr[mid] > pivot) {
[arr[mid], arr[high]] = [arr[high], arr[mid]];
high--;
} else {
mid++;
}
}
}
This code sorts the array into three parts: less than pivot, equal to pivot, and greater than pivot, all in one pass.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The while loop that moves mid pointer through the array.
- How many times: Each element is visited at most once by mid, so up to n times where n is array length.
As the array size grows, the number of steps grows roughly the same amount.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 steps |
| 100 | About 100 steps |
| 1000 | About 1000 steps |
Pattern observation: The steps increase linearly with input size; doubling the array roughly doubles the work.
Time Complexity: O(n)
This means the time to complete the partition grows directly in proportion to the size of the array.
[X] Wrong: "Because there are swaps inside the loop, the time complexity must be quadratic O(n²)."
[OK] Correct: Each element is swapped at most once or twice, and the main loop moves forward without repeating elements, so the total steps stay linear.
Understanding this linear time partition helps you handle problems that require grouping or sorting with limited passes, a valuable skill in many coding challenges.
"What if we used two pointers instead of three? How would the time complexity change?"
Practice
Dutch National Flag Three Way Partition algorithm?Solution
Step 1: Understand the partition goal
The algorithm divides the array into three parts: less than pivot, equal to pivot, and greater than pivot.Step 2: Identify the rearrangement
It rearranges elements so that all smaller come first, then equal, then larger, without full sorting.Final Answer:
To rearrange an array so that all elements less than a pivot come first, then equal elements, then greater elements -> Option DQuick Check:
Partitioning = Rearranging by pivot [OK]
- Confusing partition with full sorting
- Thinking it only finds max or min
- Assuming it reverses the array
Solution
Step 1: Identify pointer roles
Low and mid start at 0, high at last index to cover full array.Step 2: Match initialization to algorithm
Correct initialization is low=0, mid=0, high=arr.length-1 to scan and partition properly.Final Answer:
let low = 0, mid = 0, high = arr.length - 1; -> Option AQuick Check:
Pointers start at array ends and middle [OK]
- Setting high to 0 instead of last index
- Starting mid at wrong position
- Using array length instead of length - 1
function dutchFlag(arr, pivot) {
let low = 0, mid = 0, high = arr.length - 1;
while (mid <= high) {
if (arr[mid] < pivot) {
[arr[low], arr[mid]] = [arr[mid], arr[low]];
low++; mid++;
} else if (arr[mid] === pivot) {
mid++;
} else {
[arr[mid], arr[high]] = [arr[high], arr[mid]];
high--;
}
}
return arr;
}
console.log(dutchFlag([2, 5, 1, 2, 3, 2, 4], 2));Solution
Step 1: Trace swaps for elements less than pivot
Elements less than 2 (which is 1) move to front; 1 swaps to index 0.Step 2: Place all 2s in middle and greater elements at end
All 2s stay in middle; 3,4,5 move to the right side in any order preserving partition.Final Answer:
[1, 2, 2, 2, 3, 4, 5] -> Option CQuick Check:
Partitioned array with less, equal, greater [OK]
- Not swapping correctly when element equals pivot
- Mixing order of greater elements
- Forgetting to increment mid after swap with low
function partition(arr, pivot) {
let low = 0, mid = 0, high = arr.length - 1;
while (mid < high) {
if (arr[mid] < pivot) {
[arr[low], arr[mid]] = [arr[mid], arr[low]];
low++;
mid++;
} else if (arr[mid] === pivot) {
mid++;
} else {
[arr[mid], arr[high]] = [arr[high], arr[mid]];
high--;
}
}
return arr;
}Solution
Step 1: Check loop condition correctness
The loop must run while mid is less than or equal to high to process all elements.Step 2: Identify impact of incorrect condition
Using mid < high skips processing when mid equals high, missing last element check.Final Answer:
The while loop condition should be mid <= high, not mid < high -> Option BQuick Check:
Loop must cover all elements [OK]
- Using mid < high causes last element to be skipped
- Incorrect pointer initialization
- Misunderstanding swap necessity for equal elements
[4, 9, 4, 7, 4, 8, 7] and pivot value 7, what will be the final array after applying the Dutch National Flag partition algorithm?Solution
Step 1: Partition elements less than pivot (7)
All 4s move to the front: [4, 4, 4, ...]Step 2: Place all 7s in the middle and greater elements at the end
7s stay in the middle; 8 and 9 move to the right side in sorted order.Final Answer:
[4, 4, 4, 7, 7, 8, 9] -> Option AQuick Check:
Less, equal, greater partitioned correctly [OK]
- Mixing order of greater elements
- Placing pivot elements incorrectly
- Not moving all less elements to front
Ready to prove it?
Pass the test and this pattern moves unknown
