关于各种排序

源码及测试样例下载

LSD(Least Significant Digit)基数排序

需要r个辅助队列(本代码中r为10)

  • 时间复杂度O(d(n+r))
  • 空间复杂度O®
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
int *LSD(int *a, int f, int l)
{
int n = l - f + 1;
int m = log(n) / log(10);
queue<int> q[10];
for(int i = 0; i < m; i++){
for(int j = 0; j < n; j++){
int t = a[f + j] % (int)pow(10, i + 1);
t = t / (int)pow(10, i);
q[t].push(a[f + j]);
}
int k = 0;
for(int j = 0; j < 10; j++){
while(!q[j].empty()){
a[f + k] = q[j].front();
q[j].pop();
k++;
}
}
}
return a;
}

MSD(Most Significant Digit)基数排序

需要r个辅助队列(本代码中r为10)

  • 时间复杂度O(d(n+r))
  • 空间复杂度O®
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
int *MSD(int *a, int f, int l)
{
int n = l - f + 1;
int m = log(n) / log(10);
queue<int> q[10];
for(int i = 0; i < m; i++){
for(int j = 0; j < n; j++){
int t = a[f + j] / (int)pow(10, i);
t = t % 10;
q[t].push(a[f + j]);
}
int k = 0;
for(int j = 0; j < 10; j++){
while(!q[j].empty()){
a[f + k] = q[j].front();
q[j].pop();
k++;
}
}
}
return a;
}

QS(Quick Sort)快速排序

  • 时间复杂度O(nlogn)
  • 空间复杂度O(1)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
int *QS(int *a, int f, int l)
{
if(f >= l) return a;
int i = f, j = l;
int x = a[f];
while(i < j){
while(i < j && a[j] >= x) j--;
if(i < j) a[i++] = a[j];
while(i < j && a[i] <= x) i++;
if(i < j) a[j--] = a[i];
}
a[i] = x;
QS(a, f, i - 1);
QS(a, i + 1, l);
return a;
}

MS(Merge Sort)归并排序

  • 时间复杂度O(nlogn)
  • 空间复杂度O(n)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int *MS(int *a, int f, int l)
{
if(f >= l) return a;
int m = (f + l) / 2;
MS(a, f, m);
MS(a, m + 1, l);
int *b = new int[l - f + 1];
int i = f, j = m + 1, k = 0;
while(i <= m && j <= l){
if(a[i] < a[j]) b[k++] = a[i++];
else b[k++] = a[j++];
}
while(i <= m) b[k++] = a[i++];
while(j <= l) b[k++] = a[j++];
for(int i = 0; i < k; i++) a[f + i] = b[i];
return a;
}

SS(Shell Sort)希尔排序

  • 时间复杂度O(n^1.3)
  • 空间复杂度O(1)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
int *SS(int *a, int f, int l)
{
int n = l - f + 1;
int h = 1;
h = n / 2;
while(h >= 1){
for(int i = f + h; i <= l; i++){
int t = a[i];
int j = i - h;
while(j >= f && a[j] > t){
a[j + h] = a[j];
j -= h;
}
a[j + h] = t;
}
h /= 2;
}
return a;
}

IS(Insertion Sort)插入排序

  • 时间复杂度O(n^2)
  • 空间复杂度O(1)
1
2
3
4
5
6
7
8
9
10
11
12
int *IS(int *a, int f, int l){
for(int i = f + 1; i <= l; i++){
int t = a[i];
int j = i - 1;
while(j >= f && a[j] > t){
a[j + 1] = a[j];
j--;
}
a[j + 1] = t;
}
return a;
}

BS(Bubble Sort)冒泡排序

  • 时间复杂度O(n^2)
  • 空间复杂度O(1)
1
2
3
4
5
6
7
8
9
10
11
12
int *BS(int *a, int f, int l){
for(int i = f; i < l; i++){
for(int j = f; j < l - i; j++){
if(a[j] > a[j + 1]){
int t = a[j];
a[j] = a[j + 1];
a[j + 1] = t;
}
}
}
return a;
}

