-
Notifications
You must be signed in to change notification settings - Fork 0
/
sort-search.cc
247 lines (202 loc) · 4.52 KB
/
sort-search.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
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
/*Name: sort
Operations: merge sort
*/
#include <iostream>
#include <vector>
#include <cstdint>
#include <cmath>
using namespace std;
#define MAX 10
class Sort
{
public:
Sort ()
{
Init (); // init the m_seq
}
virtual ~Sort ()
{
}
virtual void MergeSort ();
virtual void QuickSort ();
uint32_t BinarySearch (const uint32_t &val);
void Print ();
private:
void Init ();
void DoMergeSort (uint32_t p, uint32_t r);
void Merge (uint32_t p, uint32_t q, uint32_t r);
void DoQuickSort (int p, int r); // NOTE: must be int, because there may be negative value, e.g., in worst case: 5,4,3,2,1
int Partition (int p, int r);
private:
vector<int> m_seq;
};
void
Sort::Init ()
{
for (uint32_t i = MAX; i > 0; i--)
m_seq.push_back (i);
}
void
Sort::Print ()
{
for (uint32_t i = 0; i < m_seq.size (); i++)
cout << m_seq.at (i) << " ";
cout << endl;
}
void
Sort::MergeSort ()
{
DoMergeSort (0, m_seq.size () - 1);
}
void
Sort::DoMergeSort (uint32_t p, uint32_t r)
{
if ( p < r )
{
uint32_t q = floor((r + p)/2);
// test
if (0)
cout << p << " " << q << " | " << q + 1 << " " << r << endl;
DoMergeSort (p, q);
DoMergeSort (q+1, r);
Merge (p, q, r);
}
}
void
Sort::Merge (uint32_t p, uint32_t q, uint32_t r)
{
vector<int> left;
vector<int> right;
// save left and right seqs to two new dynamic arrays
for (uint32_t i = p; i <= q; i++)
left.push_back (m_seq.at (i));
for (uint32_t j = q + 1; j <= r; j++ )
right.push_back (m_seq.at (j));
//test
if (0)
{
cout << " left ";
for (uint32_t i = 0; i < left.size (); i++)
cout << left.at (i) << " ";
cout << " | right ";
for (uint32_t i = 0; i < right.size (); i++)
cout << right.at (i) << " ";
cout << endl;
}
// sort the left and right, ascending sequence
uint32_t i = 0;
uint32_t j = 0;
uint32_t k = 0;
for ( k = p; k <= r; k++ )
{
// select the smallest one from left and right, and put it to m_seq
if ( left.at (i) <= right.at (j) )
{
m_seq.at (k) = left.at (i);
i++;
if ( i >= left.size () )
{
k++;
break;
}
}
else
{
m_seq.at (k) = right.at (j);
j++;
if ( j >= right.size () )
{
k++;
break;
}
}
}
// copy the reat data into m_seq
// NOTE: we only need to copy the left one because the rest of right nodes are already there
if ( j >= right.size () )
{
// put the rest left into m_seq
for (; i < left.size (); i++,k++)
m_seq.at (k) = left.at (i);
}
//Print ();
}
void
Sort::QuickSort ()
{
DoQuickSort (0, m_seq.size ()-1);
}
void
Sort::DoQuickSort (int p, int r)
{
if (p < r)
{
uint32_t q = Partition (p,r);
//cout << p << " " << q << " " << r << endl;
DoQuickSort (p, q-1);
DoQuickSort (q+1, r);
}
}
int
Sort::Partition (int p, int r)
{
int i = p - 1; // track the last element that smaller than m_seq.at (r)
int j = p;
// put all elements < m_seq.at (r) to the left of m_seq.at (r), from m_seq.at (0) to m_seq.at (i)
for (;j < r;j++)
{
if ( m_seq.at (j) <= m_seq.at (r))
{
i++;
// exchange i and j
int tmp = m_seq.at (j);
m_seq.at (j) = m_seq.at (i);
m_seq.at (i) = tmp;
}
}
// result: m_seq[0],[1],...,[i],[i+1],[i+2],...,[j],[r]
// m_seq[0],[1],...,[i], all < m_seq[r]
// [i+2],...,[j] all > m_seq[r]
// exchange i + 1 and r
int tmp = m_seq.at (i+1);
m_seq.at (i+1) = m_seq.at (r);
m_seq.at (r) = tmp;
Print ();
return i+1;
}
uint32_t
Sort::BinarySearch (const uint32_t &val)
{
uint32_t low = 0;
uint32_t high = m_seq.size () - 1;
uint32_t mid = (low + high)/2;
while ( low <= high )
{
if ( m_seq.at(mid) < val )
{
low = mid + 1;
}
else if ( m_seq.at (mid) > val)
{
high = mid - 1;
}
else
return mid;
mid = (low + high)/2;
}
return 1000;
}
int main (int argv, char *argc[])
{
Sort sort;
sort.Print ();
//cout << "MergeSort: ";
//sort.MergeSort ();
//sort.Print ();
//cout << "QuickSort: ";
sort.QuickSort ();
sort.Print ();
uint32_t s = 9;
cout << " search for " << s << " result position = " << sort.BinarySearch (s) << endl;
return 0;
}