基于比较的排序算法集

  1 #include <iostream>
2
3 using namespace std;
4
5
6
7 #define SWAP(i,j) {int t=(i);(i)=(j);(j)=t;}
8
9
10
11 //插入排序
12
13 void InsertSort(int*a,int len)
14
15 {
16
17 for (int i=1;i<len;i++)
18
19 {
20
21 int j=i,x=a[i];
22
23 while (j && a[j-1]>x)a[j]=a[j-1],j--;
24
25 a[j]=x;
26
27 }
28
29 }
30
31
32
33 //选择排序
34
35 void SelectSort(int*a,int len)
36
37 {
38
39 for (int i=1,j,k;i<len;i++)
40
41 {
42
43 for (j=i,k=i-1;j<len;j++)
44
45 if (a[j]<a[k])k=j;
46
47 if (k>=i)SWAP(a[i-1],a[k]);
48
49 }
50
51 }
52
53
54
55 //冒泡排序
56
57 void BubbletSort(int*a,int len)
58
59 {
60
61 for (bool bSwap=true;bSwap;len--)
62
63 {
64
65 bSwap=false;
66
67 for (int j=1;j<len;j++)
68
69 if (a[j-1]>a[j]){SWAP(a[j-1],a[j]);bSwap=true;}
70
71 }
72
73 }
74
75
76
77 //堆调整
78
79 void HeapAdjust(int *a,int root,int len)
80
81 {
82
83 int child,x=a[root];
84
85 while (child=root<<1|1,child<len)
86
87 {
88
89 if (child<len-1 && a[child]<a[child+1])child++;
90
91 if (x<a[child]){a[root]=a[child];root=child;}
92
93 else break;
94
95 }
96
97 a[root]=x;
98
99 }
100
101
102
103 //堆排序
104
105 void HeapSort(int*a,int len)
106
107 {
108
109 for (int i=len/2-1;i>=0;i--)
110
111 HeapAdjust(a,i,len);
112
113 while (--len)
114
115 {
116
117 SWAP(a[0],a[len]);
118
119 HeapAdjust(a,0,len);
120
121 }
122
123 }
124
125
126
127 //归并
128
129 void Merge(int*a,int len1,int len2)
130
131 {
132
133 int *a1=new int[len1+1],*a2=new int[len2+1],len=len1+len2;
134
135 for (int i=0;i<len1;i++)
136
137 a1[i]=a[i];
138
139 for (int i=0;i<len2;i++)
140
141 a2[i]=a[len1+i];
142
143 a1[len1]=a2[len2]=INT_MAX;
144
145 for (int i=0,j=0,k=0;k<len;k++)
146
147 if (a1[i]<a2[j])a[k]=a1[i++];
148
149 else a[k]=a2[j++];
150
151 delete[] a1;delete[] a2;
152
153 }
154
155
156
157 //归并排序
158
159 void MergeSort(int*a,int len)
160
161 {
162
163 if (len>1)
164
165 {
166
167 int c=len/2;
168
169 MergeSort(a,c);
170
171 MergeSort(a+c,len-c);
172
173 Merge(a,c,len-c);
174
175 }
176
177 }
178
179
180
181 //划分
182
183 int Partition(int*a,int len)
184
185 {
186
187 int x=a[--len],i=-1;
188
189 for (int j=0;j<len;j++)
190
191 if (a[j]<x){i++;SWAP(a[i],a[j]);}
192
193 SWAP(a[i+1],a[len]);
194
195 return i+1;
196
197 }
198
199
200
201 //快速排序
202
203 void QuickSort(int*a,int len)
204
205 {
206
207 if (len > 0)
208
209 {
210
211 int q=Partition(a,len);
212
213 if (q<len-q)
214
215 {
216
217 QuickSort(a,q-1);
218
219 QuickSort(a+q+1,len-q-1);
220
221 }
222
223 else
224
225 {
226
227 QuickSort(a+q+1,len-q-1);
228
229 QuickSort(a,q-1);
230
231 }
232
233 }
234
235 }
236
237
238
239 //希尔插入
240
241 void ShellInsert(int*a,int inc,int len)
242
243 {
244
245 for (int i=inc;i<len;i+=inc)
246
247 {
248
249 int j=i,x=a[i];
250
251 while (j>0 && a[j-inc]>x)a[j]=a[j-inc],j-=inc;
252
253 a[j]=x;
254
255 }
256
257 }
258
259
260
261 //插入式希尔排序
262
263 void ShellSort(int*a,int len)
264
265 {
266
267 int inc=len;
268
269 do
270
271 {
272
273 inc=inc/3+1;
274
275 for(int s=0;s<inc;s++)
276
277 ShellInsert(a-s,inc,len+s);
278
279 }
280
281 while (inc>1);
282
283 }

转载于:https://www.cnblogs.com/baohang/archive/2011/11/20/2255859.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值