测试部分:

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
/*
* @Author: 转接
* @Date: 2022-04-11 13:27:41
* @LastEditors: 转接
* @LastEditTime: 2022-04-11 15:19:31
* @Description: 各种排序算法
*/

#include <iostream>
#include <math.h>
#include <queue>
#include <time.h>
#include <fstream>

using namespace std;

// LSD(Least Significant Digit)基数排序
// 需要r个辅助队列(本代码中r为10)
// 时间复杂度O(d(n+r))
// 空间复杂度O(r)
int *LSD(int *a, int f, int l)
{
int n = l - f + 1;
int m = log(n) / log(10);
queue<int> q[10];
for(int i = 0; i < m; i++){
for(int j = 0; j < n; j++){
int t = a[f + j] % (int)pow(10, i + 1);
t = t / (int)pow(10, i);
q[t].push(a[f + j]);
}
int k = 0;
for(int j = 0; j < 10; j++){
while(!q[j].empty()){
a[f + k] = q[j].front();
q[j].pop();
k++;
}
}
}
return a;
}

// MSD(Most Significant Digit)基数排序
// 需要r个辅助队列(本代码中r为10)
// 时间复杂度O(d(n+r))
// 空间复杂度O(r)
int *MSD(int *a, int f, int l)
{
int n = l - f + 1;
int m = log(n) / log(10);
queue<int> q[10];
for(int i = 0; i < m; i++){
for(int j = 0; j < n; j++){
int t = a[f + j] / (int)pow(10, i);
t = t % 10;
q[t].push(a[f + j]);
}
int k = 0;
for(int j = 0; j < 10; j++){
while(!q[j].empty()){
a[f + k] = q[j].front();
q[j].pop();
k++;
}
}
}
return a;
}

// QS(Quick Sort)快速排序
// 时间复杂度O(nlogn)
// 空间复杂度O(1)
int *QS(int *a, int f, int l)
{
if(f >= l) return a;
int i = f, j = l;
int x = a[f];
while(i < j){
while(i < j && a[j] >= x) j--;
if(i < j) a[i++] = a[j];
while(i < j && a[i] <= x) i++;
if(i < j) a[j--] = a[i];
}
a[i] = x;
QS(a, f, i - 1);
QS(a, i + 1, l);
return a;
}

// MS(Merge Sort)归并排序
// 时间复杂度O(nlogn)
// 空间复杂度O(n)
int *MS(int *a, int f, int l)
{
if(f >= l) return a;
int m = (f + l) / 2;
MS(a, f, m);
MS(a, m + 1, l);
int *b = new int[l - f + 1];
int i = f, j = m + 1, k = 0;
while(i <= m && j <= l){
if(a[i] < a[j]) b[k++] = a[i++];
else b[k++] = a[j++];
}
while(i <= m) b[k++] = a[i++];
while(j <= l) b[k++] = a[j++];
for(int i = 0; i < k; i++) a[f + i] = b[i];
return a;
}

// SS(Shell Sort)希尔排序
// 时间复杂度O(n^1.3)
// 空间复杂度O(1)
int *SS(int *a, int f, int l)
{
int n = l - f + 1;
int h = 1;
h = n / 2;
while(h >= 1){
for(int i = f + h; i <= l; i++){
int t = a[i];
int j = i - h;
while(j >= f && a[j] > t){
a[j + h] = a[j];
j -= h;
}
a[j + h] = t;
}
h /= 2;
}
return a;
}

// IS(Insertion Sort)插入排序
// 时间复杂度O(n^2)
// 空间复杂度O(1)
int *IS(int *a, int f, int l){
for(int i = f + 1; i <= l; i++){
int t = a[i];
int j = i - 1;
while(j >= f && a[j] > t){
a[j + 1] = a[j];
j--;
}
a[j + 1] = t;
}
return a;
}

// BS(Bubble Sort)冒泡排序
// 时间复杂度O(n^2)
// 空间复杂度O(1)
int *BS(int *a, int f, int l){
for(int i = f; i < l; i++){
for(int j = f; j < l - i; j++){
if(a[j] > a[j + 1]){
int t = a[j];
a[j] = a[j + 1];
a[j + 1] = t;
}
}
}
return a;
}



