-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtp-MAN.c
410 lines (368 loc) · 9.85 KB
/
tp-MAN.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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
/**
* \file tp-MAN.c
* \brief Programme de tests.
* \author Julien TEULLE, Kurt SAVIO
* \version 1
* \date 14/09/16
*
* Programme de test pour divers tri
*
*/
#include "man-util.h"
#include "string.h"
#include "stdlib.h"
#include "time.h"
#include "stdio.h"
#include "Tri.h"
#include "list.h"
#include "QuickSort.h"
#include "SequentialInsertionSort.h"
#include "DichotomousInsertionSort.h"
#include "mergeSort.h"
#include "SelectionSwapSort.h"
#include "bubbleSort.h"
#include "StackSort.h"
#include "BinarySearchTreeSort.h"
#include "SequentialInsertionListSort.h"
/***********CONSTANTS***********/
#define MAX 1000000
#define numberOfSizesToUse 15
#define numberOfTestsToDo 20
#define numberOfTitles 23
#define sizeOfCheckTab 10
/***********TYPES***********/
typedef int TABLEAU[MAX];
/***********GLOBAL VARIABLES***********/
clock_t begin, end;
int sizesUseForTests[numberOfSizesToUse];
/**
* \fn fillWithRandomNumbers (int tab[], int size)
* \brief Fill a table with numbers randomly generated.
* \param tab The table which is filled.
* \param size The size of tab.
*/
void fillWithRandomNumbers (int tab[], int size)
{
int i = 0;
for (; i < size; i++)
{
tab[i] = (int) rand() % 101;
}
}
void fillListWithRandomNumbers (List *list, int size)
{
int i = 0;
for (; i < size; ++i)
{
list->push(list, (int) rand() % 101);
}
}
/**
* \fn displayTab (int tab[], int size)
* \author Julien TEULLE
* \brief Show the content of a table with one element per line.
* \param tab The table which is display.
* \param size The size of tab.
*/
void displayTab (int tab[], int size)
{
int i = 0;
printf("=====AFFICHAGE=====\n");
printf("Indice | Valeur\n");
for (; i < size; i++)
{
printf("%d | %d\n", i, tab[i]);
}
}
/**
* \fn CreateCSV ()
* \author Kurt SAVIO
* \brief Create a csv file named "ResultatTestTri.csv" and include
* titles at the begining of the file for better undrestanding
*/
void CreateCSV ()
{
FILE * ResultTest = fopen("ResultatTestTri.csv", "w");
if (ResultTest == NULL)
{
printf("Impossible d'ouvrir le fichier\n");
exit(1);
}
char* Titre[numberOfTitles];
Titre[0] = "Nom du tri";
Titre[1] = "Taille Tableau";
Titre[2] = "Temps Test 1";
Titre[3] = "Temps Test 2";
Titre[4] = "Temps Test 3";
Titre[5] = "Temps Test 4";
Titre[6] = "Temps Test 5";
Titre[7] = "Temps Test 6";
Titre[8] = "Temps Test 7";
Titre[9] = "Temps Test 8";
Titre[10] = "Temps Test 9";
Titre[11] = "Temps Test 10";
Titre[12] = "Temps Test 11";
Titre[13] = "Temps Test 12";
Titre[14] = "Temps Test 13";
Titre[15] = "Temps Test 14";
Titre[16] = "Temps Test 15";
Titre[17] = "Temps Test 16";
Titre[18] = "Temps Test 17";
Titre[19] = "Temps Test 18";
Titre[20] = "Temps Test 19";
Titre[21] = "Temps Test 20";
Titre[22] = "Moyenne";
int i;
for (i = 0; i < numberOfTitles - 1; i++) {
fprintf(ResultTest, "%s;", Titre[i]);
}
fprintf(ResultTest, "%s\n", Titre[22]);
fclose(ResultTest);
}
/**
* \fn OpenCSV()
* \author Kurt SAVIO
* \brief Open the csv file called "ResultatTestTri.csv" in append mode
* in order to add data after titles
* \return the opened file
*/
FILE* OpenCSV ()
{
FILE * ResultTest = fopen("ResultatTestTri.csv", "a");
if (ResultTest == NULL)
{
printf("Impossible d'ouvrir le fichier\n");
exit(1);
}
return ResultTest;
}
/**
* \fn CloseCSV()
* \author Kurt SAVIO
* \brief close the csv file given in param (that should be "ResultatTestTri.csv")
* \param ResultTest : The file to close
*/
void CloseCSV (FILE * ResultTest)
{
fclose(ResultTest);
}
/**
* \fn getTimeElapsedInMilliseconds()
* \author Julien TEULLE
* \brief Calculate the time elapsed between begin and end in
* milliseconds.
* \return The value of this time
*/
double getTimeElapsedInMilliseconds()
{
return (((double)(end - begin)) / CLOCKS_PER_SEC) * 1000.000;
}
/**
* \fn displayTimeElapsedInMilliseconds()
* \author Julien TEULLE
* \brief Show the time elapsed between begin and end.
*/
void displayTimeElapsedInMilliseconds()
{
printf("Execution time : %fms\n", getTimeElapsedInMilliseconds());
}
/**
* \fn CalcAverage (double Time[])
* \author Kurt SAVIO
* \brief Calculate the average time of a pool of sort
* \param Time[] : An array containing the 20 time (max) of a sort
* \return The average of the 20 time.
*/
double CalcAverage (double Time[], int Done)
{
double Average = 0;
int i;
for (i = 0; i < Done; ++i)
Average += Time[i];
Average /= Done;
return Average;
}
/**
* \fn CalcMinute (double Time)
* \author Kurt SAVIO
* \brief Calculate how many minute there are in "Time" milliseconds
* \param Time : a time in milliseconds
* \return minutes in "Time" ms.
*/
int CalcMinute (double Time)
{
return Time / 60000;
}
/**
* \fn CalcSeconde (double Time)
* \author Kurt SAVIO
* \brief Calculate how many second there are in "Time" milliseconds
* \param Time : a time in milliseconds
* \return seconds in "Time" ms.
*/
int CalcSeconde (double Time)
{
return Time / 1000;
}
/**
* \fn CalcTotalTime (double Time, int Result[])
* \author Kurt SAVIO
* \brief Convert a time in milliseconds to minute, seconds and milliseconds
* \param Time : a time in milliseconds
* \param Result[] : an array in which one minute, second and milliseconds will be stocked
*/
void CalcTotalTime (double Time, int Result[])
{
int Minute = CalcMinute(Time);
Time -= Minute * 60000;
int Seconde = CalcSeconde(Time);
Time -= Seconde * 1000;
Result[0] = Minute;
Result[1] = Seconde;
Result[2] = Time;
}
/**
* \fn ResetTime (double Time[])
* \author Kurt SAVIO
* \brief Reset the array of the 20 time
* \param Time[] : An array containing the 20 time (max) of a sort
*/
void ResetTime(double Time[])
{
int i;
for (i = 0; i < 20; ++i)
Time[i] = 0;
}
/**
* \fn IsSort (int tab[], int size)
* \author Kurt SAVIO
* \brief Check if a table is sort. If not, it shows where and the said values
* \param tab[] : a table contianing random numbers
* \param size : size of the table
*/
void IsSort (int tab[], int size)
{
int i = 0;
for ( ; i < size - 1; ++i)
if (tab[i] > tab[i + 1])
printf("Tableau Non Trié en %d ! \nValeur : %d > %d \n", i, tab[i], tab[i + 1]);
}
/**
* \fn DoSort ()
* \author Kurt SAVIO
* \brief Create, fill and sort a table for each sort and size, 20 time
* each. Also calculate the average time, look out for the total time.
* Then it writes all the result in the csv file.
*/
void DoSort (TRI * t, int useList)
{
int i, j, size;
double TotalTime = 0;
double Time[20];
FILE * Result = OpenCSV();
List *list = new_List();
for (j = 0; j < numberOfSizesToUse; ++j)
{
size = sizesUseForTests[j];
printf ("Tri par %s - %d\n", t->name, size);
int tab[size];
fprintf(Result, "Tri par %s;%d;",t->name, size);
for (i = 0; i < numberOfTestsToDo; ++i)
{
if (useList) {
fillListWithRandomNumbers(list, size);
begin = clock();
t->sortList(list);
}
else {
fillWithRandomNumbers(tab, size);
begin = clock();
t->sort(tab, size);
}
end = clock();
IsSort(tab, size);
Time[i] = getTimeElapsedInMilliseconds();
fprintf(Result, "%f;", Time[i]);
TotalTime += Time[i];
if (TotalTime > 300000) // 5min = 300000ms
break;
if (useList) {
list->free(list);
}
}
int Res[3];
CalcTotalTime(TotalTime, Res);
int echec = 0;
if (i != 20)
{
printf ("Temps limite dépassé : %02d:%02d.%03d ms\n", Res[0], Res[1], Res[2]);
printf ("Uniquement %d tests fait\n\n", i + 1);
echec = 1;
}
else
printf("20 test fait : %02d:%02d.%03d ms\n\n", Res[0], Res[1], Res[2]);
int n = i + 1;
while (n < 20)
{
fprintf(Result, "NULL;");
n++;
}
fprintf(Result, "%f\n", CalcAverage(Time, i));
TotalTime = 0;
ResetTime(Time);
if (echec) break;
}
CloseCSV(Result);
}
/**
* \fn initSizesUseForTests ()
* \author Julien TEULLE
* \brief Initialize the values of sizesUseForTests for the tests.
*/
void initSizesUseForTests ()
{
sizesUseForTests[0] = 100;
sizesUseForTests[1] = 500;
sizesUseForTests[2] = 5000;
sizesUseForTests[3] = 10000;
sizesUseForTests[4] = 50000;
sizesUseForTests[5] = 100000;
int i;
for (i = 6; i < numberOfSizesToUse; i++)
sizesUseForTests[i] = sizesUseForTests[i-1] + 100000;
/*for (i = 0; i < numberOfSizesToUse; i++)
sizesUseForTests[i] /= 100;*/
}
void check_sort(TRI * tri)
{
int tab[sizeOfCheckTab];
fillWithRandomNumbers(tab, sizeOfCheckTab);
displayTab(tab, sizeOfCheckTab);
tri->sort(tab, sizeOfCheckTab);
displayTab(tab, sizeOfCheckTab);
}
int main ()
{
srand(time(NULL));
CreateCSV();
initSizesUseForTests();
TRI Sequential = SequentialInsertionSort_Create();
TRI Dichotomous = DichotomousSort_Create();
TRI SelectionSwap = SelectionSwapSort_Create();
TRI Bubbles = BubbleSort_Create();
TRI Merge = MergeSort_Create();
TRI Quick = QuickSort_Create();
TRI Stack = StackSort_Create();
TRI ABR = Binary_search_tree_sort_Create();
TRI SequentialList = SequentialInsertionSort_Create();
DoSort(&Sequential, 0);
DoSort(&Dichotomous, 0);
DoSort(&SelectionSwap, 0);
DoSort(&Bubbles, 0);
DoSort(&Merge, 0);
DoSort(&Quick, 0);
DoSort(&Stack, 0);
DoSort(&ABR, 0);
DoSort(&SequentialList, 1);
return 0;
}