-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathselectionsort-c
More file actions
86 lines (63 loc) · 1.63 KB
/
selectionsort-c
File metadata and controls
86 lines (63 loc) · 1.63 KB
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
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
void selection_sort(int a[], int n)
{
int min_index, temp;
for(int i = 0; i < n-1; i++)
{
min_index = i;
for(int j = i+1; j < n; j++)
{
if(a[j] < a[min_index])
{
min_index = j;
}
}
if(min_index != i)
{
temp = a[i];
a[i] = a[min_index];
a[min_index] = temp;
}
}
}
void gen_ran_num(int a[], int n)
{
for(int i = 0; i < n; i++)
{
a[i] = rand() % 10000;
}
}
int main()
{
int a[10000];
clock_t start, end;
double time_taken;
double theoretical_time;
FILE *fp;
srand(time(NULL));
fp = fopen("s_time.txt", "w");
for(int i = 100; i <= 10000; i += 400)
{
gen_ran_num(a, i);
start = clock();
selection_sort(a, i);
end = clock();
time_taken = (double)(end - start) / CLOCKS_PER_SEC;
theoretical_time = (double)(i * i) * 1e-8;
fprintf(fp, "%d %lf %lf\n", i, time_taken, theoretical_time);
}
fclose(fp);
FILE *gP = popen("gnuplot -persistent", "w");
fprintf(gP, "set title 'Selection Sort Time Efficiency'\n");
fprintf(gP, "set xlabel 'Input Size'\n");
fprintf(gP, "set ylabel 'Time (seconds)'\n");
fprintf(gP, "set grid\n");
fprintf(gP, "set term png\n");
fprintf(gP, "set output 's_sort_efficiency_c.png'\n");
fprintf(gP, "plot 's_time.txt' using 1:2 with linespoints title 'Actual Time', \
's_time.txt' using 1:3 with lines lw 2 title 'Theoretical Time'\n");
pclose(gP);
return 0;
}