Skip to content

Commit e7239fc

Browse files
committed
Finish
1 parent 18681fc commit e7239fc

8 files changed

+35
-11
lines changed

Report.md

+28-4
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ void spin_the_bottle_sort(std::vector<int>& nums) {
9999
swap_element(nums[i], nums[s]);
100100
}
101101
}
102-
}
102+
}
103103
}
104104
```
105105

@@ -129,6 +129,7 @@ void shell_sort(std::vector<int>& nums, const std::vector<int>& gaps) {
129129
```
130130
131131
### Annealing sort
132+
132133
Inspired by simulated annealing meta-heuristic, which involves solving a problem by a sequence of choices from the nubmer of r_j neighbors and T_j distance.
133134
134135
Pseudocode:
@@ -185,7 +186,7 @@ For annealing sort, it is tested over the following temparature and repetition s
185186

186187
* Annealing 3 (Temp 3: [1024, 512, 216, 128, 64, 32, 16, 8, 4, 2, 1, 0], Rep 3: [7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 0])
187188

188-
* Annealing 4 (Temp 4: [243, 81, 27, 9, 3, 0], Rep 4: [2, 2, 2, 2, 2, 0])
189+
* Annealing 4 (Temp 4: [1597, 987, 610, 377, 233, 144, 89, 55, 34, 21, 13, 8, 5, 3, 2, 1], Rep 4: [2, 2, 2, 2, 2, 2, 2, 2,2, 2, 2, 2, 2, 2, 2, 0])
189190

190191
* Annealing 5 (Temp 5: [256, 64, 16, 4, 0], Rep 5: [5, 5, 5, 5, 0])
191192

@@ -215,20 +216,43 @@ In this section, the input testing data is permuted using uniformly distributed
215216

216217
4. The following graph shows the running time of annealing sort over all different temparature sequences and repetition sequences above:
217218

218-
![shell sort](./uniform-annealing.png)
219+
![annealing sort](./uniform-annealing.png)
219220

220221
The best two cases of annealing sort are: annealing 2 (orange line) and annealing 4 (red line). The lines of these two different parameters almost overlapped. Annealing 4 is a little bit better than annealing 2.
221222

222-
Annealing 4 has temparature sequence: [243, 81, 27, 9, 3, 0] and reqetition sequence: [2, 2, 2, 2, 2, 0]
223+
Annealing 4 has temparature sequence: [1597, 987, 610, 377, 233, 144, 89, 55, 34, 21, 13, 8, 5, 3, 2, 1] and reqetition sequence: [2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 0]
223224

224225
### Almost-sorted permutations
225226

226227
In this section, the input testing data is almost sorted by independently choosing 2logn pairs, (i, j), where i and j are uniformly-chosen random integers in the range from 0 to n-1, and swap the numbers at positions i and j in the array/vector.
227228

228229
1. The graph below shows the running time of different algorithms on increasing problem size.
229230

231+
![almost sort](./almost-sort.png)
232+
233+
Among all the sorting algorithms tested, insertion sort is the fastest.
234+
230235
2. Regression analysis of the running time experiements
231236

237+
![almost regression](./almost-regression.png)
238+
239+
Regression analysis also shows insertion sort has the smallest slope on log-log scale which means it is the fastest.
240+
232241
3. The following graph shows the running time of shell sort over all different gap sequences:
233242

243+
![shell sort](./almost-shell.png)
244+
245+
The best two cases of shell sort are: shell 3 and shell 5. And shell 5 is even faster than shell 3. Overall, gap sequence: [200, 190, 180, 170, 160, 150] has the best performance.
246+
234247
4. The following graph shows the running time of annealing sort over all different temparature sequences and repetition sequences:
248+
249+
![annealing sort](./almost-annealing.png)
250+
251+
The best two cases of annealing sort are: annealing 2 and annealing 6.
252+
Annealing 2 has a faster run time than annealing 6.
253+
254+
Annealing 2 has temparature sequence: [1000, 800, 600, 400, 200, 0] and reqetition sequence: [2, 2, 2, 2, 2, 0]
255+
256+
## Conclusion
257+
258+
Overall, annealing sort 4 is the fastest when input testing data is in uniformly distributed permutations. And insertion sort is the fastest when input testing data in almost-sorted permutations.

