1
+ #include < iostream>
2
+ #include < fstream>
3
+ #include < vector>
4
+ #include < algorithm>
5
+ #include < ctime>
6
+ #include < float.h>
7
+ #include " bubble_sort.cpp"
8
+ #include " insertion_sort.cpp"
9
+ #include " spin-the-bottle-sort.cpp"
10
+ #include " shell-sort.cpp"
11
+ #include " annealing-sort.cpp"
12
+ #include " random_generation_sample.cpp"
13
+
14
+ using namespace std ;
15
+
16
+ struct timing
17
+ {
18
+ int n;
19
+ double seconds;
20
+ };
21
+
22
+ // get a vector of ints in 1, 2, ... , n shuffled randomly
23
+ vector<int > get_random_shuffled_int_vector (int n)
24
+ {
25
+ vector<int > vec = vector<int >(n);
26
+ for (int i = 0 ; i < n; i++)
27
+ vec[i] = (i + 1 );
28
+ shuffle_vector (vec);
29
+ // almost_sorted_vector(vec); Use this line to generate almost sorted array
30
+ return vec;
31
+ }
32
+
33
+ // run and time sort for vector with n elements. return vector of timings with sizes and seconds
34
+ timing time_sort (int n, int reps, function<void (vector<int >&)> sort_algorithm)
35
+ {
36
+ double total_time = 0.0 ;
37
+ vector<int > rvec;
38
+ for (int i = 0 ; i < reps; i++) // For each input size, do "reps" times runs.
39
+ {
40
+ rvec = get_random_shuffled_int_vector (n);
41
+ double min_timing = DBL_MAX;
42
+ for (int j = 0 ; j < 1 ; j++) { // For each input vector, do 3 runs and take the smallest timing.
43
+ clock_t c_start = clock ();
44
+ sort_algorithm (rvec);
45
+ clock_t c_end = clock ();
46
+
47
+ double curr_timing = (float )(c_end - c_start) / CLOCKS_PER_SEC;
48
+ if (curr_timing < min_timing) {
49
+ min_timing = curr_timing;
50
+ }
51
+ }
52
+ total_time += min_timing;
53
+ }
54
+ timing t;
55
+ t.n = n;
56
+ t.seconds = (float )total_time/reps; // Average minimum running time of each size
57
+ return t;
58
+ }
59
+
60
+ timing time_shell_sort (int n, int reps, const std::vector<int >& gaps)
61
+ {
62
+ double total_time = 0.0 ;
63
+ vector<int > rvec;
64
+ for (int i = 0 ; i < reps; i++)
65
+ {
66
+ rvec = get_random_shuffled_int_vector (n);
67
+ double min_timing = DBL_MAX;
68
+ for (int j = 0 ; j < 1 ; j++) { // For each input vector, do 3 runs and take the smallest timing.
69
+ clock_t c_start = clock ();
70
+ shell_sort (rvec, gaps);
71
+ clock_t c_end = clock ();
72
+
73
+ double curr_timing = (float )(c_end - c_start) / CLOCKS_PER_SEC;
74
+ if (curr_timing < min_timing) {
75
+ min_timing = curr_timing;
76
+ }
77
+ }
78
+ total_time += min_timing;
79
+ }
80
+ timing t;
81
+ t.n = n;
82
+ t.seconds = (float )total_time/reps;
83
+ return t;
84
+ }
85
+
86
+ // timing time_annealing_sort(int n, int reps, const std::vector<int>& temps, const std::vector<int>& repetitions)
87
+ // {
88
+ // double total_time = 0.0;
89
+ // vector<int> rvec;
90
+ // for(int i = 0; i < reps; i++)
91
+ // {
92
+ // rvec = get_random_shuffled_int_vector(n);
93
+ // double min_timing = DBL_MAX;
94
+ // for (int j = 0; j < 3; j++) { // For each input vector, do 3 runs and take the smallest timing.
95
+ // clock_t c_start = clock();
96
+ // annealing_sort(rvec, temps, repetitions);
97
+ // clock_t c_end = clock();
98
+
99
+ // double curr_timing = (float)(c_end - c_start) / CLOCKS_PER_SEC;
100
+ // if (curr_timing < min_timing) {
101
+ // min_timing = curr_timing;
102
+ // }
103
+ // }
104
+ // total_time += min_timing;
105
+ // }
106
+ // timing t;
107
+ // t.n = n;
108
+ // t.seconds = (float)total_time/reps;
109
+ // return t;
110
+ // }
111
+
112
+ // create/truncate a file with chosen filename. insert csv header "funcname,n,seconds"
113
+ void create_empty_timings_file (string filename)
114
+ {
115
+ ofstream f;
116
+ f.open (filename, ios::trunc );
117
+ f << " funcname,n,seconds\n " ;
118
+ f.close ();
119
+ }
120
+
121
+ // append timings data in csv format to a file with no header. (header should be created first)
122
+ void add_timings_to_file (string funcname, timing t, string filename)
123
+ {
124
+ ofstream f;
125
+ f.open (filename, ios::app);
126
+ f << funcname << " ," << t.n << " ," << t.seconds << " \n " ;
127
+ f.close ();
128
+ }
129
+
130
+ int main ()
131
+ {
132
+ timing t;
133
+ const vector<int >& gaps1{1501 , 701 , 301 , 132 , 57 , 23 , 10 , 4 , 1 };
134
+ const vector<int >& gaps2{1800 , 900 , 281 , 77 , 23 , 8 , 1 };
135
+ // const vector<int>& temps1;
136
+ // const vector<int>& temps2;
137
+ // const vector<int>& repetitions1;
138
+ // const vector<int>& repetitions2;
139
+
140
+ create_empty_timings_file (" shell1.csv" );
141
+ for (int n = 10 ; n <= 100000 ; n *= 10 )
142
+ {
143
+ t = time_shell_sort (n, 3 , gaps1);
144
+ add_timings_to_file (" shell-sort(gaps1)" , t, " shell1.csv" );
145
+ }
146
+
147
+ create_empty_timings_file (" shell2.csv" );
148
+ for (int n = 10 ; n <= 100000 ; n *= 10 )
149
+ {
150
+ t = time_shell_sort (n, 3 , gaps2);
151
+ add_timings_to_file (" shell-sort(gaps2)" , t, " shell2.csv" );
152
+ }
153
+
154
+ create_empty_timings_file (" insertion.csv" );
155
+ for (int n = 10 ; n <= 100000 ; n *= 10 )
156
+ {
157
+ t = time_sort (n, 3 , insertion_sort);
158
+ add_timings_to_file (" insertion-sort" , t, " insertion.csv" );
159
+ }
160
+
161
+ create_empty_timings_file (" bubble.csv" );
162
+ for (int n = 10 ; n <= 100000 ; n *= 10 )
163
+ {
164
+ t = time_sort (n, 3 , bubble_sort);
165
+ add_timings_to_file (" bubble-sort" , t, " bubble.csv" );
166
+ }
167
+
168
+ // create_empty_timings_file("spin.csv");
169
+ // for(int n = 10; n <= 10000; n *= 10)
170
+ // {
171
+ // t = time_sort(n, 3, spin_the_bottle_sort);
172
+ // add_timings_to_file("spin-the-bottle-sort", t, "spin.csv");
173
+ // }
174
+
175
+ // TODO:
176
+ // create_empty_timings_file("annealing1.csv");
177
+ // for(int n = 10; n <= 10000; n *= 10)
178
+ // {
179
+ // t = time_annealing_sort(n, 3, temps1, repetitions1);
180
+ // add_timings_to_file("annealing-sort(temps1, reps1", t, "annealing1.csv");
181
+ // }
182
+
183
+ // create_empty_timings_file("annealing2.csv");
184
+ // for(int n = 10; n <= 10000; n *= 10)
185
+ // {
186
+ // t = time_annealing_sort(n, 3, temps2, repetitions2);
187
+ // add_timings_to_file("annealing-sort(temps2, reps2", t, "annealing2.csv");
188
+ // }
189
+ }
0 commit comments