-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathquick_sort.c
180 lines (169 loc) · 5 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
#include "quick_sort.h"
int _quick_sort_from_side_incremental (int cards[], size_t card_num)
{
size_t base;
if (card_num > 5){
base = random() % card_num;
}
else base = 0;
if (base) swap_int(cards, &cards[base]); //swap cards[0] and cards[base-1]
size_t base_final_pos = 0;
for (size_t i = 1; i <= card_num - 1; i++){
if (cards[i] < cards[0]){
base_final_pos++;
swap_int(&cards[base_final_pos], &cards[i]);
}
}
if (base_final_pos) swap_int(&cards[0], &cards[base_final_pos]);
size_t left_card_num, right_card_num;
left_card_num = base_final_pos;
right_card_num = card_num - left_card_num - 1;
if (left_card_num > K){
if (_quick_sort_from_side_incremental(cards, left_card_num)) return 2;
}else{
if (left_card_num > 1) _insert_sort_incremental(cards, left_card_num);
}
if (right_card_num > K){
if (_quick_sort_from_side_incremental(cards + base_final_pos + 1, right_card_num)) return 2;
}else{
if (right_card_num > 1) _insert_sort_incremental(cards + base_final_pos + 1, right_card_num);
}
return 0;
}
int _quick_sort_from_side_decremental (int cards[], size_t card_num)
{
size_t base;
if (card_num > 5){
time_t cur_time = time(NULL);
if (cur_time == (time_t)-1) return 1;
srandom((unsigned int)cur_time);
base = random() % card_num;
}
else base = 0;
swap_int(cards, &cards[base - 1]); //swap cards[0] and cards[base-1]
int base_final_pos = 0;
for (size_t i = 1; i <= card_num - 1; i++){
if (cards[i] > cards[0]){
base_final_pos++;
swap_int(&cards[base_final_pos], &cards[i]);
}
}
swap_int(&cards[0], &cards[base_final_pos]);
size_t left_card_num, right_card_num;
left_card_num = base_final_pos;
right_card_num = card_num - left_card_num - 1;
if (left_card_num > K){
if (_quick_sort_from_side_decremental(cards, left_card_num)) return 2;
}else{
if (left_card_num > 1) _insert_sort_decremental(cards, left_card_num);
}
if (right_card_num > K){
if (_quick_sort_from_side_decremental(cards + base_final_pos + 1, right_card_num)) return 2;
}else{
if (right_card_num > 1) _insert_sort_decremental(cards + base_final_pos + 1, right_card_num);
}
return 0;
}
int init_random (void)
{
struct timespec ts;
unsigned int rand;
if (clock_gettime(CLOCK_MONOTONIC, &ts) == -1) return 1;
rand = ts.tv_sec * ts.tv_nsec + ts.tv_sec + ts.tv_nsec;
srandom((unsigned int)rand);
return 0;
}
int quick_sort_from_side (int cards[], size_t card_num, int order)
{
if (cards == NULL || card_num == 0 || order == 0) return 1;
if (card_num == 1) return 0;
if (init_random()) return 2; //init_random(inner:clock_gettime) failed.
if (order > 0){
if (card_num == 2){
if (cards[0] > cards[1]) swap_int(&cards[0], &cards[1]);
}else if (card_num <= K) _insert_sort_incremental(cards, card_num);
else{
if (_quick_sort_from_side_incremental(cards, card_num)) return 3;
}
}else{
if (card_num == 2){
if (cards[0] < cards[1]) swap_int(&cards[0], &cards[1]);
}else if (card_num <= K) _insert_sort_decremental(cards, card_num);
else{
if (_quick_sort_from_side_decremental(cards, card_num)) return 4;
}
}
return 0;
}
/* test part */
/*
#include <stdio.h>
#include "assist.h"
int get_random_number (int *num, int max)
{
struct timespec ts;
unsigned int rand;
if (clock_gettime(CLOCK_MONOTONIC, &ts) == -1) return 1;
rand = ts.tv_sec * ts.tv_nsec + ts.tv_sec + ts.tv_nsec;
srandom(rand);
double f;
f = 1.0 * max * random() / (RAND_MAX + 1LL);
*num = f;
return 0;
}
int randomize_cards (int cards[], size_t card_num)
{
if (card_num == 1) return 0;
else if (card_num == 2){
swap_int(&cards[0], &cards[1]);
return 0;
}
static int previous1, previous2;
static int pos1, pos2;
long max = card_num / 2;
for (long int i = 0; i <= max; i++){
do{
previous1 = pos1;
previous2 = pos2;
if (get_random_number(&pos1, card_num)) return 1;
if (get_random_number(&pos2, card_num)) return 1;
if (pos1 > card_num - 1 || pos2 > card_num - 1) continue;
}while (pos1 == pos2 || (pos1 == previous1 && pos2 == previous2) || (pos1 == previous2 && pos2 == previous1));
swap_int(&cards[pos1], &cards[pos2]);
}
return 0;
}
void print_cards (int cards[], size_t card_num)
{
printf("cards:");
for (long int i = 0; i < card_num; i++) printf(" %d", cards[i]);
printf("\n");
}
int main (void)
{
int *cards = NULL;
int *orig_cards = NULL;
size_t card_num = 80;
if ((cards = malloc(sizeof(int) * card_num)) == NULL || (orig_cards = malloc(sizeof(int) * card_num)) == NULL) err("malloc failed.");
for (long i = 0; i < card_num; i++){
orig_cards[i] = i;
cards[i] = i;
}
if (randomize_cards (cards, card_num)) err("randomize failed.");
printf("original ");
print_cards(orig_cards, card_num);
printf("randomized ");
print_cards(cards, card_num);
if (quick_sort_from_side(cards, card_num, 1)) err("quick sort failed.");
for (long i = 0; i < card_num; i++){
if (cards[i] != orig_cards[i]){
printf("sort failed, cards[%ld]:%d, orig_cards[%ld]:%d.\n", i, cards[i], i, orig_cards[i]);
printf("sorted ");
print_cards(cards, card_num);
return 1;
}
}
printf("sort success.\n");
return 0;
}
*/