Step 1: mid points to 2 (index 0), which is greater than pivot; swap with high (index 6)
Array: [2, 0, 2, 1, 1, 0, 2]
Pointers: low=0, mid=0, high=6
After swap: [2, 0, 2, 1, 1, 0, 2]
(high moves left to 5)
Why: We move the larger element to the end to group all greater elements at the right
Step 2: mid points to 2 (index 0), still greater than pivot; swap with high (index 5)
Array: [2, 0, 2, 1, 1, 0, 2]
Pointers: low=0, mid=0, high=5
After swap: [0, 0, 2, 1, 1, 2, 2]
(high moves left to 4)
Why: Continue moving large elements to the right side
Step 3: mid points to 0 (index 0), less than pivot; swap with low (index 0)
Array: [0, 0, 2, 1, 1, 2, 2]
Pointers: low=0, mid=0, high=4
After swap: [0, 0, 2, 1, 1, 2, 2]
(low and mid move right to 1)
Why: Place smaller elements at the front
Step 4: mid points to 0 (index 1), less than pivot; swap with low (index 1)
Array: [0, 0, 2, 1, 1, 2, 2]
Pointers: low=1, mid=1, high=4
After swap: [0, 0, 2, 1, 1, 2, 2]
(low and mid move right to 2)
Why: Continue grouping smaller elements at the front
Step 5: mid points to 2 (index 2), greater than pivot; swap with high (index 4)
Array: [0, 0, 2, 1, 1, 2, 2]
Pointers: low=2, mid=2, high=4
After swap: [0, 0, 1, 1, 2, 2, 2]
(high moves left to 3)
Why: Move larger elements to the right side
Step 6: mid points to 1 (index 2), equal to pivot; move mid right
Array: [0, 0, 1, 1, 2, 2, 2]
Pointers: low=2, mid=3, high=3
Why: Keep equal elements in the middle
Step 7: mid points to 1 (index 3), equal to pivot; move mid right
Array: [0, 0, 1, 1, 2, 2, 2]
Pointers: low=2, mid=4, high=3
Why: All elements processed as mid crossed high
Result: 0 -> 0 -> 1 -> 1 -> 2 -> 2 -> 2 -> null