def dutch_flag_sort(arr):
#set low and high pointer
low, high = 0, len(arr) - 1
i = 0
#iterate through arr
while i <= high:
#if arr[i] is 0, set it before low via swapping
#elif arr[i] is 2, set it after high via swapping
#elif arr[i] is 1, it will naturally fall in the middle
if arr[i] == 0:
arr[i], arr[low] = arr[low], arr[i]
low += 1
i += 1
elif arr[i] == 1:
i += 1
else: #arr[i] == 2:
arr[i], arr[high] = arr[high], arr[i]
high -= 1
return
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter