Skip to content

Commit 2b9e5dc

Browse files
committed
Implementing all algorithms, testing function
1 parent c60819f commit 2b9e5dc

14 files changed

+501
-204
lines changed

annealing-sort.cpp

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#include "project1.h"
2+
#include "swap.cpp"
3+
#include "random_generation_sample.cpp"
4+
5+
using namespace std;
6+
7+
void annealing_sort(std::vector<int>& nums, const std::vector<int>& temps, const std::vector<int>& reps) {
8+
int n = nums.size();
9+
int t = temps.size();
10+
mt19937 mt = get_mersenne_twister_generator_with_current_time_seed();
11+
for (int j = 0; j < t-1; ++j) {
12+
for (int i = 0; i < n-1; ++i) {
13+
for (int k = 1; k <= reps[j]; ++k) {
14+
uniform_int_distribution<int> ui = get_uniform_int_generator(i+1, min(n-1, i+temps[j]));
15+
int s = ui(mt);
16+
if (nums[i] > nums[s]) {
17+
swap_element(nums[i], nums[s]);
18+
}
19+
}
20+
}
21+
for (int i = n-1; i >= 1; --i) {
22+
for (int k = 1; k <= reps[j]; ++k) {
23+
uniform_int_distribution<int> ui = get_uniform_int_generator(max(0, i-temps[j]), i-1);
24+
int s = ui(mt);
25+
if (nums[s] > nums[i]) {
26+
swap_element(nums[i], nums[s]);
27+
}
28+
}
29+
}
30+
}
31+
}

bubble_sort.cpp

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#include "project1.h"
2+
#include "swap.cpp"
3+
4+
using namespace std;
5+
6+
void bubble_sort(std::vector<int>& nums) {
7+
int size = nums.size();
8+
for (int i = 0; i < size; ++i) {
9+
for (int j = 1; j < size - i; ++j) {
10+
if (nums[j] < nums[j - 1]) {
11+
swap_element(nums[j-1], nums[j]);
12+
}
13+
}
14+
}
15+
}

insertion_sort.cpp

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#include "project1.h"
2+
3+
using namespace std;
4+
5+
void insertion_sort(std::vector<int>& nums) {
6+
int size = nums.size();
7+
for (int i = 1; i < size; ++i) {
8+
int tmp = nums[i];
9+
int k = i;
10+
while (k > 0 && tmp < nums[k - 1]) {
11+
nums[k] = nums[k - 1];
12+
--k;
13+
}
14+
nums[k] = tmp;
15+
}
16+
}

main.cpp

+189
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,189 @@
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+
}

plotting.py

+43-37
Original file line numberDiff line numberDiff line change
@@ -4,53 +4,59 @@
44
import math
55
import scipy.interpolate as ip
66

7-
def plot_polynomials_and_their_negations():
8-
x = np.linspace(0, 2, 100)
7+
# def plot_polynomials_and_their_negations():
8+
# x = np.linspace(0, 2, 100)
99

10-
plt.plot(x, x, label='linear')
11-
plt.plot(x, x**2, label='quadratic')
12-
plt.plot(x, x**3, label='cubic')
10+
# plt.plot(x, x, label='linear')
11+
# plt.plot(x, x**2, label='quadratic')
12+
# plt.plot(x, x**3, label='cubic')
1313

14-
plt.xlabel('x label')
15-
plt.ylabel('y label')
14+
# plt.xlabel('x label')
15+
# plt.ylabel('y label')
1616

17-
plt.title("Simple Plot")
18-
plt.legend()
19-
plt.show()
20-
#plt.savefig('curves.png')
17+
# plt.title("Simple Plot")
18+
# plt.legend()
19+
# plt.show()
20+
# #plt.savefig('curves.png')
2121

22-
plt.clf()
23-
x = np.linspace(0, 2, 100)
22+
# plt.clf() # Clear the current figure
23+
# x = np.linspace(0, 2, 100)
2424

25-
plt.plot(x, -x, label='linear')
26-
plt.plot(x, -x**2, label='quadratic')
27-
plt.plot(x, -x**3, label='cubic')
25+
# plt.plot(x, -x, label='linear')
26+
# plt.plot(x, -x**2, label='quadratic')
27+
# plt.plot(x, -x**3, label='cubic')
2828

29-
plt.xlabel('x label')
30-
plt.ylabel('y label')
29+
# plt.xlabel('x label')
30+
# plt.ylabel('y label')
31+
32+
# plt.title("Simple Plot")
33+
# plt.legend()
34+
# plt.show()
35+
# #plt.savefig('curves_neg.png')
36+
# plt.close()
37+
38+
def plot_timings_from_file(fname_list):
39+
for fname in fname_list:
40+
timings = pd.read_csv(fname, sep=',')
41+
42+
n = timings['n'].values
43+
seconds = timings['seconds'].values
44+
45+
x = n
46+
y = seconds
47+
plt.loglog(x=x, y=y, basex=2, basey=2)
48+
# s = ip.UnivariateSpline(x, y)
49+
# xs = np.linspace(min(x), max(x), 1000)
50+
# ys = s(xs)
51+
plt.plot(x, y, label=fname)
3152

32-
plt.title("Simple Plot")
33-
plt.legend()
34-
plt.show()
35-
#plt.savefig('curves_neg.png')
36-
plt.close()
3753

38-
def plot_timings_from_file(fname):
39-
timings = pd.read_csv(fname, sep=',')
40-
n = timings['n'].values
41-
seconds = timings['seconds'].values
42-
x = n
43-
y = seconds
44-
plt.loglog(x=x, y=y, basex=2, basey=2, 's')
45-
s = ip.UnivariateSpline(x, y, s = 0)
46-
xs = np.linspace(min(x), max(x), 1000)
47-
ys = s(xs)
48-
plt.plot(xs, ys)
4954
plt.xlabel('# of elements')
5055
plt.ylabel('time in seconds')
51-
plt.title('Performance of introsort')
56+
plt.title('Performance of sort')
57+
plt.legend()
5258
plt.show()
59+
#plt.savefig('sort.png')
5360
plt.close()
5461

55-
plot_timings_from_file('timings.csv')
56-
#plot_polynomials_and_their_negations()
62+
plot_timings_from_file(["bubble.csv", "insertion.csv", "shell1.csv", "shell2.csv"]) # TODO: annealing1.csv, annealing2.csv

project1.h

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#include <vector>
2+
3+
//REQUIREMENTS:
4+
//each sort should be implemented in its own file of the same name e.g. bubble_sort.cpp
5+
//each file should #include this header file
6+
//no file should use anything outside of the C++ standard library
7+
//functions should be tested using g++ in a linux environment
8+
//each function should modify the input vector so that it is sorted upon completion
9+
void bubble_sort(std::vector<int>& nums);
10+
void insertion_sort(std::vector<int>& nums);
11+
void spin_the_bottle_sort(std::vector<int>& nums);
12+
void shell_sort(std::vector<int>& nums, const std::vector<int>& gaps);
13+
void annealing_sort(std::vector<int>& nums, const std::vector<int>& temps, const std::vector<int>& reps);

random_generation_sample

40.7 KB
Binary file not shown.

0 commit comments

Comments
 (0)