Bird
0
0
DSA Cprogramming~10 mins

Dutch National Flag Three Way Partition in DSA C - Execution Trace

Choose your learning style9 modes available
Concept Flow - Dutch National Flag Three Way Partition
Initialize pointers: low=0, mid=0, high=n-1
Check mid <= high?
NoDone
Yes
Inspect arr[mid
Swap arr[low
mid++
Swap arr[mid
Repeat loop
We use three pointers low, mid, and high to rearrange the array in one pass by swapping elements based on their value compared to the pivot.
Execution Sample
DSA C
void dutch_flag_partition(int arr[], int n) {
  int low = 0, mid = 0, high = n - 1;
  while (mid <= high) {
    if (arr[mid] == 0) swap(arr[low++], arr[mid++]);
    else if (arr[mid] == 1) mid++;
    else swap(arr[mid], arr[high--]);
  }
}
This code partitions an array containing 0s, 1s, and 2s so that all 0s come first, then 1s, then 2s.
Execution Table
StepOperationlowmidhighArray StatePointer Changes
1Start008[2, 0, 2, 1, 1, 0, 0, 2, 1]Initial pointers set
2Check arr[mid]=2, swap arr[mid] and arr[high]007[1, 0, 2, 1, 1, 0, 0, 2, 2]Swap arr[0]<->arr[8], high=7
3Check arr[mid]=0, swap arr[low] and arr[mid]007[1, 0, 2, 1, 1, 0, 0, 2, 2]No swap, mid=1
4Check arr[mid]=0, swap arr[low] and arr[mid]017[0, 1, 2, 1, 1, 0, 0, 2, 2]Swap arr[0]<->arr[1], low=1, mid=2
5Check arr[mid]=2, swap arr[mid] and arr[high]127[0, 1, 2, 1, 1, 0, 0, 2, 2]Swap arr[2]<->arr[7], high=6
6Check arr[mid]=2, swap arr[mid] and arr[high]126[0, 1, 2, 1, 1, 0, 0, 2, 2]Swap arr[2]<->arr[6], high=5
7Check arr[mid]=0, swap arr[low] and arr[mid]125[0, 0, 1, 1, 1, 0, 2, 2, 2]Swap arr[1]<->arr[2], low=2, mid=3
8Check arr[mid]=1, mid++235[0, 0, 1, 1, 1, 0, 2, 2, 2]mid=3
9Check arr[mid]=1, mid++245[0, 0, 1, 1, 1, 0, 2, 2, 2]mid=4
10Check arr[mid]=1, mid++255[0, 0, 1, 1, 1, 0, 2, 2, 2]mid=5
11Check arr[mid]=0, swap arr[low] and arr[mid]255[0, 0, 0, 1, 1, 1, 2, 2, 2]Swap arr[2]<->arr[5], low=3, mid=6
12mid=6 > high=5, exit loop365[0, 0, 0, 1, 1, 1, 2, 2, 2]Loop ends as mid > high
💡 Loop ends when mid (6) becomes greater than high (5), meaning all elements are partitioned.
Variable Tracker
VariableStartAfter Step 2After Step 4After Step 7After Step 11Final
low001233
mid002366
high877555
Array[2,0,2,1,1,0,0,2,1][1,0,2,1,1,0,0,2,2][0,1,2,1,1,0,0,2,2][0,0,1,1,1,0,2,2,2][0,0,0,1,1,1,2,2,2][0,0,0,1,1,1,2,2,2]
Key Moments - 3 Insights
Why do we not increment mid when swapping with high?
Because the element swapped from high to mid is unprocessed, we must check it again. This is shown in steps 5 and 6 where mid stays the same after swapping with high.
Why do we increment both low and mid when swapping with low?
Because the element swapped from low is already processed (0), so after swapping, both pointers move forward. See steps 4 and 7 where low and mid both increment.
What stops the loop from running forever?
The loop condition mid <= high ensures termination. Each step either increments mid or decrements high, moving pointers closer. Step 12 shows mid surpassing high, ending the loop.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table at step 4, what is the array state?
A[0, 0, 2, 1, 1, 0, 0, 2, 2]
B[1, 0, 2, 1, 1, 0, 0, 2, 2]
C[0, 1, 2, 1, 1, 0, 0, 2, 2]
D[2, 0, 2, 1, 1, 0, 0, 2, 1]
💡 Hint
Check the 'Array State' column at step 4 in the execution_table.
At which step does the pointer 'mid' first move past 'high'?
AStep 10
BStep 12
CStep 11
DStep 9
💡 Hint
Look at the 'mid' and 'high' values in the execution_table rows; step 12 shows mid=6 and high=5.
If we swapped arr[mid] and arr[high] but incremented mid immediately, what would happen?
ASome 2s might remain unprocessed in the middle
BThe algorithm would still work correctly
CThe array would be sorted in reverse order
DLow pointer would move incorrectly
💡 Hint
Refer to key_moments about why mid is not incremented after swapping with high.
Concept Snapshot
Dutch National Flag Partition:
- Use three pointers: low, mid, high
- low and mid start at 0, high at end
- If arr[mid]==0: swap with arr[low], increment low and mid
- If arr[mid]==1: increment mid
- If arr[mid]==2: swap with arr[high], decrement high
- Loop ends when mid > high
- One pass, in-place partitioning
Full Transcript
The Dutch National Flag algorithm partitions an array of 0s, 1s, and 2s in one pass using three pointers: low, mid, and high. Initially, low and mid start at the beginning, and high at the end. We check the element at mid: if it is 0, we swap it with the element at low and move both pointers forward; if it is 1, we just move mid forward; if it is 2, we swap it with the element at high and move high backward without moving mid, because the swapped element needs to be checked. This continues until mid passes high, ensuring all 0s are at the start, 1s in the middle, and 2s at the end. The execution table shows each step with pointer positions and array state, clarifying how the array changes. Key moments explain why mid is not incremented after swapping with high and why both low and mid move after swapping with low. The quiz tests understanding of pointer movements and array states during execution.