-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquick_sort.c
90 lines (67 loc) · 1.94 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
#include <stdio.h>
#include <string.h>
#include <strings.h>
#include <stdlib.h>
#define ARRAY_SIZE 100
void swap(int *pa, int *pb);
void quick_sort(int sortArray[], int lowIndex, int highIndex);
void print_list(int list[], int size, char const * fmt);
int main(int argc, char *argv[]) {
int array[ARRAY_SIZE];
int i;
for (i = 0; i < ARRAY_SIZE; i++) {
array[i] = rand() % 1000;
}
print_list(array, ARRAY_SIZE, "排序之前的序列");
quick_sort(array, 0, ARRAY_SIZE - 1);
print_list(array, ARRAY_SIZE, "快速排序之后的序列");
return 0;
}
void swap(int *pa, int *pb) {
int temp;
temp = *pa;
*pa = *pb;
*pb = temp;
}
void quick_sort(int sortArray[], int lowIndex, int highIndex) {
int modIndex;
int key;
int i, j;
if (lowIndex < highIndex) {
modIndex = (lowIndex + highIndex) / 2;
swap(&sortArray[lowIndex], &sortArray[modIndex]);
key = sortArray[lowIndex];
i = lowIndex + 1;
j = highIndex;
while (i <= j) {
/* looking bigger than key from beginning of list */
while ((i <= highIndex)
&& (sortArray[i] <= key))
{
i++;
}
/* looking smaller than key from end of list */
while ((j >= lowIndex)
&& (sortArray[j] > key))
{
j--;
}
/* swap bigger and smaller value */
if (i < j) {
swap(&sortArray[i], &sortArray[j]);
}
}
/* swap key and smaller */
swap(&sortArray[lowIndex], &sortArray[j]);
quick_sort(sortArray, lowIndex, j - 1);
quick_sort(sortArray, j + 1, highIndex);
}
}
void print_list(int list[], int size, char const * fmt) {
int i;
printf("%s:\t", fmt);
for (i = 0; i < size; i++) {
printf("%d ", list[i]);
}
printf("\n");
}