-
Notifications
You must be signed in to change notification settings - Fork 37
/
sort.hpp
337 lines (290 loc) · 7.32 KB
/
sort.hpp
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
#ifndef _SORT_H_
#define _SORT_H_
#include "iterator.hpp"
#include <functional>
namespace mystl {
/**
Selection sort base on array
**/
template <typename T>
void selection_sort( T arr[], int size )
{
using std::swap;
if( size < 2 )
{
return;
}
for( int i = 0; i < size; ++i )
{
int min = i;
for( int j = i + 1; j < size; ++j )
{
if( arr[j] < arr[min] )
{
min = j;
}
}
if( min != i )
{
swap( arr[i], arr[min] );
}
}
}
/**
Selection sort in STL style using the user defined comparator
the iterator require at least be forward iterator because we must traverse the container more than once
**/
template <typename ForwardIterator, typename Comp, typename = mystl::RequireForwardIterator<ForwardIterator>>
void selection_sort( ForwardIterator first, ForwardIterator last, Comp comp )
{
using std::swap;
if( first == last )
{
return;
}
for( ; first != last; ++first )
{
auto min = first;
auto cur = first;
for( ++cur; cur != last; ++cur )
{
if( comp( *cur, *min ) )
{
min = cur;
}
}
if( min != first )
{
swap( *min, *first );
}
}
}
/**
Selection sort in STL style using the default comparator
the iterator require at least be forward iterator because we must traverse the container more than once
**/
template <typename ForwardIterator, typename = mystl::RequireForwardIterator<ForwardIterator>>
void selection_sort( ForwardIterator first, ForwardIterator last )
{
using value_type = typename mystl::iterator_traits<ForwardIterator>::value_type;
selection_sort( first, last, std::less<value_type>() );
}
/**
Bubble sort base on array
**/
template <typename T>
void bubble_sort( T arr[], int size )
{
using std::swap;
if( size < 2 )
{
return;
}
for( int i = 0; i < size; ++i )
{
for( int j = 1; j < size - i; ++j )
{
if( arr[j] < arr[j - 1] )
{
swap( arr[j], arr[j - 1] );
}
}
}
}
/**
Bubble sort in STL style using the user defined comparator
the iterator require at least be forward iterator because we must traverse the container more than once
**/
template <typename ForwardIterator, typename Comp, typename = mystl::RequireForwardIterator<ForwardIterator>>
void bubble_sort( ForwardIterator first, ForwardIterator last, Comp comp )
{
using std::swap;
if( first == last )
{
return;
}
while( last != first )
{
auto curr = first;
auto next = first;
for( ++next; next != last; ++next )
{
if( comp( *next, *curr ) )
{
swap( *next, *curr );
}
curr = next;
}
last = curr;
}
}
/**
Bubble sort in STL style using the default comparator
the iterator require at least be forward iterator because we must traverse the container more than once
**/
template <typename ForwardIterator, typename = mystl::RequireForwardIterator<ForwardIterator>>
void bubble_sort( ForwardIterator first, ForwardIterator last )
{
using value_type = typename mystl::iterator_traits<ForwardIterator>::value_type;
bubble_sort( first, last, std::less<value_type>() );
}
/**
Insertion sort base on array
**/
template <typename T>
void insertion_sort( T arr[], int size )
{
if( size < 2 )
{
return;
}
for( int i = 1; i < size; ++i )
{
T value = std::move( arr[i] );
int j = i;
for( ; j > 0 && value < arr[j - 1]; --j )
{
arr[j] = std::move( arr[j - 1] );
}
arr[j] = std::move( value );
}
}
/**
Insertion sort in STL style using the user defined comparator
the iterator require at least be bidirectional iterator because we must traverse the container forth and back
**/
template <typename BidirectionalIterator, typename Comp, typename = mystl::RequireBidirectionalIterator<BidirectionalIterator>>
void insertion_sort( BidirectionalIterator first, BidirectionalIterator last, Comp comp )
{
if( first == last )
{
return;
}
auto iter = first;
for( ++iter; iter != last; ++iter )
{
auto value = std::move( *iter );
auto curr = iter;
auto prev = curr;
--prev;
for( ; curr != first && comp( value, *prev ); --curr, --prev )
{
*curr = std::move( *prev );
}
*curr = std::move( value );
}
}
/**
Insertion sort in STL style using the default comparator
the iterator require at least be bidirectional iterator because we must traverse the container forth and back
**/
template <typename BidirectionalIterator, typename = mystl::RequireBidirectionalIterator<BidirectionalIterator>>
void insertion_sort( BidirectionalIterator first, BidirectionalIterator last )
{
using value_type = typename mystl::iterator_traits<BidirectionalIterator>::value_type;
insertion_sort( first, last, std::less<value_type>() );
}
/**
Merge two sorted array
**/
template <typename T>
void merge( T arr[], T l[], int lsize, T r[], int rsize )
{
int size = lsize + rsize;
for( int i = 0, j = 0, k = 0; k < size; ++k )
{
if( i == lsize )
{
arr[k] = std::move( r[j++] );
}
else if( j == rsize )
{
arr[k] = std::move( l[i++] );
}
else if( r[j] < l[i] )
{
arr[k] = std::move( r[j++] );
}
else
{
arr[k] = std::move( l[i++] );
}
}
}
/**
Merge sort base on array
**/
template <typename T>
void merge_sort( T arr[], int lo, int hi )
{
// we must sure range [lo, hi] is valid
if( hi <= lo )
{
return;
}
int mid = lo + ( hi - lo ) / 2;
auto left = new T[mid - lo + 1];
auto right = new T[hi - mid];
int cnt = 0;
for( int i = lo; i <= mid; ++i )
{
left[cnt++] = std::move( arr[i] );
}
cnt = 0;
for( int i = mid + 1; i <= hi; ++i )
{
right[cnt++] = std::move( arr[i] );
}
merge_sort( left, 0, mid - lo );
merge_sort( right, 0, hi - mid - 1 );
merge( arr, left, mid - lo + 1, right, hi - mid );
delete[] left;
delete[] right;
}
/**
Merge sort base on array
**/
template <typename T>
void merge_sort( T arr[], int size )
{
merge_sort( arr, 0, size - 1 );
}
/**
Quick sort base on array
**/
template <typename T>
void quick_sort( T arr[], int low, int high )
{
using std::swap;
if( low >= high )
{
return;
}
int i = low, j = high + 1;
T pivot = arr[low];
while( true )
{
while( i < high && arr[++i] < pivot )
;
while( arr[--j] > pivot )
;
if( i >= j )
{
break;
}
swap( arr[i], arr[j] );
}
swap( arr[low], arr[j] );
quick_sort( arr, low, j - 1 );
quick_sort( arr, j + 1, high );
}
/**
Quick sort base on array
**/
template <typename T>
void quick_sort( T arr[], int size )
{
quick_sort( arr, 0, size - 1 );
}
}; // namespace mystl
#endif /* _SORT_H_ */