-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAssignment02.java
More file actions
292 lines (266 loc) · 8.73 KB
/
Copy pathAssignment02.java
File metadata and controls
292 lines (266 loc) · 8.73 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
import java.util.*;
import java.io.FileWriter;
import java.io.IOException;
import org.knowm.xchart.*;
import org.knowm.xchart.style.markers.XChartSeriesMarkers;
import org.knowm.xchart.XYSeries.XYSeriesRenderStyle;
import org.knowm.xchart.style.Styler.LegendPosition;
/**
*
* @author Rowan Holop
* @version March 29 2020
*/
class RunningTimeRecorder
{
/**
* Takes in an int, produces a random list of numbers of n size and performs various sorting algorithms.
* @param int n
* @return Double[] of runningTimes: order - insertionSort, selectionSort, mergeSort, bubbleSort
*/
public static Double[] empiricalRunningTime(int n)
{
Integer[] arr = RandomIntegerGenerator.generateRandomNumbers(n);
// copy 1
Integer[] copiedArray1 = new Integer[n];
System.arraycopy(arr, 0, copiedArray1, 0, n);
// copy 2
Integer[] copiedArray2 = new Integer[n];
System.arraycopy(arr, 0, copiedArray2, 0, n);
// copy 3
Integer[] copiedArray3 = new Integer[n];
System.arraycopy(arr, 0, copiedArray3, 0, n);
// copy 4
Integer[] copiedArray4 = new Integer[n];
System.arraycopy(arr, 0, copiedArray4, 0, n);
// array of arrays
Integer[][] copiedArrays = {copiedArray1, copiedArray2, copiedArray3, copiedArray4};
Double[] runTimes = new Double[4]; // holder array of running times to return
int c = 0; // counter
for (Integer[] copiedArray : copiedArrays)
{
long startTime;
long stopTime;
// Following loop allows us to iterate through each array and perform the different sorting algorithms
if (c == 0) {startTime = System.nanoTime(); SortingAlgorithms.insertionSort(copiedArray); stopTime = System.nanoTime();}
else if (c == 1) {startTime = System.nanoTime(); SortingAlgorithms.selectionSort(copiedArray); stopTime = System.nanoTime();}
else if (c == 2) {startTime = System.nanoTime(); SortingAlgorithms.mergeSort(copiedArray); stopTime = System.nanoTime();}
else {startTime = System.nanoTime(); SortingAlgorithms.bubbleSort(copiedArray); stopTime = System.nanoTime();}
c++;
Double runningTime = (double)(stopTime - startTime)/1000000000;
runTimes[c-1] = runningTime;
}
return runTimes;
}
}
/**
* This wrapper class records results into a .csv file.
*/
class FileRecord {
FileWriter csv;
FileRecord(String filename, String[] titles)
/**
* @param String filename
* @param String[] titles (for columns of chart)
*/
{
try
{
this.csv = new FileWriter(filename);
write(String.join(",", titles));
write("\n");
}
catch (IOException e)
{
System.out.println("Unable to create file.");
e.printStackTrace();
}
}
public void write(String text)
/**
* @param String text - text to write to file
*/
{
try
{
csv.append(text);
}
catch (IOException e)
{
System.out.println("Unable to write to file.");
e.printStackTrace();
}
}
public void writeRow(Double x, Double[] dataList)
/**
* Shortcut method for writing a whole row at once.
* @param Double x - row title
* @param Double[] dataList - data to be stored in said row
*/
{
String[] data = ConvertArray.doubletoString(dataList);
List<String> row = Arrays.asList(data);
write(Double.toString(x) + ",");
write(String.join(",", row));
write("\n");
}
public void end()
/**
* Closes the .csv file
*/
{
try {
csv.flush();
csv.close();
System.out.println("Written to file");
}
catch (IOException e) {
System.out.println("An error occurred when closing file.");
e.printStackTrace();
}
}
}
/**
* Simple class to convert certain types of arrays into other arrays.
*/
class ConvertArray
{
public static String[] doubletoString(Double[] arr)
/**
* @param Double[]
* @return String[]
*/
{
String[] temp = new String[arr.length];
int i = 0;
for (Double d : arr) {
String t = d.toString();
temp[i] = t;
i++;
}
return temp;
}
public static double[] bigDtoLittleD(Double[] arr)
/**
* @param Double[]
* @return double[]
*/
{
double[] temp = new double[arr.length];
int i = 0;
for (Double d : arr) {
double t = d;
temp[i] = t;
i++;
}
return temp;
}
}
class RunningTimeChart
/**
* Wraper class for creating the runtimes chart.
*/
{
XYChart chart;
double[] xValues;
double[] yInsertionSort;
double[] ySelectionSort;
double[] yMergeSort;
double[] yBubbleSort;
RunningTimeChart(String title, String xAxis, String yAxis, double[] xValues)
/**
* Constructor
* @param String title
* @param String xAxis
* @param String yAxis
* @param double[] xValues
*/
{
// Build the chart
chart = new XYChartBuilder().width(1400).height(1000).title(title).xAxisTitle(xAxis).yAxisTitle(yAxis).build();
// Some stylistic choices
chart.getStyler().setYAxisMin((double)0.00000);
chart.getStyler().setYAxisMax((double).18000);
chart.getStyler().setXAxisMin((double)5);
chart.getStyler().setXAxisMax((double)10000.0);
chart.getStyler().setDefaultSeriesRenderStyle(XYSeriesRenderStyle.Line);
chart.getStyler().setXAxisLogarithmic(true);
chart.getStyler().setLegendPosition(LegendPosition.InsideNW);
// Initialize member variables
this.xValues = xValues;
yInsertionSort = new double[xValues.length];
ySelectionSort = new double[xValues.length];
yMergeSort = new double[xValues.length];
yBubbleSort = new double[xValues.length];
}
public void update(int i, Double[] values)
/**
* Updates the internal double[]'s for each sorting method.
* @param int i - index to insert value
* @param Double[] values - runtime values to distribute amongst the internal sorting method's arrays.
*/
{
double[] v = ConvertArray.bigDtoLittleD(values);
yInsertionSort[i] = v[0];
ySelectionSort[i] = v[1];
yMergeSort[i] = v[2];
yBubbleSort[i] = v[3];
}
public void makeChart()
/**
* Call this method after all updates are complete to create the chart's line values.
*/
{
chart.addSeries("Insertion Sort", xValues, yInsertionSort);
chart.addSeries("Selection Sort", xValues, ySelectionSort);
chart.addSeries("Merge Sort", xValues, yMergeSort);
chart.addSeries("Bubble Sort", xValues, yBubbleSort);
}
public void saveChart(String filename)
/**
* Saves the chart to a new file name - saves as PNG
* @param String filename ex: "file.PNG"
*/
{
try {
BitmapEncoder.saveBitmapWithDPI(chart, filename, BitmapEncoder.BitmapFormat.PNG, 300);
}
catch (IOException e) {
System.out.println("An error occurred.");
e.printStackTrace();
}
}
public void showChart()
/**
* Displays the chart.
*/
{
new SwingWrapper(chart).displayChart();
}
}
public class Assignment02
{
public static void main(String[] args){
// Numbers for test data
double[] numbers = {5, 10, 50, 100, 200, 500, 1000, 2000, 5000, 10000};
// Column titles
String[] titles = {"Number of Elements", "Insertion Sort Time", "Selection Sort Time",
"Merge Sort Time", "Bubble Sort Time"};
String file = "assignment02RunningHolop.csv"; // File title
FileRecord f = new FileRecord(file, titles); // Create new FileRecord Object to write to
// Create chart
RunningTimeChart c = new RunningTimeChart("Running Times", "Number of Elements", "System Time (ms)", numbers);
// Iterate through sample numbers
for (int i = 0; i < numbers.length; i++) {
// Run method to get array of runtimes
Double[] doubleRunTimes = RunningTimeRecorder.empiricalRunningTime((int)numbers[i]);
// Record runtimes in .csv file
f.writeRow(numbers[i], doubleRunTimes);
// Update chart with runtimes
c.update(i, doubleRunTimes);
}
f.end(); // Close the file
c.makeChart(); // Create the chart
c.showChart(); // Display the chart
c.saveChart("graph.PNG"); // Save the chart
}
}