-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdnf.cpp
54 lines (42 loc) · 1.03 KB
/
dnf.cpp
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include "iostream"
#define REP(i, n) for(int i = 0; i < n; i++)
using namespace std;
void printList(int arr[], int n) {
for (int i = 0; i < n; ++i)
cout << arr[i] << ' ';
cout << '\n';
}
void twoColors(int arr[], int n) {
int low = 0;
int high = n - 1;
int temp;
while(low <= high) {
if (arr[low] == 0) low++;
else { temp = arr[low]; arr[low] = arr[high]; arr[high--] = temp;}
}
}
void threeColors(int arr[], int n) {
int low = 0;
int high = n - 1;
int temp;
int mid = 0;
while(mid <= high) {
if (arr[mid] == 0) { temp = arr[low]; arr[low++] = arr[mid]; arr[mid++] = temp;}
else if (arr[mid] == 1) mid++;
else { temp = arr[high]; arr[high--] = arr[mid]; arr[mid] = temp;};
}
}
int main()
{
int arr[] = {0, 1, 0, 1, 1, 0, 0, 0};
int size = sizeof(arr)/sizeof(arr[0]);
printList(arr, size);
twoColors(arr, size);
printList(arr, size);
int arr2[] = {1, 0, 0, 0, 0, 1, 1, 2, 1, 2};
int size2 = sizeof(arr2)/sizeof(arr2[0]);
printList(arr2, size2);
threeColors(arr2, size2);
printList(arr2, size2);
return 0;
}