-
Notifications
You must be signed in to change notification settings - Fork 10
/
select.c
100 lines (87 loc) · 2.47 KB
/
select.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
#include <stdio.h>
#include <time.h>
#include <string.h>
#include <assert.h>
#include <stdarg.h>
#include <math.h>
int compare_chars(const void *a, const void *b)
{
//printf("%c %c\n", *(char *)a, *(char *)b);
//return strncmp((char*)a, (char*)b, 1);
return (*(char *)a - *(char *)b);
}
void selection_sort(char *astring) {
if (!*astring) return;
char *minc = astring;
for (const char *p = astring+1; *p; p++) {
if (*p < *minc)
minc = p;
}
if (minc != astring) {
char temp = *astring;
*astring = *minc;
*minc = temp;
}
selection_sort(astring+1);
}
void qsort( void *ptr, size_t count, size_t size,
int (*comp)(const void *, const void *) );
/*
int main() {
void (*sortfunction[2])(const char* fmt, ...) = {
selection_sort,
qsort
};
char buf[100] = "alsdkfjaslkdjf";
sortfunction[0](buf);
printf("selection_sort: %s\n", buf);
strcpy(buf, "asodfiaslkdjflkjqweroiuqwer");
sortfunction[1](buf, strlen(buf), sizeof(char), compare_chars);
printf("qsort: %s\n", buf);
}
*/
int main(int argc, char *argv[]) {
char buf[1000], teststring[1000];
int execs = 1000000;
if (argc == 1)
strcpy(buf, "samplesamplestringforsorting");
else
strcpy(buf, argv[1]), strncat(buf, buf, 20);
strcpy(teststring, buf);
printf("Len of test string %ld\n", strlen(teststring));
clock_t begin = clock();
// here, do your time-consuming job
int count = execs;
while(count--> 0) {
strcpy(buf, teststring);
qsort(buf, strlen(buf), sizeof(char), compare_chars);
}
clock_t end = clock();
double time_gold = (double)(end - begin) / CLOCKS_PER_SEC;
printf("time spent by qsort %.4f seconds\n", time_gold);
char gold[1000];
strcpy(gold, buf);
// Get timing information for custom sort function
count = execs;
begin = clock();
while(count--> 0) {
strcpy(buf, teststring);
selection_sort(buf);
}
end = clock();
// sanity check whether your sort is valid
printf("Sorted string %s\n", buf);
assert (strcmp(gold, buf)==0);
double time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
printf("time spent by selectionsort %.4f seconds\n", time_spent);
// Step 3 - analysis of the comparison
double diff = fabs(time_spent - time_gold);
printf("selectionsort is %.2f%c %s!\n",
(diff/time_spent)*100,
'%',
time_spent < time_gold? "faster" : "slower" );
double timediff = difftime(end, begin) /CLOCKS_PER_SEC;
;
printf("%f\n", timediff);
return 0;
}