-
Notifications
You must be signed in to change notification settings - Fork 1
/
UsefulSortAlgorithm.cpp
304 lines (300 loc) · 6.72 KB
/
UsefulSortAlgorithm.cpp
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
// UsefulSortAlgorithm.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
#include "pch.h"
#include <iostream>
#include<stdio.h>
using namespace std;
// 冒泡排序
void BubbleSort(int* p, int n)
{
for (int i = 0; i < n-1; i++)
{
for (int j = 0; j < n - 1 - i; j++)
{
if (p[j] > p[j + 1])
{
p[j] ^= p[j + 1];
p[j + 1] ^= p[j];
p[j] ^= p[j + 1];
}
}
}
}
// 显示函数
void dis(int* arr, int n)
{
for (int i = 0; i < n; i++)
{
cout << " "<< arr[i] << dec;
}
putchar(10);
}
// 冒泡排序优化设计
void BubbleSort_OptimizationVersion(int* p, int n)
{
int flag;
for (int i = 0; i < n - 1; i++)
{
flag = 1;//加标志的原因就是,如果本身是有序的,就不需要交换,免得浪费运行时间
for (int j = 0; j < n - 1 - i; j++)
{
if (p[j] > p[j + 1])
{
p[j] ^= p[j + 1];
p[j + 1] ^= p[j];
p[j] ^= p[j + 1];
flag = 0;
}
}
if (flag)
break;
}
}
/*插入排序
===========================================================//
插入排序的特点
(1)稳定、快速、稳定指的是对于一样大的两个数不会交换位置
(2)比较的次数不一定,比较的次数越少,插入点后的数据移动越多
(3)平均时间复杂度O(n2)
(4)适合于少量数据的排序(1000以内的数据)
(5)
===========================================================//
*/
void InsertSortionComplete(int* Array, int NumberOf_ArrayElement)
{
int i, j, temp;
for (i = 0; i < NumberOf_ArrayElement; i++)
{
temp = Array[i];
for (j = i; j - 1 >= 0 && temp < Array[j - 1]; j--)
{
Array[j] = Array[j - 1];
}
Array[j] = temp;
}
}
/*希尔排序
===========================================================//
希尔排序的特点
(1)希尔排序是基于插入排序的以下两点性质提出的改进方法
(2)优点: 快, 移动数据少; 希尔排序在对几乎已经排好序的数据操作时, 效率高, 即可以达到线性排序的效率
(3)缺点: 不稳定, d的取值是多少, 应取多少个不同的值, 都无法确切知道, 只能凭经验来取
(4)平均时间复杂度O(n1.3)
(5)希尔排序的本质还是插入排序
(6)
===========================================================//
*/
void shellSort(int* arr, int n)
{
int i, j, temp;
int gap = n / 2;
while (gap >= 1)
{
for (i = gap; i < n; i++)
{
temp = arr[i];
for (j = i; j - gap >= 0 && temp < arr[j - gap]; j -= gap)
{
arr[j] = arr[j - gap];
}
arr[j] = temp;
}
gap /= 2;
}
}
/*选择排序
===========================================================//
选择排序的特点
===========================================================//
*/
void selectSort(int* arr, int n)
{
for (int i = 0; i < n - 1; i++)
{
for (int j = i + 1; j < n; j++)
{
if (arr[i] > arr[j])
{
arr[i] ^= arr[j];
arr[j] ^= arr[i];
arr[i] ^= arr[j];
}
}
}
}
void selectSort_OptimizationVersion(int* arr, int n)
{
int idx;
for (int i = 0; i < n - 1; i++)
{
idx = i;
for (int j = i + 1; j < n; j++)
{
if (arr[idx] > arr[j])
{
idx = j;
}
}
if (idx != i)
{
arr[i] ^= arr[idx];
arr[idx] ^= arr[i];
arr[i] ^= arr[idx];
}
}
}
/*快速排序
===========================================================//
快速排序
this mean needs to master permanently
===========================================================//
*/
void QuikSort(int* arr, int left, int right)
{
if (left < right)
{
int pivot = arr[left], l = left, h = right;
while (l < h)
{
while (l < h&&arr[h] >= pivot)
h--;
arr[l] = arr[h];
while (l < h&&arr[l] <= pivot)
l++;
arr[h] = arr[l];
}
arr[h] = pivot;
QuikSort(arr, left, h - 1);
QuikSort(arr, h + 1, right);
}
}
void Qsort(int arr[], int low, int high) {
if (high <= low) return;
int i = low;
int j = high + 1;
int key = arr[low];
while (true)
{
/*从左向右找比key大的值*/
while (arr[++i] < key)
{
if (i == high) {
break;
}
}
/*从右向左找比key小的值*/
while (arr[--j] > key)
{
if (j == low) {
break;
}
}
if (i >= j) break;
/*交换i,j对应的值*/
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
/*中枢值与j对应值交换*/
int temp = arr[low];
arr[low] = arr[j];
arr[j] = temp;
Qsort(arr, low, j - 1);
Qsort(arr, j + 1, high);
}
// 数组聚合函数(将两个有序的数组,合并为一个有序的数组)
void MergeTwoArrsToOneArr(int* a, int an, int* b, int bn, int* c, int cn)
{
int i = 0; int j = 0; int k = 0;
while (i != an && j != bn)
{
if (a[i] < b[j])
c[k++] = a[i++];
else
c[k++] = b[j++];
}
if (i == an)
while (j != bn)
c[k++] = b[j++];
else
while (i != an)
c[k++] = a[i++];
}
// 将一个数组里面的有序子数组合并为一个大的有序的数组,例如将int Source[8] = { 2,6,89,99,1,5,56,59 }到int Aim[8]
void MergeOneArrToAnotherArr(int* SourceArray,int* TempArray,int Start,int Mid,int End)
{
int i = Start; int j = Mid + 1; int k = Start;
while (i != Mid + 1 && j != End + 1)
{
if (SourceArray[i] < SourceArray[j])
TempArray[k++] = SourceArray[i++];
else
TempArray[k++] = SourceArray[j++];
}
if (i == Mid + 1)
while (j != End + 1)
TempArray[k++] = SourceArray[j++];
else
while (i != Mid + 1)
TempArray[k++] = SourceArray[i++];
while (Start <= End)
{
SourceArray[Start] = TempArray[Start];
Start++;
}
}
/*归并排序
===========================================================//
归并排序
this mean needs to master permanently
===========================================================//
*/
void MergeSort(int* arr, int* tmp, int start, int end)
{
if (start < end)
{
int mid = (start + end) / 2;
MergeSort(arr, tmp, start, mid);
MergeSort(arr, tmp, mid + 1, end);
MergeOneArrToAnotherArr(arr, tmp, start, mid, end);
}
}
#if 0
int main()
{
int arr[] = { 1,3,5,7,9,2,4,6,8 , 10 };
//BubbleSort(arr, sizeof(arr) / sizeof(*arr));
//dis(arr, sizeof(arr) / sizeof(*arr));
//InsertSortionComplete(arr, sizeof(arr) / sizeof(*arr));
selectSort_OptimizationVersion(arr, 10);
dis(arr, sizeof(arr) / sizeof(*arr));
cout << endl;
return 0;
}
#endif
#if 0
int main()
{
int a[5] = { 2,4,45,78,99 };
int b[3] = { 1,90,101 };
int c[8];
MergeTwoArrsToOneArr(a, 5, b, 3, c, 8);
for (int n = 0; n < 8; n++)
{
printf("%d ", c[n]);
}
return 0;
}
#endif
#if 1
int main()
{
int Source[8] = { 2,6,89,99,1,5,56,59 };
int Aim[8];
MergeOneArrToAnotherArr(Source, Aim, 0, 3, 7);
for (int n = 0; n < 8; n++)
{
printf("%d ", Aim[n]);
}
return 0;
}
#endif