Bird
0
0
DSA Cprogramming~10 mins

Sort Colors Two Pointer Dutch Flag in DSA C - Execution Trace

Choose your learning style9 modes available
Concept Flow - Sort Colors Two Pointer Dutch Flag
Initialize pointers: low=0, mid=0, high=n-1
Check mid <= high?
NoDone
Yes
Inspect nums[mid
Swap nums[low
mid++
Swap nums[mid
Repeat loop
We use three pointers low, mid, and high to sort colors in one pass by swapping 0s to front, 2s to end, and leaving 1s in middle.
Execution Sample
DSA C
int low = 0, mid = 0, high = n - 1;
while (mid <= high) {
  if (nums[mid] == 0) {
    swap(nums[low++], nums[mid++]);
  } else if (nums[mid] == 1) {
    mid++;
  } else {
    swap(nums[mid], nums[high--]);
  }
}
Sorts array nums containing 0,1,2 by moving 0s left, 2s right, and 1s stay in middle using two pointers.
Execution Table
StepOperationlowmidhighArray StatePointer ChangesVisual State
1Start007[2,0,2,1,1,0,0,2]None2 -> 0 -> 2 -> 1 -> 1 -> 0 -> 0 -> 2
2nums[mid]=2, swap nums[mid] and nums[high], high--006[2,0,2,1,1,0,0,2]Swap indices 0 and 7, high=62 -> 0 -> 2 -> 1 -> 1 -> 0 -> 0 -> 2
3nums[mid]=2, swap nums[mid] and nums[high], high--005[0,0,2,1,1,0,2,2]Swap indices 0 and 6, high=50 -> 0 -> 2 -> 1 -> 1 -> 0 -> 2 -> 2
4nums[mid]=0, swap nums[low] and nums[mid], low++, mid++115[0,0,2,1,1,0,2,2]Swap indices 0 and 0, low=1, mid=10 -> 0 -> 2 -> 1 -> 1 -> 0 -> 2 -> 2
5nums[mid]=0, swap nums[low] and nums[mid], low++, mid++225[0,0,2,1,1,0,2,2]Swap indices 1 and 1, low=2, mid=20 -> 0 -> 2 -> 1 -> 1 -> 0 -> 2 -> 2
6nums[mid]=2, swap nums[mid] and nums[high], high--224[0,0,0,1,1,2,2,2]Swap indices 2 and 5, high=40 -> 0 -> 0 -> 1 -> 1 -> 2 -> 2 -> 2
7nums[mid]=0, swap nums[low] and nums[mid], low++, mid++334[0,0,0,1,1,2,2,2]Swap indices 2 and 2, low=3, mid=30 -> 0 -> 0 -> 1 -> 1 -> 2 -> 2 -> 2
8nums[mid]=1, mid++344[0,0,0,1,1,2,2,2]mid=40 -> 0 -> 0 -> 1 -> 1 -> 2 -> 2 -> 2
9nums[mid]=1, mid++354[0,0,0,1,1,2,2,2]mid=50 -> 0 -> 0 -> 1 -> 1 -> 2 -> 2 -> 2
10Check mid <= high? 5 <= 4 is False354[0,0,0,1,1,2,2,2]Exit loop0 -> 0 -> 0 -> 1 -> 1 -> 2 -> 2 -> 2
💡 mid (5) > high (4), loop ends with array sorted
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4After Step 5After Step 6After Step 7After Step 8After Step 9Final
low0001223333
mid0001223455
high7655544444
Array[2,0,2,1,1,0,0,2][2,0,2,1,1,0,0,2][0,0,2,1,1,0,2,2][0,0,2,1,1,0,2,2][0,0,2,1,1,0,2,2][0,0,0,1,1,2,2,2][0,0,0,1,1,2,2,2][0,0,0,1,1,2,2,2][0,0,0,1,1,2,2,2][0,0,0,1,1,2,2,2]
Key Moments - 3 Insights
Why do we not increment mid when swapping with high pointer?
When nums[mid] is 2, we swap with nums[high] and decrement high but do not increment mid because the new nums[mid] after swap is unknown and must be checked again (see steps 2 and 3).
Why do we increment both low and mid when swapping 0?
When nums[mid] is 0, swapping with nums[low] places 0 at the front, so we increment low and mid to move forward safely (see steps 4 and 5).
When does the loop stop?
The loop stops when mid passes high (step 10), meaning all elements are sorted with 0s before low, 1s between low and mid, and 2s after high.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table at step 5, what is the value of low and mid?
Alow=2, mid=2
Blow=1, mid=1
Clow=0, mid=0
Dlow=3, mid=3
💡 Hint
Check the 'low' and 'mid' columns in execution_table row 5.
At which step does the condition mid <= high become false?
AStep 9
BStep 10
CStep 8
DStep 7
💡 Hint
Look at the exit_note and the last row in execution_table.
If we swapped nums[mid] and nums[high] but incremented mid immediately, what problem would occur?
ALow pointer would move incorrectly
BThe array would be sorted faster
CWe might skip checking the new nums[mid] which could be 0 or 2
DHigh pointer would increment instead of decrement
💡 Hint
Refer to key_moments explanation about why mid is not incremented after swapping with high.
Concept Snapshot
Sort Colors (Dutch Flag) uses three pointers: low, mid, high.
- low and mid start at 0, high at end.
- If nums[mid]==0, swap with nums[low], increment low and mid.
- If nums[mid]==1, just increment mid.
- If nums[mid]==2, swap with nums[high], decrement high only.
- Loop ends when mid > high.
This sorts array in one pass with O(n) time and O(1) space.
Full Transcript
This visualization shows the Sort Colors problem solved by the Dutch National Flag algorithm using two pointers. We start with three pointers: low, mid at the beginning, and high at the end of the array. 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. We repeat this until mid passes high. The execution table shows each step with pointer positions and array state. Key moments clarify why mid is not incremented after swapping with high and when the loop ends. The quiz tests understanding of pointer values and loop termination. This method sorts the array in one pass efficiently.