Dutch National Flag Three Way Partition in DSA C++ - 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 does the number of steps change when we separate values into less than, equal to, and greater than a pivot?
Analyze the time complexity of the following code snippet.
void dutchFlagPartition(int pivot, std::vector<int>& arr) {
int low = 0, mid = 0, high = arr.size() - 1;
while (mid <= high) {
if (arr[mid] < pivot) {
std::swap(arr[low++], arr[mid++]);
} else if (arr[mid] == pivot) {
mid++;
} else {
std::swap(arr[mid], arr[high--]);
}
}
}
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 through the array with the mid pointer.
- How many times: Each element is visited at most once by mid, so the loop runs roughly n times for an array of size n.
As the array size grows, the number of steps grows roughly the same amount because each element is checked once.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 checks and swaps |
| 100 | About 100 checks and swaps |
| 1000 | About 1000 checks and swaps |
Pattern observation: The work grows linearly with the input size.
Time Complexity: O(n)
This means the time to complete the partition grows directly in proportion to the number of elements.
[X] Wrong: "The algorithm might check some elements multiple times, so it could be slower than linear."
[OK] Correct: Each element is moved or checked at most once, so the loop runs only about n times, keeping it efficient.
Understanding this partitioning method shows you can handle in-place sorting and smart pointer movement, skills useful in many coding problems.
"What if we changed the pivot to be the median of the array instead of a fixed value? How would the time complexity change?"
Practice
Solution
Step 1: Understand the partitioning goal
The algorithm divides the array into three parts: less than pivot, equal to pivot, and greater than pivot.Step 2: Recognize the rearrangement purpose
This helps in sorting or grouping elements efficiently without full sorting.Final Answer:
To rearrange an array so that all elements less than a pivot come first, then equal, then greater -> Option AQuick 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 initialization
In the algorithm, low and mid start at 0, high starts at n-1 (last index).Step 2: Match correct initialization
int low = 0, mid = 0, high = n - 1; correctly initializes low and mid to 0, high to n-1.Final Answer:
int low = 0, mid = 0, high = n - 1; -> Option CQuick Check:
Pointers start: low=0, mid=0, high=n-1 [OK]
- Initializing pointers incorrectly
- Setting high to 0 instead of n-1
- Not initializing mid at 0
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: Rearrange array accordingly
Place all 0s first, then 1s, then 2s resulting in [0, 0, 1, 1, 2, 2].Final Answer:
[0, 0, 1, 1, 2, 2] -> Option BQuick Check:
Less, equal, greater order = [0,0,1,1,2,2] [OK]
- Not grouping equal elements in the middle
- Mixing order of groups
- Assuming full sort instead of partition
int low = 0, mid = 0, high = n - 1;
while (mid <= high) {
if (arr[mid] < pivot) {
swap(arr[low], arr[mid]);
low++;
} else if (arr[mid] == pivot) {
mid++;
} else {
swap(arr[mid], arr[high]);
high--;
}
}Solution
Step 1: Analyze pointer increments
When arr[mid] < pivot, low is incremented but mid is not incremented, causing infinite loop.Step 2: Identify missing mid increment
Mid must also increment after swapping to move forward.Final Answer:
Missing increment of mid when arr[mid] < pivot -> Option DQuick Check:
Increment mid after swap for less than pivot [OK]
- Forgetting to increment mid after swap with low
- Changing loop condition incorrectly
- Ignoring pivot initialization
arr = [3, 5, 2, 3, 8, 3, 1] and want to partition it using the Dutch National Flag algorithm with pivot = 3. Which of the following is the correct final partitioned array?Solution
Step 1: Group elements by pivot 3
Less than 3: 1, 2; equal to 3: 3, 3, 3; greater than 3: 5, 8.Step 2: Arrange groups in order
Place less first, then equal, then greater: [1, 2, 3, 3, 3, 5, 8].Step 3: Check options for exact order
[1, 2, 3, 3, 3, 5, 8] matches the correct partition with greater elements 5 and 8 in any order, which is valid partitioning.Final Answer:
[1, 2, 3, 3, 3, 5, 8] -> Option AQuick Check:
Partition order less, equal, greater = [1,2,3,3,3,5,8] [OK]
- Assuming full sort instead of partition
- Mixing order of greater elements
- Placing equal elements incorrectly
Ready to prove it?
Pass the test and this pattern moves unknown
