-
Notifications
You must be signed in to change notification settings - Fork 2
/
quick_sort.c
379 lines (337 loc) · 9.55 KB
/
quick_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
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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
/**
* Copyhigh © https://github.com/jarry All rights reserved.
* @author: [email protected]
* @version: 1.0
*/
#include <stdio.h>
#include <math.h>
#include <time.h>
#include <stdbool.h>
#include <stdlib.h>
void printArray(int *arr, int len)
{
for (int i = 0; i < len; i++)
{
printf("%d ", arr[i]);
}
}
void printArray2(int *arr, int start, int end)
{
printf("[");
for (int i = start; i < end; i++)
{
printf("%d", arr[i]);
if (i != end - 1)
printf(", ");
}
printf("]");
}
void swap(int *a, int *b)
{
int t = *a;
*a = *b;
*b = t;
}
/** 方式1,标准递归版本。需要左右不断交换,无需新建数组。*/
void *quickSort1(int arr[], int low, int high)
{
int i = low > 0 ? low : 0;
int j = high;
int midIndex = (i + j) / 2;
int pivot = arr[midIndex];
// 当左侧小于等于右侧则表示还有值没有对比,需要继续
while (i <= j)
{
// 当左侧小于基准时查找位置右移,直到找出比基准值大的位置来
while (arr[i] < pivot)
{
printf("\r\narr[i] < pivot: i=%d, j=%d, pivot=%d", i, j, pivot);
i++;
}
// 当前右侧大于基准时左移,直到找出比基准值小的位置来
while (arr[j] > pivot)
{
printf("\r\narr[i] > pivot: i=%d, j=%d, pivot=%d", i, j, pivot);
j--;
}
printf("\r\n low=%d, high=%d, i=%d, j=%d, midIndex=%d, pivot=%d", low, high, i, j, midIndex, pivot);
// 当左侧位置小于右侧时,将数据交换,小的交换到基准左侧,大的交换到右侧
if (i <= j)
{
swap(&arr[i], &arr[j]);
// 缩小搜查范围,直到左侧都小于基数,右侧都大于基数
i++;
j--;
}
}
// 左侧小于基数位置,不断递归左边部分
if (low < j)
{
printf("\r\n low < j:recursion: low=%d, high=%d, i=%d, j=%d, midIndex=%d, pivot=%d", low, high, i, j, midIndex, pivot);
quickSort1(arr, low, j);
}
// 基数位置小于右侧,不断递归右侧部分
if (i < high)
{
printf("\r\n i < high:recursion: low=%d, high=%d, i=%d, j=%d, midIndex=%d, pivot=%d", low, high, i, j, midIndex, pivot);
quickSort1(arr, i, high);
}
return arr;
}
/* 快排方式2,递归分区交换 */
// 数组分区写法,以左侧第1个数为基准,将大于基准的数放在右侧
int partition(int *arr, int left, int right)
{
// 基数位置以左侧为准
int pivotIndex = left;
int partitionIndex = pivotIndex + 1;
for (int i = partitionIndex; i <= right; i++)
{
// 当比较项小于基数时,将比较项逐个挪到左侧,同时左侧下标移动1位
if (arr[i] < arr[pivotIndex])
{
swap(&arr[i], &arr[partitionIndex]);
partitionIndex++;
}
}
// 将基数项调整到最新交换位置,交换位置-1就是分割线
swap(&arr[pivotIndex], &arr[partitionIndex - 1]);
printf("\r\n");
printArray2(arr, left, partitionIndex);
printf(" partitionIndex=%d, arr[partitionIndex]=%d ", partitionIndex, arr[partitionIndex]);
printArray2(arr, partitionIndex, right);
return partitionIndex - 1;
}
// 快排方式2,递归分区交换
int *quickSort2(int *arr, int left, int right)
{
if (left < right)
{
int pivot = partition(arr, left, right);
quickSort2(arr, left, pivot - 1);
quickSort2(arr, pivot + 1, right);
}
return arr;
}
/** 方式3,非递归版本。先建立栈,或引入外部stack库。*/
/* defined stack */
typedef int item;
typedef struct stack *Stack;
Stack stack_create(int);
void stack_println(Stack);
bool stack_push(Stack, item);
item stack_pop(Stack);
bool stack_is_full(Stack);
bool stack_is_empty(Stack);
void stack_make_empty(Stack);
int stack_len(Stack);
/* stack.c */
struct stack
{
int top;
int size;
item *data_array;
};
Stack stack_create(int size)
{
Stack s = malloc(sizeof(struct stack));
if (s == NULL)
{
return NULL;
}
s->data_array = malloc(sizeof(int) * size);
if (s->data_array == NULL)
{
return NULL;
}
s->top = 0;
s->size = size;
return s;
}
void stack_println(Stack s)
{
for (int i = 0; i < s->top; i++)
{
printf("%d ", s->data_array[i]);
}
printf("\n");
}
bool stack_is_full(Stack s)
{
return s->top == s->size;
}
bool stack_is_empty(Stack s)
{
return s->top == 0;
}
bool stack_push(Stack s, item data)
{
if (stack_is_full(s))
{
return false;
}
s->data_array[s->top++] = data;
return true;
}
item stack_pop(Stack s)
{
if (stack_is_empty(s))
{
return 0;
}
return s->data_array[--s->top];
}
void stack_make_empty(Stack s)
{
s->top = 0;
return;
}
int stack_len(Stack s)
{
return s->top;
}
/** 非递归版本。需要交换,无需新建数组,利用stack或queue遍历。*/
void *quickSortNotRecurion(int arr[], int low, int high)
{
Stack stack = stack_create(high - low);
printf("\nhigh - low = %d\n", high - low);
int i, j, midIndex, pivot, tmp;
// 与标准递归版相同,只是将递归改为遍历栈的方式
// 先将左右各取一个入栈
stack_push(stack, low);
stack_push(stack, high);
while (!stack_is_empty(stack))
{
// 如果栈内还有数据,则一并马上取出,其他逻辑与标准递归版同
j = high = stack_pop(stack);
i = low = stack_pop(stack);
midIndex = (i + j) / 2;
pivot = arr[midIndex];
while (i <= j)
{
while (arr[i] < pivot)
{
printf("\r\narr[i] < pivot: i=%d, j=%d, arr[i]=%d, arr[j]=%d, pivot=%d", i, j, arr[i], arr[j], pivot);
i++;
}
while (arr[j] > pivot)
{
printf("\r\narr[i] > pivot: i=%d, j=%d, arr[i]=%d, arr[j]=%d, pivot=%d", i, j, arr[i], arr[j], pivot);
j--;
}
if (i <= j)
{
tmp = arr[j];
arr[j] = arr[i];
arr[i] = tmp;
i++;
j--;
}
}
if (low < j)
{
// 与递归版不同,这里添加到栈中,以便继续循环
printf("\r\n low < j:recursion: low=%d, high=%d, i=%d, j=%d, midIndex=%d, pivot=%d", low, high, i, j, midIndex, pivot);
stack_push(stack, low);
stack_push(stack, j);
}
if (i < high)
{
printf("\r\n i < high:recursion: low=%d, high=%d, i=%d, j=%d, midIndex=%d, pivot=%d", low, high, i, j, midIndex, pivot);
stack_push(stack, i);
stack_push(stack, high);
}
}
free(stack);
return arr;
}
// test
int main()
{
printf("\r\n sort start\r\n");
// test quicksort1
printf("\r\n==quick1 origin==\r\n");
int arr1[7] = {7, 11, 9, 10, 12, 13, 8};
int len1 = sizeof(arr1) / sizeof(arr1[0]);
printArray(arr1, len1);
float startTime = clock();
int *newArr1 = quickSort1(arr1, 0, len1 - 1);
// int *newArr = quickSortNotRecurion(arr, 0, len - 1);
printf("\r\nquick1 sorted:");
printArray(newArr1, len1);
printf("\ntime: %f ms.", ((clock() - startTime) / CLOCKS_PER_SEC * 1000));
printf("\n==quick1 end==\n");
// test quicksort1
printf("\r\n==quick2 origin==\r\n");
int arr2[7] = {7, 11, 9, 10, 12, 13, 8};
int len2 = sizeof(arr2) / sizeof(arr2[0]);
printArray(arr2, len2);
startTime = clock();
int *newArr2 = quickSort2(arr2, 0, len2 - 1);
printf("\r\n quick2 sorted:");
printArray(newArr2, len2);
printf("\ntime: %f ms.", ((clock() - startTime) / CLOCKS_PER_SEC * 1000));
printf("\n==quick2 end==\n");
// test quicksort3
printf("\r\n==quick3 origin==\r\n");
int arr3[7] = {7, 11, 9, 10, 12, 13, 8};
int len3 = sizeof(arr3) / sizeof(arr3[0]);
printArray(arr3, len3);
startTime = clock();
int *newArr3 = quickSortNotRecurion(arr3, 0, len3 - 1);
printf("\r\n quick3 sorted:");
printArray(newArr3, len3);
printf("\ntime: %f ms.", ((clock() - startTime) / CLOCKS_PER_SEC * 1000));
printf("\n==quick3 end==\n");
}
/**
jarry@jarrys-MacBook-Pro quicksort % gcc quick_sort.c
jarry@jarrys-MacBook-Pro quicksort % ./a.out
sort start
==quick1 origin==
7 11 9 10 12 13 8
arr[i] < pivot: i=0, j=6, pivot=10
low=0, high=6, i=1, j=6, midIndex=3, pivot=10
arr[i] < pivot: i=2, j=5, pivot=10
arr[i] > pivot: i=3, j=5, pivot=10
arr[i] > pivot: i=3, j=4, pivot=10
low=0, high=6, i=3, j=3, midIndex=3, pivot=10
low < j:recursion: low=0, high=6, i=4, j=2, midIndex=3, pivot=10
arr[i] < pivot: i=0, j=2, pivot=8
arr[i] > pivot: i=1, j=2, pivot=8
low=0, high=2, i=1, j=1, midIndex=1, pivot=8
i < high:recursion: low=0, high=6, i=4, j=2, midIndex=3, pivot=10
arr[i] < pivot: i=4, j=6, pivot=13
low=4, high=6, i=5, j=6, midIndex=5, pivot=13
low < j:recursion: low=4, high=6, i=6, j=5, midIndex=5, pivot=13
low=4, high=5, i=4, j=5, midIndex=4, pivot=12
quick1 sorted:7 8 9 10 11 12 13
time: 0.046000 ms.
==quick1 end==
==quick2 origin==
7 11 9 10 12 13 8
[7] partitionIndex=1, arr[partitionIndex]=11 [11, 9, 10, 12, 13]
[8, 9, 10, 11] partitionIndex=5, arr[partitionIndex]=13 [13]
[8] partitionIndex=2, arr[partitionIndex]=9 [9]
[9] partitionIndex=3, arr[partitionIndex]=10 []
[12, 13] partitionIndex=7, arr[partitionIndex]=0 []
quick2 sorted:7 8 9 10 11 12 13
time: 0.023000 ms.
==quick2 end==
==quick3 origin==
7 11 9 10 12 13 8
high - low = 6
arr[i] < pivot: i=0, j=6, arr[i]=7, arr[j]=8, pivot=10
arr[i] < pivot: i=2, j=5, arr[i]=9, arr[j]=13, pivot=10
arr[i] > pivot: i=3, j=5, arr[i]=10, arr[j]=13, pivot=10
arr[i] > pivot: i=3, j=4, arr[i]=10, arr[j]=12, pivot=10
low < j:recursion: low=0, high=6, i=4, j=2, midIndex=3, pivot=10
i < high:recursion: low=0, high=6, i=4, j=2, midIndex=3, pivot=10
arr[i] < pivot: i=4, j=6, arr[i]=12, arr[j]=11, pivot=13
low < j:recursion: low=4, high=6, i=6, j=5, midIndex=5, pivot=13
arr[i] < pivot: i=0, j=2, arr[i]=7, arr[j]=9, pivot=8
arr[i] > pivot: i=1, j=2, arr[i]=8, arr[j]=9, pivot=8
quick3 sorted:7 8 9 10 11 12 13
time: 0.075000 ms.
==quick3 end==
*/