Skip to content

Commit b9b7291

Browse files
Add Dutch National Flag algorithm
1 parent ba0d3ff commit b9b7291

File tree

1 file changed

+114
-0
lines changed

1 file changed

+114
-0
lines changed

sorting/dutch_flag.cpp

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
#include <algorithm>
2+
#include <iostream>
3+
#include <vector>
4+
5+
void dutchNationalFlag(std::vector<int> &arr) {
6+
int low = 0, mid = 0, high = arr.size() - 1;
7+
8+
while (mid <= high) {
9+
if (arr[mid] == 0) // if arr[mid] is 0 it needs to be placed in the
10+
// beginning so swap it with low position
11+
swap(arr[low++], arr[mid++]);
12+
else if (arr[mid] ==
13+
1) // if arr[mid] is 1 it needs to be placed in the middle ,so
14+
// just increase the value of mid
15+
mid++;
16+
else
17+
swap(arr[mid],
18+
arr[high--]); // if arr[mid] is 2 it needs to be placed in the
19+
// beginning so swap it with high position
20+
}
21+
}
22+
23+
int main() {
24+
vector<int> arr = {2, 0, 1, 2, 1, 0, 1, 2};
25+
dutchNationalFlag(arr);
26+
27+
for (int x : arr) cout << x << " ";
28+
cout << "\n";
29+
}
30+
// a dry run to explain the code:
31+
// arr = {2, 0, 1, 2, 1, 0, 1, 2} We have three pointers :
32+
33+
// low → start of 0’s segment
34+
35+
// mid → current element
36+
37+
// high → end of 2’s segment
38+
39+
// Initially :
40+
41+
// ini Copy code low = 0,
42+
// mid = 0,
43+
// high = 7 arr = [ 2, 0, 1, 2, 1, 0, 1, 2 ] Step 1
44+
// : arr[mid] = 2
45+
46+
// Swap arr[mid] and
47+
// arr[high] → swap 2 with 2(no change)
48+
49+
// Decrement high = 6
50+
51+
// ini Copy code arr = [ 2, 0, 1, 2, 1, 0, 1, 2 ] low = 0,
52+
// mid = 0,
53+
// high = 6 Step 2 : arr[mid] = 2
54+
55+
// Swap arr[mid] and
56+
// arr[high] → swap 2 with 1
57+
58+
// Decrement high = 5
59+
60+
// ini Copy code arr =
61+
// [ 1, 0, 1, 2, 1, 0, 2, 2 ] low = 0,
62+
// mid = 0,
63+
// high = 5 Step 3 : arr[mid] = 1
64+
65+
// Do nothing,
66+
// just increment mid = 1
67+
68+
// ini Copy code arr = [ 1, 0, 1, 2, 1, 0, 2, 2 ] low = 0,
69+
// mid = 1,
70+
// high = 5 Step 4 : arr[mid] = 0
71+
72+
// Swap arr[low] and
73+
// arr[mid] → swap 1 and 0
74+
75+
// Increment low = 1,
76+
// mid = 2
77+
78+
// ini Copy code arr = [ 0, 1, 1, 2, 1, 0, 2, 2 ] low = 1,
79+
// mid = 2,
80+
// high = 5 Step 5 : arr[mid] = 1
81+
82+
// Increment mid = 3
83+
84+
// ini Copy code arr =
85+
// [ 0, 1, 1, 2, 1, 0, 2, 2 ] low = 1,
86+
// mid = 3,
87+
// high = 5 Step 6 : arr[mid] = 2
88+
89+
// Swap arr[mid] and
90+
// arr[high] → swap 2 and 0
91+
92+
// Decrement high = 4
93+
94+
// ini Copy code arr =
95+
// [ 0, 1, 1, 0, 1, 2, 2, 2 ] low = 1,
96+
// mid = 3,
97+
// high = 4 Step 7 : arr[mid] = 0
98+
99+
// Swap arr[low] and
100+
// arr[mid] → swap 1 and 0
101+
102+
// Increment low = 2,
103+
// mid = 4
104+
105+
// ini Copy code arr = [ 0, 0, 1, 1, 1, 2, 2, 2 ] low = 2,
106+
// mid = 4,
107+
// high = 4 Step 8 : arr[mid] = 1
108+
109+
// Increment mid = 5 → now mid > high,
110+
// done !
111+
112+
// Final sorted array :
113+
114+
// csharp Copy code[0, 0, 1, 1, 1, 2, 2, 2]

0 commit comments

Comments
 (0)