@@ -28,8 +28,10 @@ public class SortsTiming {
28
28
private static final double [] bubbleResults = new double [1 * 3 ];
29
29
private static int shellCount = 0 ;
30
30
private static final double [] shellResults = new double [1 * 3 ];
31
- private static int mergeCount = 0 ;
32
- private static final double [] mergeResults = new double [1 * 3 ];
31
+ private static int mergeInPlaceCount = 0 ;
32
+ private static final double [] mergeInPlaceResults = new double [1 * 3 ];
33
+ private static int mergeNotInPlaceCount = 0 ;
34
+ private static final double [] mergeNotInPlaceResults = new double [1 * 3 ];
33
35
private static int quickCount = 0 ;
34
36
private static final double [] quickResults = new double [3 * 3 ];
35
37
private static int heapCount = 0 ;
@@ -44,7 +46,8 @@ public class SortsTiming {
44
46
private static final boolean showInsertion = true ;
45
47
private static final boolean showBubble = true ;
46
48
private static final boolean showShell = true ;
47
- private static final boolean showMerge = true ;
49
+ private static final boolean showMergeInPlace = true ;
50
+ private static final boolean showMergeNotInPlace = true ;
48
51
private static final boolean showQuick = true ;
49
52
private static final boolean showHeap = true ;
50
53
private static final boolean showCounting = true ;
@@ -221,10 +224,10 @@ public static void main(String[] args) {
221
224
System .out .flush ();
222
225
}
223
226
224
- if (showMerge ) {
227
+ if (showMergeNotInPlace ) {
225
228
// Merge sort
226
229
long bMerge = System .nanoTime ();
227
- Integer [] result = MergeSort .sort (unsorted .clone ());
230
+ Integer [] result = MergeSort .sort (MergeSort . SPACE_TYPE . NOT_IN_PLACE , unsorted .clone ());
228
231
if (checkResults && !check (result ))
229
232
System .err .println ("MergeSort failed." );
230
233
long aMerge = System .nanoTime ();
@@ -233,11 +236,11 @@ public static void main(String[] args) {
233
236
if (showResult )
234
237
showResult (unsorted , result );
235
238
if (showComparison )
236
- mergeResults [ mergeCount ++] = diff ;
239
+ mergeNotInPlaceResults [ mergeNotInPlaceCount ++] = diff ;
237
240
System .gc ();
238
241
239
242
bMerge = System .nanoTime ();
240
- result = MergeSort .sort (sorted .clone ());
243
+ result = MergeSort .sort (MergeSort . SPACE_TYPE . NOT_IN_PLACE , sorted .clone ());
241
244
if (checkResults && !check (result ))
242
245
System .err .println ("MergeSort failed." );
243
246
aMerge = System .nanoTime ();
@@ -246,11 +249,11 @@ public static void main(String[] args) {
246
249
if (showResult )
247
250
showResult (sorted , result );
248
251
if (showComparison )
249
- mergeResults [ mergeCount ++] = diff ;
252
+ mergeNotInPlaceResults [ mergeNotInPlaceCount ++] = diff ;
250
253
System .gc ();
251
254
252
255
bMerge = System .nanoTime ();
253
- result = MergeSort .sort (reverse .clone ());
256
+ result = MergeSort .sort (MergeSort . SPACE_TYPE . NOT_IN_PLACE , reverse .clone ());
254
257
if (checkResults && !check (result ))
255
258
System .err .println ("MergeSort failed." );
256
259
aMerge = System .nanoTime ();
@@ -259,7 +262,52 @@ public static void main(String[] args) {
259
262
if (showResult )
260
263
showResult (reverse , result );
261
264
if (showComparison )
262
- mergeResults [mergeCount ++] = diff ;
265
+ mergeNotInPlaceResults [mergeNotInPlaceCount ++] = diff ;
266
+ System .gc ();
267
+
268
+ System .out .println ();
269
+ System .out .flush ();
270
+ }
271
+
272
+ if (showMergeInPlace ) {
273
+ // Merge sort
274
+ long bMerge = System .nanoTime ();
275
+ Integer [] result = MergeSort .sort (MergeSort .SPACE_TYPE .IN_PLACE , unsorted .clone ());
276
+ if (checkResults && !check (result ))
277
+ System .err .println ("MergeSort failed." );
278
+ long aMerge = System .nanoTime ();
279
+ double diff = (aMerge - bMerge ) / 1000000d / 1000d ;
280
+ System .out .println ("Random: MergeSort=" + FORMAT .format (diff ) + " secs" );
281
+ if (showResult )
282
+ showResult (unsorted , result );
283
+ if (showComparison )
284
+ mergeInPlaceResults [mergeInPlaceCount ++] = diff ;
285
+ System .gc ();
286
+
287
+ bMerge = System .nanoTime ();
288
+ result = MergeSort .sort (MergeSort .SPACE_TYPE .IN_PLACE , sorted .clone ());
289
+ if (checkResults && !check (result ))
290
+ System .err .println ("MergeSort failed." );
291
+ aMerge = System .nanoTime ();
292
+ diff = (aMerge - bMerge ) / 1000000d / 1000d ;
293
+ System .out .println ("Sorted: MergeSort=" + FORMAT .format (diff ) + " secs" );
294
+ if (showResult )
295
+ showResult (sorted , result );
296
+ if (showComparison )
297
+ mergeInPlaceResults [mergeInPlaceCount ++] = diff ;
298
+ System .gc ();
299
+
300
+ bMerge = System .nanoTime ();
301
+ result = MergeSort .sort (MergeSort .SPACE_TYPE .IN_PLACE , reverse .clone ());
302
+ if (checkResults && !check (result ))
303
+ System .err .println ("MergeSort failed." );
304
+ aMerge = System .nanoTime ();
305
+ diff = (aMerge - bMerge ) / 1000000d / 1000d ;
306
+ System .out .println ("Reverse sorted: MergeSort=" + FORMAT .format (diff ) + " secs" );
307
+ if (showResult )
308
+ showResult (reverse , result );
309
+ if (showComparison )
310
+ mergeInPlaceResults [mergeInPlaceCount ++] = diff ;
263
311
System .gc ();
264
312
265
313
System .out .println ();
@@ -593,9 +641,13 @@ private static final void showComparison() {
593
641
int i = 0 ;
594
642
System .out .println ("Shell sort\t \t \t " + FORMAT .format (shellResults [i ++]) + "\t " + FORMAT .format (shellResults [i ++]) + "\t " + FORMAT .format (shellResults [i ++]));
595
643
}
596
- if (showMerge ) {
644
+ if (showMergeInPlace ) {
645
+ int i = 0 ;
646
+ System .out .println ("Merge (in-place) sort\t \t " + FORMAT .format (mergeInPlaceResults [i ++]) + "\t " + FORMAT .format (mergeInPlaceResults [i ++]) + "\t " + FORMAT .format (mergeInPlaceResults [i ++]));
647
+ }
648
+ if (showMergeNotInPlace ) {
597
649
int i = 0 ;
598
- System .out .println ("Merge sort\t \t \t " + FORMAT .format (mergeResults [i ++]) + "\t " + FORMAT .format (mergeResults [i ++]) + "\t " + FORMAT .format (mergeResults [i ++]));
650
+ System .out .println ("Merge (not-in-place) sort\t " + FORMAT .format (mergeNotInPlaceResults [i ++]) + "\t " + FORMAT .format (mergeNotInPlaceResults [i ++]) + "\t " + FORMAT .format (mergeNotInPlaceResults [i ++]));
599
651
}
600
652
if (showQuick ) {
601
653
int i = 0 ;
0 commit comments