-
Notifications
You must be signed in to change notification settings - Fork 0
/
MergeSortTester.java
244 lines (190 loc) · 7.74 KB
/
MergeSortTester.java
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
/*======================================
class MergeSortTester
ALGORITHM:
1. if the array length is less than or equal to 1, return the array. (base case for recursion; an array of length one is already sorted).
2. If the length is greater than one, split the array into two, then sort each half(using the same method, so this method is recursive).
3. MergeSort.merge your two halves together, which will take your two halves, which are both sorted, and combine them to make a sorted whole.
BIG-OH CLASSIFICATION OF ALGORITHM:
O(nlogn)
Mean execution times for dataset of size n:
Batch size: 1000
n=1 time: ~107 nanoseconds
n=10 time: ~1796 nanoseconds
n=100 time: ~14,800 nanoseconds
n=1000 time: ~138,477 nanoseconds
n=10000 time: ~1,439,076 nanoseconds
n=100000 time: ~16,682,320 nanoseconds
n=1000000 time: ~176,653,200 nanoseconds
ANALYSIS:
Even though our runtime is not exactly nlogn, it somewhat resembles an nlogn curve with the base of the logarithm being about 1.02.
======================================*/
import java.util.Arrays;
public class MergeSortTester
{
public static int[] pop(int[] arr){
for(int i=0; i<arr.length; i++){
arr[i]= (int) (Math.random()*100);
}
return arr;
}
public static long[] getVals(int[] arrpop){
long[] data= new long[1000];
int ctr= 1;
while(ctr<=1000){
long startTime=System.nanoTime();
MergeSort.sort(pop(arrpop));
long endTime=System.nanoTime();
long duration= (endTime-startTime);
data[ctr-1]=duration;
ctr+=1;
}
return data;
}
public static double getSum(long[] dataSet){
double sum=0;
int ctr=0;
while (ctr<1000){
sum+= dataSet[ctr];
ctr+=1;
}
return sum;
}
public static double getAvg(double sum){
return sum/1000;
}
/******************************
* execution time analysis
* We defined the start time and then subtracted that from the start time.
* to get the execution time we subtracted the start time from the end time.
*In order to get the time we used the built in nanoTime.
******************************/
public static void main( String[] args )
{
int[] arr1=new int[1];
System.out.println("average for n=1)");
System.out.println(getAvg(getSum(getVals(pop(arr1)))));
System.out.println("average for n=1)");
System.out.println(getAvg(getSum(getVals(pop(arr1)))));
System.out.println("average for n=1)");
System.out.println(getAvg(getSum(getVals(pop(arr1)))));
System.out.println("average for n=1)");
System.out.println(getAvg(getSum(getVals(pop(arr1)))));
System.out.println("average for n=1)");
System.out.println(getAvg(getSum(getVals(pop(arr1)))));
int[] arr10=new int[10];
System.out.println("average for n=10)");
System.out.println(getAvg(getSum(getVals(pop(arr10)))));
System.out.println("average for n=10)");
System.out.println(getAvg(getSum(getVals(pop(arr10)))));
System.out.println("average for n=10)");
System.out.println(getAvg(getSum(getVals(pop(arr10)))));
System.out.println("average for n=10)");
System.out.println(getAvg(getSum(getVals(pop(arr10)))));
System.out.println("average for n=10)");
System.out.println(getAvg(getSum(getVals(pop(arr10)))));
System.out.println("average for n=10)");
System.out.println(getAvg(getSum(getVals(pop(arr10)))));
System.out.println("average for n=10)");
System.out.println(getAvg(getSum(getVals(pop(arr10)))));
int[] arr100=new int[100];
System.out.println("average for n=100)");
System.out.println(getAvg(getSum(getVals(pop(arr100)))));
System.out.println("average for n=100)");
System.out.println(getAvg(getSum(getVals(pop(arr100)))));
System.out.println("average for n=100)");
System.out.println(getAvg(getSum(getVals(pop(arr100)))));
System.out.println("average for n=100)");
System.out.println(getAvg(getSum(getVals(pop(arr100)))));
System.out.println("average for n=100)");
System.out.println(getAvg(getSum(getVals(pop(arr100)))));
int[] arr1000=new int[1000];
System.out.println("average for n=1,000)");
System.out.println(getAvg(getSum(getVals(pop(arr1000)))));
System.out.println("average for n=1,000)");
System.out.println(getAvg(getSum(getVals(pop(arr1000)))));
System.out.println("average for n=1,000)");
System.out.println(getAvg(getSum(getVals(pop(arr1000)))));
System.out.println("average for n=1,000)");
System.out.println(getAvg(getSum(getVals(pop(arr1000)))));
System.out.println("average for n=1,000)");
System.out.println(getAvg(getSum(getVals(pop(arr1000)))));
System.out.println("average for n=1,000)");
System.out.println(getAvg(getSum(getVals(pop(arr1000)))));
System.out.println("average for n=1,000)");
System.out.println(getAvg(getSum(getVals(pop(arr1000)))));
int[] arr10000=new int[10000];
System.out.println("average for n=10,000)");
System.out.println(getAvg(getSum(getVals(pop(arr10000)))));
System.out.println("average for n=10,000)");
System.out.println(getAvg(getSum(getVals(pop(arr10000)))));
System.out.println("average for n=10,000)");
System.out.println(getAvg(getSum(getVals(pop(arr10000)))));
System.out.println("average for n=10,000)");
System.out.println(getAvg(getSum(getVals(pop(arr10000)))));
System.out.println("average for n=10,000)");
System.out.println(getAvg(getSum(getVals(pop(arr10000)))));
int[] arr100000=new int[100000];
System.out.println("average for n=100,000)");
System.out.println(getAvg(getSum(getVals(pop(arr100000)))));
System.out.println("average for n=100,000)");
System.out.println(getAvg(getSum(getVals(pop(arr100000)))));
System.out.println("average for n=100,000)");
System.out.println(getAvg(getSum(getVals(pop(arr100000)))));
System.out.println("average for n=100,000)");
System.out.println(getAvg(getSum(getVals(pop(arr100000)))));
System.out.println("average for n=100,000)");
System.out.println(getAvg(getSum(getVals(pop(arr100000)))));
int[] arr1000000=new int[1000000];
System.out.println("average for n=1,000,000)");
System.out.println(getAvg(getSum(getVals(pop(arr1000000)))));
System.out.println("average for n=1,000,000)");
System.out.println(getAvg(getSum(getVals(pop(arr1000000)))));
System.out.println("average for n=1,000,000)");
System.out.println(getAvg(getSum(getVals(pop(arr1000000)))));
System.out.println("average for n=1,000,000)");
System.out.println(getAvg(getSum(getVals(pop(arr1000000)))));
System.out.println("average for n=1,000,000)");
System.out.println(getAvg(getSum(getVals(pop(arr1000000)))));
/*
// System.out.println(Arrays.toString(arr1));
long startTime = System.nanoTime();
MergeSort.sort(arr1);
long endTime = System.nanoTime();
MergeSort.sort(arr1);
//System.out.println(Arrays.toString(arr1));
long duration = (endTime - startTime);
System.out.println("duration for n=1");
System.out.println(duration);
int[] arr10=new int[10];
pop(arr10);
//System.out.println(Arrays.toString(arr10));
long sTime = System.nanoTime();
MergeSort.sort(arr10);
long eTime = System.nanoTime();
MergeSort.sort(arr10);
long d = (eTime - sTime);
System.out.println("duration for n=10");
System.out.println(d);
int[] arr100=new int[100];
pop(arr100);
//System.out.println(Arrays.toString(arr100));
long s = System.nanoTime();
MergeSort.sort(arr100);
long e = System.nanoTime();
MergeSort.sort(arr100);
long dur = (e - s);
System.out.println("duration for n=100");
System.out.println(dur);
int[] big=new int[10000000];
pop(big);
// System.out.println(Arrays.toString(big));
long sBig = System.nanoTime();
MergeSort.sort(big);
long eBig = System.nanoTime();
// System.out.println(Arrays.toString(MergeSort.sort(big)));
long durBig = (eBig - sBig);
System.out.println("duration for n=10,000,000");
System.out.println(durBig);
*/
}//end main
}//end class