class Solution:
def sortColors(self, nums):
low, mid, high = 0, 0, 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] == 2
nums[mid], nums[high] = nums[high], nums[mid]
high -= 1
if __name__ == '__main__':
arr = [2, 0, 2, 1, 1, 0, 0]
Solution().sortColors(arr)
print(' -> '.join(map(str, arr)) + ' -> null')continue until mid passes high to process all elements
if current is 0, swap with low and move low and mid forward
nums[low], nums[mid] = nums[mid], nums[low]
swap 0 to the front
advance low and mid pointers after placing 0
if current is 1, just move mid forward
advance mid pointer to check next element
if current is 2, swap with high and move high backward
nums[mid], nums[high] = nums[high], nums[mid]
swap 2 to the end
decrease high pointer after placing 2
0 -> 0 -> 0 -> 1 -> 1 -> 2 -> 2 -> null