Dutch National Flag Three Way Partition in DSA Typescript - 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 fast does the Dutch National Flag algorithm run when dividing the array into three groups?
Analyze the time complexity of the following code snippet.
function dutchNationalFlag(arr: number[], pivot: number): void {
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 rearranges the array so that all elements less than the pivot come first, then elements equal to the pivot, then elements greater than the pivot.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The while loop that moves the mid pointer through the array.
- How many times: Each element is visited at most once by mid, so the loop runs roughly n times where n is the 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 work grows linearly with input size; doubling the array roughly doubles the work.
Time Complexity: O(n)
This means the algorithm processes each element once, so the time grows directly with the number of elements.
[X] Wrong: "Because there are nested swaps, the time complexity must be quadratic O(n²)."
[OK] Correct: Each element is swapped at most once or twice, and the mid pointer moves forward without repeating elements, so the total steps stay linear.
Understanding this linear time partitioning is a great skill for sorting and searching problems, showing you can handle multiple groups efficiently.
"What if we changed the algorithm to use two passes instead of one? How would the time complexity change?"
Practice
Solution
Step 1: Understand the algorithm's purpose and partitioning criteria
The Dutch National Flag algorithm partitions an array into three parts based on a pivot value. It separates elements less than, equal to, and greater than the pivot.Final Answer:
To rearrange an array into three parts: less than, equal to, and greater than a pivot -> Option CQuick Check:
Partitioning = Rearranging into three parts [OK]
- Confusing it with full sorting algorithms
- Thinking it only separates two parts
- Assuming it finds max or min values
arr[i] and arr[j] in an array?Solution
Step 1: Understand swapping in TypeScript and verify correct code
TypeScript does not support tuple unpacking like Python, so use a temporary variable. const temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; ensures correct swap.Final Answer:
const temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; -> Option AQuick Check:
Temporary variable swap = const temp = arr[i]; arr[i] = arr[j]; arr[j] = temp; [OK]
- Using Python-style tuple unpacking syntax
- Overwriting one element before saving it
- Calling undefined swap functions
arr = [2, 0, 2, 1, 1, 0] with pivot = 1?Solution
Step 1: Partition elements and arrange array
Elements less than 1: 0, 0; equal to 1: 1, 1; greater than 1: 2, 2. Arrange as [less than] + [equal] + [greater than]: [0, 0, 1, 1, 2, 2].Final Answer:
[0, 0, 1, 1, 2, 2] -> Option DQuick Check:
Partitioned array = [0, 0, 1, 1, 2, 2] [OK]
- Not grouping equal elements in the middle
- Mixing order of groups
- Assuming full sort instead of partition
function dnfPartition(arr: number[], pivot: number): void {
let low = 0, mid = 0, high = arr.length - 1;
while (mid <= high) {
if (arr[mid] < pivot) {
const temp = arr[low]; arr[low] = arr[mid]; arr[mid] = temp;
low++;
} else if (arr[mid] === pivot) {
mid++;
} else {
const temp = arr[mid]; arr[mid] = arr[high]; arr[high] = temp;
high--;
}
}
}Solution
Step 1: Analyze the swap when arr[mid] < pivot and identify effect
After swapping arr[low] and arr[mid], low++ but missing mid++, causing the loop to recheck the same mid index, leading to infinite loop or wrong behavior.Final Answer:
Missing increment of mid after swapping when arr[mid] < pivot -> Option AQuick Check:
mid++ needed after swap when arr[mid] < pivot [OK]
- Forgetting to increment mid after swap
- Changing loop condition incorrectly
- Misusing comparison operators
arr = [3, 5, 2, 1, 4, 3, 2] and want to partition it using the Dutch National Flag algorithm with pivot = 3. After partitioning, which of the following is a valid output?Solution
Step 1: Identify groups based on pivot 3 and check options
Less than 3: 1, 2, 2; equal to 3: 3, 3; greater than 3: 4, 5. Valid output must have all less, then all equal, then all greater.
A: [1, 2, 2, 3, 3, 4, 5] valid.
B: equals first, invalid.
C: [2, 1, 3, 2, 3, 4, 5] has 2 (<3) after 3's, invalid.
D: greater before equals, invalid.Final Answer:
[1, 2, 2, 3, 3, 4, 5] -> Option BQuick Check:
Partition order: less, equal, greater [OK]
- Placing equal elements at wrong position
- Confusing full sort with partition
- Ignoring group order rules
Ready to prove it?
Pass the test and this pattern moves unknown