almost-annealing.png

108 KB
Loading

almost-regression.png

94.2 KB
Loading

almost-shell.png

105 KB
Loading

almost-sort.png

90.2 KB
Loading

main.cpp

+5-5
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ vector<int> get_random_shuffled_int_vector(int n)
2626
vector<int> vec = vector<int>(n);
2727
for(int i = 0; i < n; i++)
2828
vec[i] = (i + 1);
29-
// shuffle_vector(vec);
30-
almost_sorted_vector(vec); // Use this line to generate almost sorted array
29+
shuffle_vector(vec);
30+
// almost_sorted_vector(vec); // Use this line to generate almost sorted array
3131
return vec;
3232
}
3333

@@ -40,7 +40,7 @@ timing time_sort(int n, int reps, function<void(vector<int>&)> sort_algorithm)
4040
{
4141
rvec = get_random_shuffled_int_vector(n);
4242
double min_timing = DBL_MAX;
43-
for (int j = 0; j < 1; j++) { // For each input vector, do 3 runs and take the smallest timing.
43+
for (int j = 0; j < 3; j++) { // For each input vector, do 3 runs and take the smallest timing.
4444
clock_t c_start = clock();
4545
sort_algorithm(rvec);
4646
clock_t c_end = clock();
@@ -66,7 +66,7 @@ timing time_shell_sort(int n, int reps, const std::vector<int>& gaps)
6666
{
6767
rvec = get_random_shuffled_int_vector(n);
6868
double min_timing = DBL_MAX;
69-
for (int j = 0; j < 1; j++) { // For each input vector, do 3 runs and take the smallest timing.
69+
for (int j = 0; j < 3; j++) { // For each input vector, do 3 runs and take the smallest timing.
7070
clock_t c_start = clock();
7171
shell_sort(rvec, gaps);
7272
clock_t c_end = clock();
@@ -92,7 +92,7 @@ timing time_annealing_sort(int n, int reps, const std::vector<int>& temps, const
9292
{
9393
rvec = get_random_shuffled_int_vector(n);
9494
double min_timing = DBL_MAX;
95-
for (int j = 0; j < 1; j++) { // For each input vector, do 3 runs and take the smallest timing.
95+
for (int j = 0; j < 3; j++) { // For each input vector, do 3 runs and take the smallest timing.
9696
clock_t c_start = clock();
9797
annealing_sort(rvec, temps, repetitions);
9898
clock_t c_end = clock();

plotting.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,4 @@ def plot_timings_from_file(fname_list):
2525
# plt.savefig('uniform-shell.png')
2626
plt.close()
2727

28-
plot_timings_from_file(["bubble.csv", "insertion.csv", "spin.csv", "shell1.csv", "shell2.csv", "annealing2.csv", "annealing4.csv"])
28+
plot_timings_from_file(["bubble.csv", "insertion.csv", "spin.csv", "shell5.csv", "shell3.csv", "annealing2.csv", "annealing6.csv"])

regression.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ def plot_regression_lines(fname):
3030
m, b = get_regression_line_coefficients(n, seconds)
3131
plt.plot(x, m*x + b, label=f'{fname.rstrip(".csv")}: y ~ {round(m, 2)}x + {round(b, 2)}')
3232

33-
file_list = ["bubble.csv", "insertion.csv", "spin.csv", "shell1.csv", "shell2.csv", "annealing2.csv", "annealing4.csv"]
33+
file_list = ["bubble.csv", "insertion.csv", "spin.csv", "shell5.csv", "shell3.csv", "annealing2.csv", "annealing6.csv"]
3434

3535
# Plot graph
3636
plt.subplot(2, 1, 1)

0 commit comments

Comments
 (0)