-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
182 lines (144 loc) · 4.78 KB
/
Copy pathMain.java
File metadata and controls
182 lines (144 loc) · 4.78 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
import java.util.*;
import java.io.IOException;
import java.util.Arrays;
import java.util.concurrent.TimeUnit;
import java.io.StringWriter;
import java.io.PrintWriter;
import java.io.BufferedWriter;
import java.io.FileWriter;
public class Main {
static boolean SHUFFLENUMS = true;
public static void main(String[] args) throws IOException {
boolean testRun = false;
if (testRun) { performTestRun(); return; }
long start = System.nanoTime(); // total test timer
int sampleRate = 1000;
int runCount = 40;
int nMax = 100000;
String filePath = "./data.csv";
// store results in the form of:
// key: number of items sorted (integer), value: time (integer)
// time taken to sort is an average from runCount runs
TreeMap <Integer, Long> myMap = new TreeMap<Integer, Long>();
for (int n = sampleRate; n <= nMax; n += sampleRate) {
// generate random numbers, sort them, do this runCount times
// store the individual sort times
long[] times = generateAndSortNumbers(n, runCount);
// average the times
long avg = getAverage(times);
// get the min time
long min = getMin(times);
// get the min time
long max = getMax(times);
// n = the number of items sorted
// avg = the average sorting time for the given n
myMap.put(n, avg);
}
writeStringToFile(convertMapToString(myMap), filePath);
long end = System.nanoTime(); // total test timer
long elapsed = end-start;
System.out.println("Total elapsed time: " + elapsed / 1000000000 + " seconds");
}
public static long getMax(long[] times) {
long max = times[0];
for (int i = 0; i < times.length; i++) {
if (times[i] > max) {
max = times[i];
}
}
return max;
}
public static long getMin(long[] times) {
long min = times[0];
for (int i = 0; i < times.length; i++) {
if (times[i] < min) {
min = times[i];
}
}
return min;
}
//
// Test run
//
public static void performTestRun() {
// Integer[] nums = {2, 5, 0, 16, 42, 8, 3, 12, 7, 12};
// Integer[] nums = {846096478, 852999422, 736753295, 607471164, 103317737, 1051199469, 207973094, 623476563, 917012307, 712123254};
Integer[] nums = {7, 2, 1, 8, 6, 3, 5, 4};
QuickSort.sort(nums);
if (!isSorted(nums)){
System.out.println("Result: " + Arrays.toString(nums));
}
return;
}
public static void writeStringToFile(String fileContent, String filePath) throws IOException {
BufferedWriter writer = new BufferedWriter(new FileWriter(filePath));
writer.write(fileContent);
writer.close();
}
public static String convertMapToString(TreeMap<Integer, Long> myMap) {
StringWriter stringWriter = new StringWriter();
PrintWriter writer = new PrintWriter(stringWriter, true);
String result = "";
int count = 0;
int lastItem = myMap.size() - 1;
// Format: time in nanoseconds, n, time (in s)
for (Map.Entry<Integer, Long> entry : myMap.entrySet()) {
writer.println(entry.getKey() + ", " + (float)entry.getValue()/1000000000);
}
return stringWriter.toString();
}
public static long[] generateAndSortNumbers(int n, int runCount) {
long[] times = new long[runCount];
for (int i = 0; i < runCount; i++) {
// generate numbers
Integer[] nums = getRandomNumberList(n);
// sort, and time the sorting
long start = System.nanoTime();
// InsertionSort.sort(nums);
// SelectionSort.sort(nums);
nums = MergeSort.sort(nums); // merge sort returns a new array
// MergeSort2.sort(nums, nums.length); // merge sort returns a new array
// QuickSort.sort(nums);
long end = System.nanoTime();
long elapsed = end-start;
times[i] = elapsed;
if (!isSorted(nums)) {
System.out.println("🍎🍎🍎 ERROR ARRAY WAS NOT SORTED🍎🍎🍎");
}
}
return times;
}
public static boolean isSorted(Integer[] nums) {
for (int i = 1; i < nums.length; i++) {
if (nums[i] < nums[i - 1]) {
System.out.println("Array: " + Arrays.toString(nums));
return false;
}
}
return true;
}
public static long getAverage(long[] nums) {
long average = 0;
for (int i = 0; i < nums.length; i++) {
average += nums[i];
}
return average/nums.length;
}
public static Integer[] getRandomNumberList(int count) {
//
// Generate the random numbers to be used in the sort
// All numbers are unique
//
Integer[] nums = new Integer[count];
for (int i = 0; i < count; i++) {
nums[i] = i;
}
if (SHUFFLENUMS) {
Collections.shuffle(Arrays.asList(nums));
}
return nums;
}
public static int getRandomNumber() {
return (int)(Math.random() * 10 * Math.random() * 10 * 200000);
}
}