0
0
DSA Pythonprogramming~10 mins

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

Choose your learning style9 modes available
Concept Flow - Sort Colors Two Pointer Dutch Flag
Start: Initialize pointers
low=0, mid=0, high=n-1
Check mid <= high?
NoDone
Yes
Check nums[mid
Swap nums[low
mid++
Swap nums[mid
Repeat Check mid <= high?
We use three pointers low, mid, and high to sort colors in one pass by swapping 0s to front, 2s to end, and 1s stay in middle.
Execution Sample
DSA Python
nums = [2,0,2,1,1,0]
low = 0
mid = 0
high = len(nums) - 1
while mid <= high:
    if nums[mid] == 0:
        nums[low], nums[mid] = nums[mid], nums[low]
        low += 1
        mid += 1
    elif nums[mid] == 1:
        mid += 1
    else:
        nums[mid], nums[high] = nums[high], nums[mid]
        high -= 1
Sorts the list nums containing 0,1,2 using three pointers in one pass.
Execution Table
StepOperationlowmidhighArray StatePointer Changes
1Start005[2,0,2,1,1,0]Initialize pointers
2Check nums[mid]=2005[2,0,2,1,1,0]Swap nums[mid] and nums[high], high--
3Swap nums[0]<->nums[5]004[0,0,2,1,1,2]high=5->4, mid stays 0
4Check nums[mid]=0004[0,0,2,1,1,2]Swap nums[low] and nums[mid], low++, mid++
5Swap nums[0]<->nums[0]114[0,0,2,1,1,2]low=0->1, mid=0->1
6Check nums[mid]=0114[0,0,2,1,1,2]Swap nums[low] and nums[mid], low++, mid++
7Swap nums[1]<->nums[1]224[0,0,2,1,1,2]low=1->2, mid=1->2
8Check nums[mid]=2224[0,0,2,1,1,2]Swap nums[mid] and nums[high], high--
9Swap nums[2]<->nums[4]223[0,0,1,1,2,2]high=4->3, mid stays 2
10Check nums[mid]=1223[0,0,1,1,2,2]mid++
11mid=2->3233[0,0,1,1,2,2]No swap
12Check nums[mid]=1233[0,0,1,1,2,2]mid++
13mid=3->4243[0,0,1,1,2,2]No swap
14Check mid <= high? 4 <= 3243[0,0,1,1,2,2]Condition false, stop
💡 mid (4) > high (3), sorting complete
Variable Tracker
VariableStartAfter Step 3After Step 5After Step 7After Step 9After Step 11After Step 13Final
low00122222
mid00122344
high54443333
nums[2,0,2,1,1,0][0,0,2,1,1,2][0,0,2,1,1,2][0,0,2,1,1,2][0,0,1,1,2,2][0,0,1,1,2,2][0,0,1,1,2,2][0,0,1,1,2,2]
Key Moments - 3 Insights
Why do we not increment mid when swapping with high pointer?
Because after swapping with high, the new nums[mid] is unknown and must be checked again. See steps 2-3 and 8-9 where mid stays the same after swap.
Why do we increment both low and mid when swapping 0?
Because swapping 0 to front ensures that position is sorted, so we can safely move both pointers forward. See steps 4-7 where low and mid both increment.
When does the loop stop?
The loop stops when mid passes high, meaning all elements beyond high are sorted as 2s. See step 14 where mid=4 > high=3.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table at step 9, what is the array state?
A[0,0,2,1,1,2]
B[2,0,1,1,0,2]
C[0,0,1,1,2,2]
D[0,0,1,2,1,2]
💡 Hint
Check the 'Array State' column at step 9 in the execution_table.
At which step does the mid pointer first increment without swapping?
AStep 10
BStep 2
CStep 6
DStep 4
💡 Hint
Look for steps where 'mid++' happens without swap in the 'Pointer Changes' column.
If we changed the first element from 2 to 1, what would happen at step 2?
ASwap with high pointer and decrement high
BIncrement mid pointer only
CSwap with low pointer and increment low and mid
DLoop ends immediately
💡 Hint
Check the condition for nums[mid] == 1 in the code and execution_table steps.
Concept Snapshot
Sort Colors (0,1,2) using three pointers:
- low: next position for 0
- mid: current element
- high: next position for 2
Loop while mid <= high:
- If nums[mid]==0: swap with low, low++, mid++
- If nums[mid]==1: mid++
- If nums[mid]==2: swap with high, high--
One pass, in-place, O(n) time.
Full Transcript
This visualization shows how the Dutch National Flag algorithm sorts an array of 0s, 1s, and 2s 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's 0, we swap it with low and move both pointers forward; if it's 1, we just move mid forward; if it's 2, we swap it with high and move high backward without moving mid, because the swapped element needs checking. This continues until mid passes high, ensuring all 0s are at front, 1s in middle, and 2s at end. The execution table tracks each step, pointer positions, and array state changes. Key moments clarify why mid doesn't always move after swapping with high and when the loop ends. The quiz tests understanding of pointer movements and array states during sorting.