forked from wangzheng0822/algo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sort.c
executable file
·268 lines (226 loc) · 4.86 KB
/
sort.c
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
/*************************************************************************
> File Name: sort.c
> Author: jinshaohui
> Mail: [email protected]
> Time: 18-10-20
> Desc:
************************************************************************/
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<assert.h>
void dump(int a[],int size)
{
int i = 0;
printf("\r\n");
for(i = 0; i <size; i++)
{
printf("%d ",a[i]);
}
printf("\r\n");
}
/*计数排序,时间复杂度0(n),非原地排序
*计数排序也是利用桶排序的解决方式
* 如果数组最大值max比数组大小size大很多不适合;
* 计数排序要求时非负整数
* */
void count_sort(int a[],int size)
{
int i = 0;
int max = 0;
int *count = 0;
int *res = 0;
/*找到最大数*/
for (i = 0 ; i< size; i++)
{
if (a[i] > max)
{
max = a[i];
}
}
count = (int *)malloc(sizeof(int)*(max + 1));
assert(count != NULL);
memset(count,0,sizeof(int)*(max + 1));
/*计数*/
for (i = 0; i < size;i++)
{
count[a[i]]++;
}
/*依次累加*/
for(i = 1 ;i <= max; i ++)
{
count[i] += count[i-1];
}
res = (int *)malloc(sizeof(int)*(size));
assert(res != NULL);
/*核心代码,count[a[i] - 1]就是排序好的下标*/
for (i = size-1;i >= 0; i--)
{
res[count[a[i]] -1] = a[i];
count[a[i]]--;
}
memcpy(a,res,size*(sizeof(int)));
free(res);
free(count);
return;
}
int count_sort_test()
{
int a [10]={1,5,6,8,10,9,3,1,2,1};
printf("\r\n conunt sort test ....");
count_sort(a,10);
dump(a,10);
return 0;
}
#define NUM_OF_POS(a,pval) ((a)/pval)%10
void radix_sort(int a[],int size,int num_count)
{
int count[10] = {0}; /*计数*/
int *pres = NULL;
int i = 0;
int j = 0;
int pval = 10;
int index = 0;
int break_flg = 0;
pres = (int *)malloc(sizeof(int)*size);
assert(pres != NULL);
for (i = 0; i < num_count; i ++)
{
memset(count,0,sizeof(int)*10);
/*求当前的基数*/
pval = pow(10,i);
/*计数*/
for (j = 0; j < size; j++)
{
index = NUM_OF_POS(a[j],pval);
count[index]++;
}
/*小的优化,可能位数最大的就1,其他的位数差很多*/
if(count[0] == 9)
{
break_flg++;
}
if(break_flg >=2)
{
printf("\r\n %i",i);
break;
}
/*累加*/
for(j = 1; j < 10; j ++)
{
count[j] += count[j-1];
}
/*排序必须从后往前,否则不是稳定排序*/
for(j = size -1; j >= 0; j--)
{
index = NUM_OF_POS(a[j],pval);
pres[count[index] - 1] = a[j];
count[index]--;
}
/*本轮排序好的,拷贝到a中*/
memcpy(a,pres,sizeof(int)*size);
}
return;
}
void radix_sort_test()
{
int a[10] = {123,12341,1232134,124,236,128,1112313129,98,9,8989};
printf("\r\n radix sort test.....");
radix_sort(a,10,10);
dump(a,10);
return;
}
struct barrel {
int node[10];
int count;/* the num of node */
};
int partition(int a[],int left,int right)
{
int i = left;
int j = right;
int key = a[left];
while(i < j)
{
while((i < j)&& (a[j] >= key))
{
j--;
}
if (i < j)
{
a[i] = a[j];
}
while((i < j) && a[i] <= key)
{
i++;
}
if (i<j)
{
a[j] = a[i];
}
}
a[i] = key;
return i;
}
void quick_sort(int a[],int left,int right)
{
int q = 0;
/*递归终止条件*/
if (left >= right)
{
return;
}
q = partition(a,left,right);
quick_sort(a,left,(q - 1));
quick_sort(a,(q + 1),right);
return;
}
void bucket_sort(int data[], int size)
{
int max, min, num, pos;
int i, j, k;
struct barrel *pBarrel;
max = min = data[0];
for (i = 1; i < size; i++) {
if (data[i] > max) {
max = data[i];
} else if (data[i] < min) {
min = data[i];
}
}
num = (max - min + 1) / 10 + 1;
pBarrel = (struct barrel*)malloc(sizeof(struct barrel) * num);
memset(pBarrel, 0, sizeof(struct barrel) * num);
/* put data[i] into barrel which it belong to */
for (i = 0; i < size; i++) {
k = (data[i] - min + 1) / 10;/* calculate the index of data[i] in barrel */
(pBarrel + k)->node[(pBarrel + k)->count] = data[i];
(pBarrel + k)->count++;
}
pos = 0;
for (i = 0; i < num; i++) {
if ((pBarrel + i)->count != 0)
{
quick_sort((pBarrel+i)->node, 0, ((pBarrel+i)->count)-1);/* sort node in every barrel */
for (j = 0; j < (pBarrel+i)->count; j++) {
data[pos++] = (pBarrel+i)->node[j];
}
}
}
free(pBarrel);
}
void bucket_sort_test()
{
int a[] = {78, 17, 39, 26, 72, 94, 21, 12, 23, 91};
int size = sizeof(a) / sizeof(int);
printf("\r\n bucket sort test ...");
bucket_sort(a, size);
dump(a,size);
}
int main()
{
count_sort_test();
radix_sort_test();
bucket_sort_test();
return 0;
}