Repository navigation
Expand file tree
/
Copy pathSorting.cpp
More file actions
302 lines (256 loc) · 7.4 KB
/
Copy pathSorting.cpp
File metadata and controls
302 lines (256 loc) · 7.4 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
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
#include <iostream>
#include <stack>
#include <algorithm>
using namespace std;
//************************************************************
int partition(int *a, int start, int end){
int l = start, r = end - 1;
const int pivot_val = a[l];
while(l < r){
while(l < r && a[r] >= pivot_val) --r;
a[l] = a[r];
while(l < r && a[l] <= pivot_val) ++l;
a[r] = a[l];
}
a[l] = pivot_val;
return l;
}
void quick_sort(int *a, int start, int end){
if(a == NULL) return ;
if(start + 1 < end){
const int pivot_pos = partition(a, start, end);
quick_sort(a, start, pivot_pos);
quick_sort(a, pivot_pos + 1, end);
}
}
//------------------------------------------------------------
//non recursive,左闭右开
void quick_sort2(int *a, int start, int end){
stack<int> stk;
int l = start, r = end;
if(l + 1 < r){
//part 1
int pivot_pos = partition(a, l, r);
if(l + 1 < pivot_pos){
stk.push(l);
stk.push(pivot_pos);
}
if(pivot_pos + 1 + 1 < r){
stk.push(pivot_pos + 1);
stk.push(r);
}
//part 2
while(!stk.empty()){
r = stk.top();
stk.pop();
l = stk.top();
stk.pop();
//same with above
pivot_pos = partition(a, l, r);
if(l + 1 < pivot_pos){
stk.push(l);
stk.push(pivot_pos);
}
if(pivot_pos + 1 + 1 < r){
stk.push(pivot_pos + 1);
stk.push(r);
}
}
}
}
//************************************************************
//k means dist to start
int kth_small_num(int *a, int n, int start, int end, int k){
const int pivot_pos = partition(a, start, end);
int dist = pivot_pos - start + 1;
if(dist == k){
return a[pivot_pos];
}else if(dist < k){
return kth_small_num(a, n, pivot_pos + 1, end, k - dist);
}else {
return kth_small_num(a, n, start, pivot_pos, k);
}
}
//************************************************************
void bubble_sort(int *a, int start, int end){
if(a == NULL) return ;
bool exchange;
for(int i = start; i < end - 1; ++i){
exchange = false;
for(int j = end - 1; j > i; --j){
if(a[j] < a[j - 1]){
swap(a[j], a[j - 1]);
exchange = true;
}
}
if(!exchange) return;
}
}
//************************************************************
void merge(int *a, int *tmp, int start, int mid, int end){
for(int i = start; i < end; ++i){
tmp[i] = a[i];
}
int i = start, j = mid, k = start;
while(i < mid && j < end){
if(tmp[i] < tmp[j]) a[k++] = tmp[i++];
else a[k++] = tmp[j++];
}
while(i < mid) a[k++] = tmp[i++];
while(j < end) a[k++] = tmp[j++];
}
void merge_sort(int *a, int *tmp, int start, int end){
if(a == NULL || tmp == NULL) return ;
if(start + 1 < end){
int mid = start + ((end - start) >> 1);
merge_sort(a, tmp, start, mid);
merge_sort(a, tmp, mid, end); //mid should not plus 1
merge(a, tmp, start, mid, end);
}
}
//use merge sort to count reversion count
int mrmerge(int *a, int *tmp, int start, int mid, int end){
for(int i = start; i < end; ++i)
tmp[i] = a[i];
int i = mid - 1, j = end - 1, k = end - 1;
int cnt = 0;
while(i >= start && j >= mid){
if(tmp[i] > tmp[j]){
cnt += (j - mid + 1);
a[k--] = tmp[i--];
}else{
a[k--] = tmp[j--];
}
}
while(i >= start) a[k--] = tmp[i--];
while(j >= mid) a[k--] = tmp[j--];
return cnt;
}
int mrcount(int *a, int *tmp, int start, int end){
if(a == NULL || tmp == NULL) return 0;
if(start + 1 < end){
int mid = start + ((end - start) >> 1);
int left = mrcount(a, tmp, start, mid);
int right = mrcount(a, tmp, mid, end);
int own = mrmerge(a, tmp, start, mid, end);
return left + right + own;
}
return 0;
}
//************************************************************
void selection_sort(int *a, int start, int end){
if(a == NULL) return ;
for(int i = start; i < end; ++i){
int min_idx = i;
for(int j = i + 1; j < end; ++j){
if(a[j] < a[min_idx])
min_idx = j;
}
if(min_idx != i){
swap(a[i], a[min_idx]);
}
}
}
//************************************************************
void straight_insert_sort(int *a, int start, int end){
if(a == NULL) return ;
int i, j;
for(i = start + 1; i < end; ++i){
int tmp = a[i];
for(j = i - 1; j >= start && tmp < a[j] ; --j)
a[j + 1] = a[j];
a[j + 1] = tmp;
}
}
//************************************************************
void shell_insert(int *a, int start, int end, int gap){
int i, j;
for(i = start + gap; i < end; ++i){
int tmp = a[i];
for(j = i - gap; j >= start && tmp < a[j]; j -= gap){
a[j + gap] = a[j];
}
a[j + gap] = tmp;
}
}
void shell_sort(int *a, int start, int end){
if(a == NULL) return;
int gap = end - start;
while(gap > 1){
gap = gap / 3 + 1;
shell_insert(a, start, end, gap);
}
}
//************************************************************
//array a range: 0 ~ k
void counting_sort(int *a, int n, int k){
if(a == NULL) return;
int *count = (int*) malloc(sizeof(int) * (k + 1));
memset(count, 0, sizeof(int) * (k + 1));
for(int i = 0; i < n; ++i)
count[a[i]]++;
for(int i = 1; i <= k; ++i)
count[i] += count[i - 1];
int *b = (int*) malloc(sizeof(int) * n);
for(int i = n - 1; i >= 0; --i){
b[count[a[i]] - 1] = a[i];
count[a[i]]--;
}
for(int i = 0; i < n; ++i)
a[i] = b[i];
free(count);
free(b);
}
//this one is much better
void counting_sort2(int *a, int n, int k){
if(a == NULL) return;
int *count = new int[k + 1]();
for(int i = 0; i < n; ++i)
count[a[i]]++;
int j = 0;
for(int i = 0; i <= k; ++i){
while(count[i]-- > 0){
a[j++] = i;
}
}
delete[] count;
}
//************************************************************
//n: array number, rm: radius max, d: d-th digit
void radix_counting_sort(int *a, int n, int radius_max, int d){
int *count = (int*) malloc(sizeof(int) * (radius_max + 1));
memset(count, 0, sizeof(int) * (radius_max + 1));
int *b = (int*)malloc(sizeof(int) * n);
for(int i = 0; i < n; ++i){
int digit = (a[i] >> (d - 1)) & 1;
count[digit]++;
}
for(int i = 1; i <= radius_max; ++i)
count[i]+= count[i - 1];
// from end to start to ensure stable
for(int i = n - 1; i >= 0; --i){
int digit = (a[i] >> (d - 1)) & 0x1;
b[count[digit] - 1] = a[i];
count[digit]--;
}
for(int i = 0; i < n; ++i)
a[i] = b[i];
free(count);
free(b);
}
//d: 最大2进制的位数
void radix_sort(int *a, int n, int d){
if(a == NULL) return ;
for(int i = 1; i <= d; ++i){
radix_counting_sort(a, n, 1, i);
}
}
int main(){
int a[] = {3, 5, 1, 4, 5, 6, 120, 23, 234};
const int n = sizeof(a) / sizeof(a[0]);
quick_sort2(a, 0, n);
for(int i = 0; i < n; ++i)
cout<<a[i] <<" ";
cout<<endl;
return 0;
}