forked from guanshan/allsort
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathallSort.cpp
297 lines (279 loc) · 4.89 KB
/
allSort.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
#include "allSort.h"
using namespace std;
template <class T> void allSort<T>::insertSort(T a[],int n)
{
if (a==NULL || n<=0)
{
cout << "输入参数有问题!" << endl;
return ;
}
T flag;//哨兵
int j = 0;
for(int i=1;i<n;++i)
{
flag = a[i];
for(j=i-1;j>=0;j--)
{
if(flag<a[j])
{
a[j+1]=a[j];
}
else
break;
}
a[j+1]=flag;
}
}
template <class T> void allSort<T>::shellSort(T a[],int n)
{
if (a==NULL || n<=0)
{
cout << "输入参数有问题!" << endl;
return ;
}
int inc = n/2;
for(;inc>0;inc/=2)
{
shellInsert(a,inc,n);
}
}
template <class T> void allSort<T>::shellInsert(T a[],int inc,int n)
{
int j;
T flag;
for(int i=inc;i<n;i++)
{
flag = a[i];
for(j=i-inc;j>=0;j-=inc)
{
if(flag<a[j])
{
a[j+inc]=a[j];
}
else
{
break;
}
}
a[j+inc]=flag;
/* j = i;//风格与之前插入排序保持一致,所以用上面的代码
while(j>=0 && a[j-inc]>flag)
{
a[j]=a[j-inc];
j-=inc;
}
a[j]=flag;//是不是有可能j<0啊,这段代码有问题*/
}
}
template <class T> void allSort<T>::selectSort(T a[],int n)
{
if (a==NULL || n<=0)
{
cout << "输入参数有问题!" << endl;
return ;
}
int min = -1;
T temp;
//i=n-1的时候已经不需要比了,所以是i<n-1
for(int i=0;i<n-1;++i)
{
min=i;
for(int j=i+1;j<n;++j)
{
if(a[min]>a[j])
{
min = j;
}
}
if(min!=i)
{
temp = a[i];
a[i] = a[min];
a[min] = temp;
}
}
}
template <class T> void allSort<T>::bubbleSort(T a[],int n)
{
if (a==NULL || n<=0)
{
cout << "输入参数有问题!" << endl;
return ;
}
int i = 0;
int j = 0;
bool change=true;
T temp;
//大的数字往下沉
for (i=n-1;i>0&&change;--i)
{
change=false;
for (j=0;j<i;j++)
{
if(a[j]>a[j+1])
{
temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
change = true;
}
}
}
}
template <class T> void allSort<T>::mergeSort(T a[],int n)
{
if (a==NULL || n<=0)
{
cout << "输入参数有问题!" << endl;
return ;
}
int first = 0;
int last = n-1;
T * temp = new T[n];
recursionMerge(a,first,last,temp);
}
template <class T> void allSort<T>::recursionMerge(T a[],int first,int last,T temp[])
{
int mid = (first+last)/2;
if(first<last)
{
recursionMerge(a,first,mid,temp);
recursionMerge(a,mid+1,last,temp);
mergeArray(a,first,mid,last,temp);
}
}
template <class T> void allSort<T>::mergeArray(T a[],int first,int mid,int last,T temp[])
{
int i = first;
int j = mid+1;
int m = mid;
int n = last;
int k = 0;
while(i<=m&&j<=n)
{
if(a[i]<a[j])
temp[k++] = a[i++];
else
temp[k++] = a[j++];
}
while(i<=m)
{
temp[k++] = a[i++];
}
while(j<=n)
{
temp[k++] = a[j++];
}
for (int index = 0; index < k; ++index)
{
a[first+index]=temp[index];
}
}
template <class T> void allSort<T>::quickSort(T a[],int n)
{
if (a==NULL || n<=0)
{
cout << "输入参数有问题!" << endl;
return ;
}
QuickSort(a,0,n-1);
}
template <class T> void allSort<T>::QuickSort(T a[],int low,int high)
{
int i = low;
int j = high;
int key = a[low];
while(low<high)
{
while(low<high && a[high]>=key)
--high;
a[low] = a[high];
while(low<high && a[low]<=key)
++low;
a[high] = a[low];
}
a[low] = key;
if(low<high)
{
QuickSort(a,i,low-1);
QuickSort(a,low+1,j);
}
}
template <class T> void allSort<T>::heapSort(T a[],int n)
{
if (a==NULL || n<=0)
{
cout << "输入参数有问题!" << endl;
return ;
}
int size;
// createMinHeap(a,n);
createMaxHeap(a,n);
for(size=n-1;size>0;size--)
{
swap(a[0],a[size]);
// filterMinHeap(a,0,size);
filterMaxHeap(a,0,size);
}
}
template <class T> void allSort<T>::createMinHeap(T a[],int n)
{
//为什么是(n-2)/2?本应该是(n-1)/2;但是因为下取整,分子上再减一
int start = (n-2)/2;
for (start = (n-2)/2; start >=0; start--)
{
filterMinHeap(a,start,n);
}
}
template <class T> void allSort<T>::createMaxHeap(T a[],int n)
{
//为什么是(n-2)/2?本应该是(n-1)/2;但是因为下取整,分子上再减一
int start = (n-2)/2;
for (start = (n-2)/2; start >=0; start--)
{
filterMaxHeap(a,start,n);
}
}
//调整堆,以循环的方式实现,调整为最小堆
template <class T> void allSort<T>::filterMinHeap(T a[],int start, int n)
{
int i=start;
int j=2*i+1;
T temp = a[i];
while(j<n)
{
if(j+1<n && a[j]>a[j+1])
++j;
if(a[j]>temp)
break;
else
{
a[i] = a[j];
i=j;
j=2*i+1;
}
a[i]=temp;
}
}
//调整堆,以递归的方式实现,调整为最大堆
template <class T> void allSort<T>::filterMaxHeap(T a[],int node,int n)
{
int left = 2*node+1;
int right = 2*node+2;
int largest = node;
if (left<n && a[left]>a[node])
largest = left;
if (right<n && a[right]>a[largest])
largest = right;
if (largest!=node)
{
swap(a[largest],a[node]);
filterMaxHeap(a,largest,n);
}
}
template <class T> void allSort<T>::printArray(T a[],int n)
{
for(int i=0;i<n;++i)
cout << a[i] << " " << flush;
cout << endl;
}