-
Notifications
You must be signed in to change notification settings - Fork 0
/
sorting_test.c
236 lines (173 loc) · 5 KB
/
sorting_test.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
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "sorting.h"
#define SORT_NUM 5 // 테스트할 정렬함수의 개수
//#define DB // 정렬한 결과를 출력할지 결정하는 매크로
int* generateRandArray(int size);
void deleteRandArray(int* array);
void printArray(int size, int* arr);
void testExchangesort(int size, int* arr);
void testMergesort(int size, int* arr);
void testQuicksort(int size, int* arr);
void testHeapsort(int size, int* arr);
void testRadixsort(int size, int* arr);
int main(void) {
int test_sizes[] = { /*10, 15, 20, 30*/ 100, 500, 1000, 5000, 10000 }; // 테스트에 사용할 배열의 크기
int test_num = sizeof(test_sizes) / sizeof(int); // 테스트 횟수
int* rand_arr; // 정렬할 배열을 할당받는 변수
int size; // 정렬할 배열의 크기
srand((unsigned int)time(NULL));
for (int i = 0; i < test_num; i++) {
size = test_sizes[i];
rand_arr = generateRandArray(size);
printf("input size: %d ", size);
#ifdef DB
printf("before : "); printArray(size, rand_arr); printf("\n");
#endif
testExchangesort(size, rand_arr);
deleteRandArray(rand_arr);
}
printf("\n");
for (int i = 0; i < test_num; i++) {
size = test_sizes[i];
rand_arr = generateRandArray(size);
printf("input size: %d ", size);
#ifdef DB
printf("before : "); printArray(size, rand_arr); printf("\n");
#endif
testMergesort(size, rand_arr);
deleteRandArray(rand_arr);
}
printf("\n");
for (int i = 0; i < test_num; i++) {
size = test_sizes[i];
rand_arr = generateRandArray(size);
printf("input size: %d ", size);
#ifdef DB
printf("before : "); printArray(size, rand_arr); printf("\n");
#endif
testQuicksort(size, rand_arr);
deleteRandArray(rand_arr);
}
printf("\n");
for (int i = 0; i < test_num; i++) {
size = test_sizes[i];
rand_arr = generateRandArray(size);
printf("input size: %d ", size);
#ifdef DB
printf("before : "); printArray(size, rand_arr); printf("\n");
#endif
testHeapsort(size, rand_arr);
deleteRandArray(rand_arr);
}
printf("\n");
for (int i = 0; i < test_num; i++) {
size = test_sizes[i];
rand_arr = generateRandArray(size);
printf("input size: %d ", size);
#ifdef DB
printf("before : "); printArray(size, rand_arr); printf("\n");
#endif
testRadixsort(size, rand_arr);
deleteRandArray(rand_arr);
}
printf("\n");
}
// 교환정렬 테스트 함수
void testExchangesort(int size, int* arr) {
clock_t start, end;
double enlapsed_time;
start = clock();
exchangesort(size, arr);
end = clock();
enlapsed_time = (double)(end - start);
printf("< exchangesort > enlasped time: %f sec\n", enlapsed_time / CLOCKS_PER_SEC);
#ifdef DB
printf("after : "); printArray(size, arr); printf("\n");
#endif
}
// 합병정렬 테스트 함수
void testMergesort(int size, int* arr) {
clock_t start, end;
double enlapsed_time;
start = clock();
mergesort(size, arr);
end = clock();
enlapsed_time = (double)(end - start);
printf("< mergesort > enlasped time: %f sec\n", enlapsed_time / CLOCKS_PER_SEC);
#ifdef DB
printf("after : "); printArray(size, arr); printf("\n");
#endif
}
// 퀵정렬 테스트 함수
void testQuicksort(int size, int* arr) {
clock_t start, end;
double enlapsed_time;
start = clock();
quicksort(0, size - 1, arr);
end = clock();
enlapsed_time = (double)(end - start);
printf("< quicksort > enlasped time: %f sec\n", enlapsed_time / CLOCKS_PER_SEC);
#ifdef DB
printf("after : "); printArray(size, arr); printf("\n");
#endif
}
// 힙정렬 테스트 함수
void testHeapsort(int size, int* arr) {
clock_t start, end;
double enlapsed_time;
Heap heap;
// 동적배열을 만들어서 힙에 할당
int* data = (int*)malloc(sizeof(int) * (size + 1));
for (int i = 1; i <= size; i++) {
data[i] = arr[i - 1];
}
heap.data = data;
heap.heapsize = size;
start = clock();
heapsort(size, &heap);
end = clock();
enlapsed_time = (double)(end - start);
printf("< heapsort > enlasped time: %f sec\n", enlapsed_time / CLOCKS_PER_SEC);
#ifdef DB
printf("after : ");
for (int i = 1; i <= size; i++) {
printf("%d ", heap.data[i]);
}
printf("\n");
#endif
free(data);
}
// 기수 정렬 테스트 함수
void testRadixsort(int size, int* arr) {
clock_t start, end;
double enlapsed_time;
start = clock();
//
end = clock();
enlapsed_time = (double)(end - start);
printf("< radixsort > enlasped time: %f sec\n", enlapsed_time / CLOCKS_PER_SEC);
#ifdef DB
printf("after : "); printArray(size, arr); printf("\n");
#endif
}
// 난수 생성기를 통해서 배열을 동적으로 생성하는 함수
int* generateRandArray(int size) {
int* array = (int*)malloc(sizeof(int) * size);
for (int i = 0; i < size; i++) {
array[i] = rand() % 100;
}
return array;
}
// 동적 배열을 반환하는 함수
void deleteRandArray(int* array) {
free(array);
}
// 배열의 값을 출력하는 함수
void printArray(int size, int* arr) {
for (int i = 0; i < size; i++) {
printf("%d ", arr[i]);
}
printf("\n");
}