-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathdutch_national_flag.cc
96 lines (89 loc) · 2.49 KB
/
dutch_national_flag.cc
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#include "common/common.h"
template<typename RandomAccessIterator>
void dutch_national_flag_sort(RandomAccessIterator begin, RandomAccessIterator end) {
RandomAccessIterator lower = begin;
RandomAccessIterator upper = end - 1;
// upper is the next position for writing the largest element
// the element in the position still needs exmination
for(RandomAccessIterator itor = begin; itor <= upper; ) {
if(*itor < 0) {
if (itor - begin == lower - begin) {
// same.
++itor;
} else {
std::swap(*itor, *lower);
}
++lower;
} else if(*itor > 0) {
std::swap(*itor, *upper);
--upper;
} else {
++itor;
}
}
}
template<typename RandomAccessIterator>
bool negative_positive_partition_check(RandomAccessIterator begin, RandomAccessIterator end) {
int current = -1;
for(RandomAccessIterator i = begin; i != end; ++i) {
switch(current) {
case -1:
if(*i == 0) {
current = 0;
}
if(*i > 0) {
current = 1;
}
break;
case 0:
if(*i < 0) {
return false;
}
if(*i > 0) {
current = 1;
}
break;
case 1:
if(*i <= 0) {
return false;
}
break;
}
}
return true;
}
void test() {
std::vector<int> v = rnd_fill(100, rnd);
v.resize(0);
v.push_back(0);
v.push_back(0);
v.push_back(1);
dutch_national_flag_sort(v.begin(), v.end());
print(v.begin(), v.end());
if(!negative_positive_partition_check(v.begin(), v.end())) {
std::cout<<"Test failed"<<std::endl;
} else {
std::cout<<"Test passed"<<std::endl;
}
}
void sortColors(int A[], int n) {
// Start typing your C/C++ solution below
// DO NOT write int main() function
int zero_pos = 0;
int two_pos = n - 1;
for (int i = 0; i < n && i <= two_pos && zero_pos < two_pos; ) {
if (A[i] == 0) {
swap(A[i], A[zero_pos]);
if (i == zero_pos) { ++i; }
++zero_pos;
} else if (A[i] == 1) {
++i;
} else if (A[i] == 2) {
swap(A[i], A[two_pos]);
--two_pos;
}
}
}
int main() {
test();
}