Bird
0
0
DSA Cprogramming

Dutch National Flag Three Way Partition in DSA C

Choose your learning style9 modes available
Mental Model
We rearrange an array into three parts: less than pivot, equal to pivot, and greater than pivot, all in one pass.
Analogy: Imagine sorting colored balls into three bins: red, white, and blue. You pick each ball once and put it in the correct bin without extra space.
[less] ← i -> [equal] ← j -> [unknown] ← k -> [greater]
Array: 2 0 2 1 1 0
Pointers: low, mid, high
Dry Run Walkthrough
Input: array: [2, 0, 2, 1, 1, 0], pivot = 1
Goal: Rearrange array so that all 0s come first, then all 1s, then all 2s
Step 1: Check element at mid=0 (value 2), swap with element at high=5 (value 0), decrease high
Array: [0, 0, 2, 1, 1, 2]
low=0, mid=0, high=4
Why: 2 is greater than pivot, so move it to the end
Step 2: Check element at mid=0 (value 0), swap with element at low=0 (same), increase low and mid
Array: [0, 0, 2, 1, 1, 2]
low=1, mid=1, high=4
Why: 0 is less than pivot, so move it to the front
Step 3: Check element at mid=1 (value 0), swap with element at low=1 (same), increase low and mid
Array: [0, 0, 2, 1, 1, 2]
low=2, mid=2, high=4
Why: 0 is less than pivot, so move it to the front
Step 4: Check element at mid=2 (value 2), swap with element at high=4 (value 1), decrease high
Array: [0, 0, 1, 1, 2, 2]
low=2, mid=2, high=3
Why: 2 is greater than pivot, so move it to the end
Step 5: Check element at mid=2 (value 1), equal to pivot, increase mid
Array: [0, 0, 1, 1, 2, 2]
low=2, mid=3, high=3
Why: 1 is equal to pivot, so keep it in the middle
Step 6: Check element at mid=3 (value 1), equal to pivot, increase mid
Array: [0, 0, 1, 1, 2, 2]
low=2, mid=4, high=3
Why: 1 is equal to pivot, so keep it in the middle
Result:
Array: [0, 0, 1, 1, 2, 2]
Annotated Code
DSA C
#include <stdio.h>

void dutch_national_flag_partition(int arr[], int size, int pivot) {
    int low = 0, mid = 0, high = size - 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 { // arr[mid] > pivot
            int temp = arr[mid];
            arr[mid] = arr[high];
            arr[high] = temp;
            high--;
        }
    }
}

void print_array(int arr[], int size) {
    for (int i = 0; i < size; i++) {
        printf("%d", arr[i]);
        if (i != size - 1) printf(" -> ");
    }
    printf(" -> null\n");
}

int main() {
    int arr[] = {2, 0, 2, 1, 1, 0};
    int size = sizeof(arr) / sizeof(arr[0]);
    int pivot = 1;
    dutch_national_flag_partition(arr, size, pivot);
    print_array(arr, size);
    return 0;
}
while (mid <= high) {
continue processing until mid passes high
if (arr[mid] < pivot) {
if current element less than pivot, swap with low and move low and mid forward
int temp = arr[low]; arr[low] = arr[mid]; arr[mid] = temp; low++; mid++;
swap to move smaller element to front and advance pointers
else if (arr[mid] == pivot) { mid++; }
if equal to pivot, just move mid forward
else { int temp = arr[mid]; arr[mid] = arr[high]; arr[high] = temp; high--; }
if greater than pivot, swap with high and decrease high pointer
OutputSuccess
0 -> 0 -> 1 -> 1 -> 2 -> 2 -> null
Complexity Analysis
Time: O(n) because we scan the array once with mid pointer moving forward
Space: O(1) because partitioning is done in place without extra storage
vs Alternative: Compared to sorting the whole array O(n log n), this is faster and uses no extra space
Edge Cases
empty array
function does nothing and array remains empty
DSA C
while (mid <= high) {
all elements equal to pivot
mid moves through entire array without swaps, array unchanged
DSA C
else if (arr[mid] == pivot) { mid++; }
array with only elements less than pivot
all elements swapped to front, effectively unchanged order
DSA C
if (arr[mid] < pivot) {
array with only elements greater than pivot
all elements swapped to end, order reversed among them
DSA C
else { int temp = arr[mid]; arr[mid] = arr[high]; arr[high] = temp; high--; }
When to Use This Pattern
When you need to separate an array into three groups around a pivot in one pass, use Dutch National Flag partition to do it efficiently.
Common Mistakes
Mistake: Incrementing mid pointer after swapping with high pointer
Fix: Do not increment mid after swapping with high because the swapped element needs to be checked
Mistake: Using two pointers instead of three, missing the equal group
Fix: Use three pointers: low, mid, and high to separate less, equal, and greater groups
Summary
Rearranges array into three parts: less than, equal to, and greater than a pivot value.
Use when you want to partition data into three categories efficiently in one pass.
The key is to use three pointers to track boundaries and swap elements accordingly without extra space.