Dutch National Flag Three Way Partition in DSA C - Time & Space Complexity
We want to know how the time taken by the Dutch National Flag algorithm changes as the input size grows.
Specifically, how many steps does it take to rearrange the array into three parts?
Analyze the time complexity of the following code snippet.
void dutchFlagPartition(int arr[], int n, int pivot) {
int low = 0, mid = 0, high = n - 1;
while (mid <= high) {
if (arr[mid] < pivot) {
int temp = arr[low]; arr[low] = arr[mid]; arr[mid] = temp;
low++; mid++;
} else if (arr[mid] == pivot) {
mid++;
} else {
int temp = arr[mid]; arr[mid] = arr[high]; arr[high] = temp;
high--;
}
}
}
This code rearranges the array so that all elements less than pivot come first, then equal to pivot, then greater than pivot.
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 the mid pointer.
As the array size grows, the number of steps grows roughly the same as the number of elements.
| 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.
Time Complexity: O(n)
This means the time to rearrange grows directly in proportion to the number of elements.
[X] Wrong: "Because there are nested if-else checks inside the loop, the time complexity is O(n²)."
[OK] Correct: The loop runs only once through the array, and each element is handled a limited number of times, so the total steps grow linearly, not quadratically.
Understanding this linear time partitioning is a key skill for array problems and shows you can handle multiple conditions efficiently in one pass.
"What if we changed the algorithm to use two separate passes instead of one? How would the time complexity change?"
Practice
Solution
Step 1: Understand the partitioning goal
The algorithm divides the array into three parts based on a pivot value: elements less than pivot, equal to pivot, and greater than pivot.Step 2: Compare with other options
Options A, C, and D describe different operations: sorting, finding max, and reversing, which are not the main goal here.Final Answer:
To rearrange an array into three parts: less than, equal to, and greater than a pivot -> Option BQuick Check:
Partitioning = three parts [OK]
- Confusing with full sorting algorithms
- Thinking it only separates two parts
- Assuming it finds max or min values
Solution
Step 1: Understand pointer initialization
In the algorithm, low and mid start at 0, and high starts at n-1 (last index).Step 2: Check each option
int low = 0, mid = 0, high = n - 1; correctly initializes low and mid to 0, high to n-1. Others have incorrect initial values.Final Answer:
int low = 0, mid = 0, high = n - 1; -> Option AQuick Check:
Pointers start: low=0, mid=0, high=n-1 [OK]
- Setting low or mid to n or n-1 incorrectly
- Not initializing all pointers
- Confusing variable assignments
arr = {2, 0, 2, 1, 1, 0} with pivot = 1?Solution
Step 1: Partition array around 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 AQuick Check:
Partitioned array = [0,0,1,1,2,2] [OK]
- Mixing order of groups
- Not grouping equal elements correctly
- Assuming full sorting instead of partitioning
void dnfPartition(int arr[], int n, int pivot) {
int low = 0, mid = 0, high = n - 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 usage
The code uses swap(arr[low], arr[mid]) but swap is not a built-in function in C for array elements.Step 2: Verify other parts
Loop condition and pointer initialization are correct. Pivot comparisons are correct as per algorithm.Final Answer:
swap function is not defined for array elements -> Option DQuick Check:
Missing swap definition = error [OK]
- Assuming swap is built-in for arrays in C
- Changing loop condition incorrectly
- Wrong pointer initialization
arr = {3, 5, 2, 3, 8, 3, 1, 3} 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 pointers: low=0, mid=0, high=7. - mid=0 (3 == pivot): mid++ (0 swaps) - mid=1 (5 > pivot): swap arr[mid] and arr[high] -> swap(5,3), high=6 (1 swap) - mid=1 (now 3 == pivot): mid++ (1 swap) - mid=2 (2 < pivot): swap arr[low] and arr[mid] -> swap(3,2), low=1, mid=3 (2 swaps) - mid=3 (3 == pivot): mid++ (2 swaps) - mid=4 (8 > pivot): swap arr[mid] and arr[high] -> swap(8,1), high=5 (3 swaps) - mid=4 (now 1 < pivot): swap arr[low] and arr[mid] -> swap(3,1), low=2, mid=5 (4 swaps) - mid=5 (3 == pivot): mid++ (4 swaps) Loop ends as mid=6 > high=5.Step 2: Count total swaps
Total swaps performed = 4Final Answer:
4 -> Option CQuick Check:
Count swaps carefully = 4 [OK]
- Counting swaps only when mid moves
- Ignoring swaps after swapping with high
- Miscounting swaps when elements equal pivot
Ready to prove it?
Pass the test and this pattern moves unknown
