包括MergeSort, QuickSort, 包括数组和链表的实现
1
// SortAlgorithm.cpp : Defines the entry point for the console application.
2
//
3
4
#include "stdafx.h"
5
#include <iostream>
6
#include <time.h>
7
#include "CProTimer.h"
8
9
using namespace std;
10
const int nSize = 500000 ;
11
12
// MergeSort
13
void MergeSort( int nlow, int nhigh);
14
void Merge( int nlow, int nmid, int nhigh);
15
int A[nSize];
16
int B[nSize]; // temp array
17
18
// MergeSortL
19
int MergeSortL( int low, int high);//, int p
20
int MergeL( int q, int r );//返回int p
21
int InsertSortL( int low, int high);//, int p
22
int AL[nSize+1]; //比较特殊,第0个不用
23
int Link[nSize+1]; // linked list
24
25
//QuickSort
26
int Partition( int m, int p);
27
void QuickSort( int p, int q);
28
int Aq[nSize];
29
void InsertSort(int p,int q);
30
31
int _tmain(int argc, _TCHAR* argv[])
32
{
33
srand( (unsigned)time( NULL ) );
34
for (int i=1; i<nSize+1; i++)
35
{
36
AL[i] = (int)rand() ;
37
Link[i]=i+1;
38
39
Aq[i-1] = AL[i];
40
}
41
Link[0]=1;
42
Link[nSize]=0;
43
//MergeSort( 0, nSize-1);
44
45
// Aq[3]=Aq[0];
46
//for (int i=0; i<nSize; i++)
47
//{
48
// cout<< Aq[i] << " ";
49
50
//}
51
cout<< endl;
52
53
// int s1=CTime::GetCurrentTime();
54
//int e1=CTime::GetCurrentTime();
55
56
CProTimer proTimer;
57
proTimer.Reset();
58
MergeSortL(1,nSize);
59
float f=proTimer.GetTimeMicro(TRUE);
60
cout<<"MergeSort: "<<nSize<<"个耗时(Microsecond): "<< f <<endl;
61
62
// s1=GetTickCount();
63
proTimer.Reset();
64
QuickSort(0,nSize-1) ;
65
// e1=GetTickCount();
66
f=proTimer.GetTimeMicro(TRUE);
67
cout<<"QuickSort: "<<nSize<<"个耗时(Microsecond): "<< f << endl;
68
69
//for (int i=0; i<nSize; i++)
70
//{
71
//
72
// cout<< Aq[i] << " ";
73
74
//}
75
//cout<<endl<<"quicksort data:"<<endl;
76
//int i=0;
77
//while (1)
78
//{
79
// int temp=AL[i];
80
// i=Link[i];
81
// if (i==0)
82
// {
83
// break;
84
// }
85
// cout<<AL[i]<<" ";
86
//}
87
88
;
89
//for (int i=0; i<(nSize-1); i++)
90
//{
91
// cout<< (Aq[i+1]-Aq[i]) << " ";
92
// if ((Aq[i+1]-Aq[i])<0)
93
// {
94
// cout<<endl<<"*****"<<endl;
95
// }
96
//
97
//}
98
99
return 0;
100
}
101
102
void MergeSort( int nlow, int nhigh)
103
{
104
if ( nlow < nhigh )
105
{
106
int nmid = ( nlow + nhigh ) / 2;
107
108
MergeSort( nlow, nmid);
109
110
111
MergeSort( (nmid+1) , nhigh);
112
113
Merge( nlow, nmid, nhigh );
114
}
115
}
116
117
void Merge( int nlow, int nmid, int nhigh )
118
{
119
int h, i, j, k;
120
//
121
h = nlow; i = nlow; j = nmid +1;
122
123
while ( h <= nmid && j <= nhigh)
124
{
125
if ( A[h] <= A[j] )
126
{
127
B[i++] = A[h++];
128
129
}
130
else
131
{
132
B[i++] = A[j++];
133
}
134
}
135
136
//while (j <=nhigh)
137
//{
138
// B[i++]=A[j++];
139
//}
140
//while (h <=nmid)
141
//{
142
// B[i++]=A[h++];
143
//}
144
if( h > nmid)
145
for ( k= j; k <=nhigh; k ++)
146
{
147
B[i]=A[k];
148
++i;
149
}
150
else
151
for (k = h; k <=nmid; k ++)
152
{
153
B[i]=A[k];
154
++i;
155
}
156
157
for ( k = nlow; k <=nhigh; k ++)
158
{
159
A[k] = B[k];
160
}
161
162
}
163
164
// MergeSortL
165
int MergeSortL(int low, int high)
166
{
167
if ( high - low < 10)
168
{
169
return InsertSortL(low,high);
170
}
171
else
172
{
173
int mid = (low+high)/2;
174
int q=MergeSortL(low, mid);
175
int r=MergeSortL(mid+1,high);
176
return MergeL(q,r);
177
}
178
}
179
int MergeL( int q, int r)
180
{
181
int i=q,j=r,k=0;
182
while (i!=0&&j!=0)
183
{
184
if (AL[i] <= AL[j])
185
{
186
Link[k]=i;
187
k=i;
188
i=Link[i];
189
}
190
else
191
{
192
Link[k]=j;
193
k=j;
194
j=Link[j];
195
}
196
197
}
198
if (i==0)
199
{
200
Link[k]=j;
201
}
202
else
203
Link[k]=i;
204
205
return Link[0];
206
207
}
208
int InsertSortL( int low, int high)
209
{
210
for (int i=low;i<=high;i++)
211
{
212
for (int j=i;j<=high;j++)
213
{
214
if (AL[j]<AL[i])
215
{
216
int temp = AL[i];
217
AL[i]=AL[j];
218
AL[j]=temp;
219
}
220
}
221
}
222
Link[high]=0;
223
return low;
224
}
225
226
// Quick Sort
227
int Partition(int m, int p)
228
{
229
int i;
230
int v = Aq[m];
231
int t1=m,t2=p;
232
int v1=Aq[p],v2=Aq[(m+p)/2]; //选择首,中,尾三个数中的中间值
233
if ((v-v1)*(v-v2)>0)
234
{
235
if (v>v1)
236
{
237
if (v1>v2)
238
{
239
Aq[m]=v1;
240
Aq[p]=v;
241
v=v1;
242
}
243
else
244
{
245
Aq[m]=v2;
246
Aq[(m+p)/2]=v;
247
v=v2;
248
}
249
}
250
else
251
{
252
if (v1<v2)
253
{
254
Aq[m]=v1;
255
Aq[p]=v;
256
v=v1;
257
}
258
else
259
{
260
Aq[m]=v2;
261
Aq[(m+p)/2]=v;
262
v=v2;
263
}
264
265
}
266
}
267
268
i=m;
269
//p=p-1;
270
while (1)
271
{
272
while (Aq[i]<=v)
273
{
274
i++;
275
}
276
while (Aq[p]>=v&&p>m)
277
{
278
p--;
279
}
280
if ( i < p)
281
{
282
int temp = Aq[i];
283
Aq[i]=Aq[p];
284
Aq[p]=temp;
285
}
286
else
287
break;
288
289
}
290
291
if (p<0)
292
{
293
cout<<"";
294
}
295
Aq[m]=Aq[p]; Aq[p]=v;
296
return p;
297
298
}
299
300
void QuickSort( int p, int q)
301
{
302
if (q>p)
303
{
304
if ((q-p)<10)
305
{
306
InsertSort(p,q);
307
}
308
else
309
{
310
int x=Partition(p,q);
311
QuickSort(p,x-1);
312
QuickSort(x+1,q);
313
}
314
315
}
316
}
317
318
void InsertSort(int p,int q)
319
{
320
for (int i=p;i<=q;i++)
321
{
322
for (int j=i;j<=q;j++)
323
{
324
if (Aq[j]<Aq[i])
325
{
326
int temp = Aq[i];
327
Aq[i]=Aq[j];
328
Aq[j]=temp;
329
}
330
}
331
}
332
}

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