int main(){
int n = 10000;
// 生成n个随机数
int a[n];
for(int i = 0; i < n; i++){
a[i] = rand() % 10000;
}
// 将数据写入文件
ofstream fout("data.txt");
fout << "排序前:" << endl;
for(int i = 0; i < n; i++){
fout << a[i] << " ";
}
fout << endl;
fout.close();
// LSD基数排序
// 记录时间
clock_t time1 = clock();
int *b = LSD(a,0,n-1);
clock_t time2 = clock();
// 输出结果追加到data.txt
fout.open("data.txt", ios::app);
fout << "LSD基数排序后:" << endl;
for(int i = 0; i < n; i++){
fout << b[i] << " ";
}
fout << endl;
fout.close();

// MSD基数排序
// 记录时间
clock_t time3 = clock();
int *c = MSD(a,0,n-1);
clock_t time4 = clock();
// 输出结果追加到data.txt
fout.open("data.txt", ios::app);
fout << "MSD基数排序后:" << endl;
for(int i = 0; i < n; i++){
fout << c[i] << " ";
}
fout << endl;
fout.close();

// QS快速排序
// 记录时间
clock_t time5 = clock();
int *d = QS(a,0,n-1);
clock_t time6 = clock();
// 输出结果追加到data.txt
fout.open("data.txt", ios::app);
fout << "QS快速排序后:" << endl;
for(int i = 0; i < n; i++){
fout << d[i] << " ";
}
fout << endl;
fout.close();

// MS归并排序
// 记录时间
clock_t time7 = clock();
int *e = MS(a,0,n-1);
clock_t time8 = clock();
// 输出结果追加到data.txt
fout.open("data.txt", ios::app);
fout << "MS归并排序后:" << endl;
for(int i = 0; i < n; i++){
fout << e[i] << " ";
}
fout << endl;
fout.close();

// SS希尔排序
// 记录时间
clock_t time9 = clock();
int *f = SS(a,0,n-1);
clock_t time10 = clock();
// 输出结果追加到data.txt
fout.open("data.txt", ios::app);
fout << "SS希尔排序后:" << endl;
for(int i = 0; i < n; i++){
fout << f[i] << " ";
}
fout << endl;
fout.close();

// IS插入排序
// 记录时间
clock_t time11 = clock();
int *g = IS(a,0,n-1);
clock_t time12 = clock();
// 输出结果追加到data.txt
fout.open("data.txt", ios::app);
fout << "IS插入排序后:" << endl;
for(int i = 0; i < n; i++){
fout << g[i] << " ";
}
fout << endl;
fout.close();

// BS冒泡排序
// 记录时间
clock_t time15 = clock();
int *k = BS(a,0,n-1);
clock_t time16 = clock();
// 输出结果追加到data.txt
fout.open("data.txt", ios::app);
fout << "BS冒泡排序后:" << endl;
for(int i = 0; i < n; i++){
fout << k[i] << " ";
}
fout << endl;
fout.close();

// 最后输出排序时间比较
fout.open("data.txt", ios::app);
fout << "-----------------------------------------------" << endl;
fout << "LSD基数排序用时:" << (double)(time2 - time1) / CLOCKS_PER_SEC << "s" << endl;
fout << "MSD基数排序用时:" << (double)(time4 - time3) / CLOCKS_PER_SEC << "s" << endl;
fout << "QS快速排序用时:" << (double)(time6 - time5) / CLOCKS_PER_SEC << "s" << endl;
fout << "MS归并排序用时:" << (double)(time8 - time7) / CLOCKS_PER_SEC << "s" << endl;
fout << "SS希尔排序用时:" << (double)(time10 - time9) / CLOCKS_PER_SEC << "s" << endl;
fout << "IS插入排序用时:" << (double)(time12 - time11) / CLOCKS_PER_SEC << "s" << endl;
fout.close();
return 0;
}

关于各种排序
https://blog.zhuanjie.ltd/2022/04/11/all-kinds-of-sort/
作者
转接
发布于
2022年4月11日
许可协议