Skip to content

Commit 4356c48

Browse files
authored
Add Dutch National Flag sorting algorithm
Implements the Dutch National Flag algorithm to sort an array of 0s, 1s, and 2s in-place. Includes error handling for invalid values and provides examples in the docstring.
1 parent e2a78d4 commit 4356c48

File tree

1 file changed

+108
-0
lines changed

1 file changed

+108
-0
lines changed
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
"""
2+
Sorts an array consisting of 0s, 1s, and 2s using the Dutch National Flag algorithm.
3+
4+
This algorithm sorts the array in a single pass with O(n) time complexity and O(1) space complexity.
5+
It uses three pointers to partition the array into sections for 0s, 1s, and 2s.
6+
7+
https://en.wikipedia.org/wiki/Dutch_national_flag_problem
8+
"""
9+
10+
def dutch_flag_sort(arr: list[int]) -> None:
11+
"""
12+
Sorts an array of 0s, 1s, and 2s in-place.
13+
14+
Args:
15+
arr: The list to sort, containing only 0, 1, or 2.
16+
17+
Returns:
18+
None
19+
20+
Raises:
21+
ValueError: If the array contains values other than 0, 1, or 2.
22+
23+
Examples:
24+
>>> arr = [2, 0, 2, 1, 1, 0]
25+
>>> dutch_flag_sort(arr)
26+
>>> arr
27+
[0, 0, 1, 1, 2, 2]
28+
>>> arr = [2, 0, 1]
29+
>>> dutch_flag_sort(arr)
30+
>>> arr
31+
[0, 1, 2]
32+
>>> arr = []
33+
>>> dutch_flag_sort(arr)
34+
>>> arr
35+
[]
36+
>>> arr = [1]
37+
>>> dutch_flag_sort(arr)
38+
>>> arr
39+
[1]
40+
>>> arr = [0, 0, 0]
41+
>>> dutch_flag_sort(arr)
42+
>>> arr
43+
[0, 0, 0]
44+
>>> arr = [2, 2, 2]
45+
>>> dutch_flag_sort(arr)
46+
>>> arr
47+
[2, 2, 2]
48+
>>> arr = [1, 1, 1]
49+
>>> dutch_flag_sort(arr)
50+
>>> arr
51+
[1, 1, 1]
52+
>>> arr = [0, 1, 2, 0, 1, 2]
53+
>>> dutch_flag_sort(arr)
54+
>>> arr
55+
[0, 0, 1, 1, 2, 2]
56+
>>> arr = [2, 1, 0, 2, 1, 0]
57+
>>> dutch_flag_sort(arr)
58+
>>> arr
59+
[0, 0, 1, 1, 2, 2]
60+
>>> arr = [3]
61+
>>> dutch_flag_sort(arr)
62+
Traceback (most recent call last):
63+
...
64+
ValueError: Array contains invalid value: 3
65+
>>> arr = [-1, 0, 1]
66+
>>> dutch_flag_sort(arr)
67+
Traceback (most recent call last):
68+
...
69+
ValueError: Array contains invalid value: -1
70+
>>> arr = [0, 1, 2, 3]
71+
>>> dutch_flag_sort(arr)
72+
Traceback (most recent call last):
73+
...
74+
ValueError: Array contains invalid value: 3
75+
>>> arr = (2, 0, 1)
76+
>>> dutch_flag_sort(arr)
77+
Traceback (most recent call last):
78+
...
79+
TypeError: 'tuple' object does not support item assignment
80+
>>> arr = [2, 0, '1']
81+
>>> dutch_flag_sort(arr)
82+
Traceback (most recent call last):
83+
...
84+
ValueError: Array contains invalid value: 1
85+
"""
86+
if not arr:
87+
return
88+
low, mid, high = 0, 0, len(arr) - 1
89+
while mid <= high:
90+
if arr[mid] == 0:
91+
arr[low], arr[mid] = arr[mid], arr[low]
92+
low += 1
93+
mid += 1
94+
elif arr[mid] == 1:
95+
mid += 1
96+
elif arr[mid] == 2:
97+
arr[mid], arr[high] = arr[high], arr[mid]
98+
high -= 1
99+
else:
100+
raise ValueError(f"Array contains invalid value: {arr[mid]}")
101+
102+
if __name__ == "__main__":
103+
import doctest
104+
doctest.testmod()
105+
106+
107+
108+
does it require any improvements?

0 commit comments

Comments
 (0)