-
-
Notifications
You must be signed in to change notification settings - Fork 50.4k
Expand file tree
/
Copy pathdutch_national_flag.py
More file actions
39 lines (32 loc) · 989 Bytes
/
dutch_national_flag.py
File metadata and controls
39 lines (32 loc) · 989 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
def dutch_national_flag(arr: list[int]) -> list[int]:
"""
Sorts an array containing only 0s, 1s and 2s
Args:
arr(list[int]): The input array (containing only 0s, 1s and 2s)
Returns:
list[int]: Sorted array
Examples:
>>> dutch_national_flag([2, 0, 2, 1, 2, 0, 1])
[0, 0, 1, 1, 2, 2, 2]
>>> dutch_national_flag([0, 1, 2, 0, 1, 2])
[0, 0, 1, 1, 2, 2]
>>> dutch_national_flag([1, 1, 1])
[1, 1, 1]
>>> dutch_national_flag([])
[]
"""
low, mid, high = 0, 0, len(arr) - 1
while mid <= high:
if arr[mid] == 0:
arr[low], arr[mid] = arr[mid], arr[low]
low += 1
mid += 1
elif arr[mid] == 1:
mid += 1
else:
arr[mid], arr[high] = arr[high], arr[mid]
high -= 1
return arr
if __name__ == "__main__":
arr = [2, 0, 2, 1, 2, 0, 1]
print("Sorted array: ", dutch_national_flag(arr))