-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdutch_national_flag.h
More file actions
140 lines (123 loc) · 3.13 KB
/
Copy pathdutch_national_flag.h
File metadata and controls
140 lines (123 loc) · 3.13 KB
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#ifndef CPP_ALGORITHM_DUTCH_NATIONAL_FLAG_H
#define CPP_ALGORITHM_DUTCH_NATIONAL_FLAG_H
#include <vector>
namespace DutchFlag
{
/**
* \brief Color enum
*/
enum Color
{
Red,
White,
Blue
};
/**
* \brief Dutch national flag problem.
* \param pivot_index pivot index
* \param arr input array
* \return result array
*/
std::vector<Color> DutchFlagPartition1(
int pivot_index,
std::vector<Color>& arr);
/**
* \brief Dutch national flag problem.
* \param pivot_index pivot index
* \param arr input array
* \return result array
*/
std::vector<Color> DutchFlagPartition2(
int pivot_index,
std::vector<Color>& arr);
/**
* \brief Dutch national flag problem.
* \param pivot_index pivot index
* \param arr input array
* \return result array
*/
std::vector<Color> DutchFlagPartition3(
int pivot_index,
std::vector<Color>& arr);
}
// ----------------------------------------------------------------------------
inline std::vector<DutchFlag::Color> DutchFlag::DutchFlagPartition1(
const int pivot_index,
std::vector<Color>& arr)
{
const Color pivot = arr[pivot_index];
for (int i = 0; i < static_cast<Color>(arr.size()); ++i)
{
for (int j = i + 1; j < static_cast<Color>(arr.size()); ++j)
{
if (arr[j] < pivot)
{
std::swap(arr[i], arr[j]);
break;
}
}
}
for (int i = static_cast<Color>(arr.size()) - 1; i >= 0; --i)
{
for (int j = i - 1; j >= 0; --j)
{
if (arr[j] > pivot)
{
std::swap(arr[i], arr[j]);
break;
}
}
}
return arr;
}
// ----------------------------------------------------------------------------
inline std::vector<DutchFlag::Color> DutchFlag::DutchFlagPartition2(
const int pivot_index,
std::vector<Color>& arr)
{
const Color pivot = arr[pivot_index];
int smaller = 0;
for (int i = 0; i < static_cast<Color>(arr.size()); ++i)
{
if (arr[i] < pivot)
{
std::swap(arr[i], arr[smaller++]);
}
}
int larger = static_cast<Color>(arr.size()) - 1;
for (int i = static_cast<Color>(arr.size()) - 1; i >= 0; --i)
{
if (arr[i] > pivot)
{
std::swap(arr[i], arr[larger--]);
}
}
return arr;
}
// ----------------------------------------------------------------------------
inline std::vector<DutchFlag::Color> DutchFlag::DutchFlagPartition3(
const int pivot_index,
std::vector<Color>& arr)
{
const Color pivot = arr[pivot_index];
int smaller = 0;
int equal = 0;
int larger = static_cast<Color>(arr.size());
while (equal < larger)
{
if (arr[equal] < pivot)
{
std::swap(arr[smaller++], arr[equal++]);
}
else if (arr[equal] == pivot)
{
++equal;
}
else
{
std::swap(arr[equal], arr[--larger]);
}
}
return arr;
}
#endif