-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMergesort3.java
More file actions
707 lines (584 loc) · 29.6 KB
/
Mergesort3.java
File metadata and controls
707 lines (584 loc) · 29.6 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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
// public class Mergesort3 {
// static int[] S;
// static int[] U;
// static long totalComparisons = 0; // Counter for comparisons
// static long grandTotalComparisons = 0; // Counter for grand total comparisons
// public static void main(String[] args) {
// System.out.println("Merge Sort Algorithm Used: MergeSort3\n");
// S = new int[]{16, 14, 5, 7, 1, 8, 12, 10, 1, 9};
// int n = S.length;
// U = new int[n];
// mergesort3(S, n);
// System.out.println("Sorted array: ");
// for (int num : S) {
// System.out.print(num + " ");
// }
// System.out.println("\n");
// // Display initial analysis
// System.out.println("Mergesort Analysis:");
// System.out.println(" Algorithm: Bottom-Up Iterative Merge Sort");
// System.out.println(" Total Comparisons Made: " + totalComparisons);
// System.out.println(" Time Complexity: O(n log n)");
// System.out.println(" Space Complexity: O(n)");
// // Performance testing
// int[] sizes = {10, 100, 1000, 10000, 100000, 1000000};
// long grandTotalTimeMillis = 0; // Variable to track grand total time in milliseconds
// long grandTotalMemoryBytes = 0; // Variable to track grand total memory usage in bytes
// for (int size : sizes) {
// int[][] testArrays = {
// ArrayGenerator.generateRandomArray(size),
// ArrayGenerator.generateSortedArray(size),
// ArrayGenerator.generateReversedArray(size),
// ArrayGenerator.generateNearlySortedArray(size, 0.05)
// };
// String[] arrayTypes = {"Random", "Sorted", "Reversed", "Nearly Sorted"};
// for (int t = 0; t < testArrays.length; t++) {
// int[] generatedArray = testArrays[t];
// long totalTime = 0;
// long totalMemoryUsed = 0;
// // Reset local counters
// totalComparisons = 0;
// for (int i = 0; i < 100; i++) {
// int[] arrayCopy = generatedArray.clone();
// // Measure memory usage before sorting
// Runtime runtime = Runtime.getRuntime();
// runtime.gc(); // Request garbage collection
// long beforeMemory = runtime.totalMemory() - runtime.freeMemory();
// long startTime = System.currentTimeMillis();
// mergesort3(arrayCopy, arrayCopy.length);
// long endTime = System.currentTimeMillis();
// // Measure memory usage after sorting
// long afterMemory = runtime.totalMemory() - runtime.freeMemory();
// long memoryUsed = afterMemory - beforeMemory;
// totalMemoryUsed += memoryUsed;
// totalTime += (endTime - startTime);
// }
// long averageTimeMillis = totalTime / 100;
// double averageTimeSeconds = averageTimeMillis / 1000.0;
// long averageMemoryUsed = totalMemoryUsed / 100;
// // Add to grand totals
// grandTotalTimeMillis += totalTime;
// grandTotalMemoryBytes += totalMemoryUsed;
// grandTotalComparisons += totalComparisons;
// long minutes = averageTimeMillis / (60 * 1000);
// double seconds = (averageTimeMillis % (60 * 1000)) / 1000.0;
// System.out.println("Average execution time for " + arrayTypes[t] + " array of size " + size + ":");
// System.out.println(" " + averageTimeMillis + " milliseconds");
// System.out.println(" " + averageTimeSeconds + " seconds");
// System.out.println(" " + minutes + " minutes and " + seconds + " seconds");
// System.out.println(" " + averageMemoryUsed + " bytes of memory used");
// System.out.println(" Total Comparisons: " + totalComparisons);
// }
// }
// double grandTotalTimeSecondsFinal = grandTotalTimeMillis / 1000.0;
// long grandTotalMinutes = grandTotalTimeMillis / (60 * 1000);
// double grandTotalSeconds = (grandTotalTimeMillis % (60 * 1000)) / 1000.0;
// // Display grand totals
// System.out.println("\nGrand Total Execution Time for All Arrays and Sizes:");
// System.out.println(" " + grandTotalTimeMillis + " milliseconds");
// System.out.println(" " + grandTotalTimeSecondsFinal + " seconds");
// System.out.println(" " + grandTotalMinutes + " minutes and " + grandTotalSeconds + " seconds");
// System.out.println(" " + grandTotalMemoryBytes + " bytes of total memory used");
// System.out.println(" " + grandTotalComparisons + " total comparisons made");
// System.out.println("Merge Sort Algorithm Used: MergeSort3\n");
// }
// public static void mergesort3(int[] array, int n) {
// U = new int[n]; // Auxiliary array for merging
// // Start with single element arrays and merge iteratively
// for (int width = 1; width < n; width = 2 * width) {
// for (int i = 0; i < n; i = i + 2 * width) {
// int low = i;
// int mid = Math.min(i + width - 1, n - 1);
// int high = Math.min(i + 2 * width - 1, n - 1);
// // Merge the sorted subarrays
// merge3(array, low, mid, high);
// }
// // Copy merged result from U back to array
// System.arraycopy(U, 0, array, 0, n);
// }
// }
// public static void merge3(int[] array, int low, int mid, int high) {
// int i = low;
// int j = mid + 1;
// int k = low;
// // Merge the two subarrays into U
// while (i <= mid && j <= high) {
// totalComparisons++; // Count each comparison
// if (array[i] <= array[j]) {
// U[k] = array[i];
// i++;
// } else {
// U[k] = array[j];
// j++;
// }
// k++;
// }
// // Copy remaining elements of the left subarray if any
// while (i <= mid) {
// U[k] = array[i];
// i++;
// k++;
// }
// // Copy remaining elements of the right subarray if any
// while (j <= high) {
// U[k] = array[j];
// j++;
// k++;
// }
// }
// }
// import java.util.ArrayList;
// import java.util.List;
// public class Mergesort3 {
// static long totalComparisons = 0; // Counter for comparisons
// static long totalMemoryAllocated = 0; // Counter for memory allocated
// static long grandTotalMemoryBytes = 0; // Grand total memory used
// static long grandTotalComparisons = 0; // Grand total comparisons
// static List<String> detailedResults = new ArrayList<>(); // Detailed summary
// static int[] S; // Primary array
// static int[] U; // Auxiliary array for merging
// public static void main(String[] args) {
// System.out.println("Merge Sort Algorithm Used: MergeSort3 (Dynamic Programming Version)\n");
// // Custom array for demonstration
// int[] customArray = {2679, 7481, 2801, 5558, 5353, 1605, 214, 4653, 3213, 7961};
// System.out.println("Initial Custom Array (size " + customArray.length + "):");
// printArray(customArray);
// // Prepare for sorting
// S = customArray.clone();
// U = new int[S.length]; // Auxiliary array
// mergesort3(S, S.length);
// System.out.println("Sorted Custom Array (size " + customArray.length + "):");
// printArray(S);
// // Performance testing with various sizes and array types
// int[] sizes = {10, 100, 1000, 10000, 100000, 1000000};
// String[] arrayTypes = {"Random Array", "Sorted Array", "Reversed Array", "Nearly Sorted Array"};
// long grandTotalTimeMillis = 0;
// for (int size : sizes) {
// for (String arrayType : arrayTypes) {
// int[] testArray = generateArray(arrayType, size);
// long totalTime = 0;
// long totalMemoryUsed = 0;
// totalComparisons = 0;
// System.out.println("\nInitial " + arrayType + " (size " + size + "):");
// printArray(testArray);
// for (int i = 0; i < 100; i++) {
// int[] arrayCopy = testArray.clone();
// S = arrayCopy.clone();
// U = new int[S.length]; // Reset auxiliary array
// // Measure memory usage
// Runtime runtime = Runtime.getRuntime();
// long beforeMemory = runtime.totalMemory() - runtime.freeMemory();
// long startTime = System.currentTimeMillis();
// mergesort3(S, S.length);
// long endTime = System.currentTimeMillis();
// long afterMemory = runtime.totalMemory() - runtime.freeMemory();
// long memoryUsed = Math.max(0, afterMemory - beforeMemory); // Ensure non-negative values
// totalMemoryUsed += memoryUsed;
// totalTime += (endTime - startTime);
// }
// long averageTimeMillis = totalTime / 100;
// double averageTimeSeconds = averageTimeMillis / 1000.0;
// long averageMemoryUsed = totalMemoryUsed / 100;
// grandTotalTimeMillis += totalTime;
// grandTotalMemoryBytes += totalMemoryUsed;
// grandTotalComparisons += totalComparisons;
// System.out.println("\nSorted " + arrayType + " (size " + size + "):");
// printArray(S);
// System.out.println("\nAverage execution time for " + arrayType + " of size " + size + ":");
// System.out.println(" " + averageTimeMillis + " milliseconds");
// System.out.println(" " + averageTimeSeconds + " seconds");
// System.out.println(" Average Memory Used: " + averageMemoryUsed + " bytes");
// System.out.println(" Total Comparisons for size " + size + ": " + totalComparisons);
// detailedResults.add("Array Type: " + arrayType +
// ", Size: " + size +
// ", Average Time: " + averageTimeMillis + " ms" +
// ", Average Memory Used: " + averageMemoryUsed + " bytes");
// }
// }
// System.out.println("\nInitial Custom Array (Printed Again for Reference):");
// printArray(customArray);
// double grandTotalTimeSecondsFinal = grandTotalTimeMillis / 1000.0;
// long grandTotalMinutes = grandTotalTimeMillis / (60 * 1000);
// double grandTotalSeconds = (grandTotalTimeMillis % (60 * 1000)) / 1000.0;
// System.out.println("\nGrand Total Execution Time for All Sizes:");
// System.out.println(" " + grandTotalTimeMillis + " milliseconds");
// System.out.println(" " + grandTotalTimeSecondsFinal + " seconds");
// System.out.println(" " + grandTotalMinutes + " minutes and " + grandTotalSeconds + " seconds");
// System.out.println(" Grand Total Memory Used: " + grandTotalMemoryBytes + " bytes");
// System.out.println(" Grand Total Comparisons: " + grandTotalComparisons);
// System.out.println("\nDetailed Results Summary:");
// for (String result : detailedResults) {
// System.out.println(result);
// }
// }
// public static void mergesort3(int[] array, int n) {
// boolean useAux = true;
// for (int width = 1; width < n; width *= 2) {
// for (int i = 0; i < n; i += 2 * width) {
// int low = i;
// int mid = Math.min(i + width - 1, n - 1);
// int high = Math.min(i + 2 * width - 1, n - 1);
// if (useAux) {
// merge(array, U, low, mid, high);
// } else {
// merge(U, array, low, mid, high);
// }
// }
// useAux = !useAux; // Alternate between U and S
// }
// if (!useAux) {
// System.arraycopy(U, 0, array, 0, n); // Final copy if necessary
// }
// }
// public static void merge(int[] source, int[] dest, int low, int mid, int high) {
// int i = low, j = mid + 1, k = low;
// while (i <= mid && j <= high) {
// totalComparisons++;
// if (source[i] <= source[j]) {
// dest[k++] = source[i++];
// } else {
// dest[k++] = source[j++];
// }
// }
// while (i <= mid) {
// dest[k++] = source[i++];
// }
// while (j <= high) {
// dest[k++] = source[j++];
// }
// }
// public static void printArray(int[] array) {
// int limit = Math.min(array.length, 25);
// for (int i = 0; i < limit; i++) {
// System.out.print(array[i] + " ");
// }
// if (array.length > 25) {
// System.out.println("... (output limited to 25 elements)");
// } else {
// System.out.println();
// }
// }
// public static int[] generateArray(String type, int size) {
// int[] array = new int[size];
// switch (type) {
// case "Random Array":
// for (int i = 0; i < size; i++) array[i] = (int) (Math.random() * 10000);
// break;
// case "Sorted Array":
// for (int i = 0; i < size; i++) array[i] = i;
// break;
// case "Reversed Array":
// for (int i = 0; i < size; i++) array[i] = size - i;
// break;
// case "Nearly Sorted Array":
// for (int i = 0; i < size; i++) array[i] = i;
// int swaps = (int) (size * 0.05);
// for (int i = 0; i < swaps; i++) {
// int index1 = (int) (Math.random() * size);
// int index2 = (int) (Math.random() * size);
// int temp = array[index1];
// array[index1] = array[index2];
// array[index2] = temp;
// }
// break;
// }
// return array;
// }
// }
// import java.util.ArrayList;
// import java.util.List;
// public class Mergesort3 {
// static int[] S; // Primary array
// static int[] U; // Auxiliary array
// static long totalComparisons = 0; // Counter for comparisons
// static long grandTotalComparisons = 0; // Grand total comparisons
// static long grandTotalMemoryBytes = 0; // Grand total memory usage
// static List<String> detailedResults = new ArrayList<>(); // Summary of results
// public static void main(String[] args) {
// System.out.println("Merge Sort Algorithm Used: MergeSort3\n");
// // Custom array for demonstration
// S = new int[]{16, 14, 5, 7, 1, 8, 12, 10, 1, 9};
// int n = S.length;
// U = new int[n];
// mergesort3(S, n);
// System.out.println("Sorted array:");
// // printArray(S);
// // Display initial analysis
// System.out.println("\nMergesort Analysis:");
// System.out.println(" Algorithm: Bottom-Up Iterative Merge Sort");
// System.out.println(" Total Comparisons Made: " + totalComparisons);
// System.out.println(" Time Complexity: O(n log n)");
// System.out.println(" Space Complexity: O(n)");
// // Performance testing
// int[] sizes = {10, 100, 1000, 10000, 100000, 1000000};
// String[] arrayTypes = {"Random", "Sorted", "Reversed", "Nearly Sorted"};
// long grandTotalTimeMillis = 0; // Variable to track total time
// for (int size : sizes) {
// for (String arrayType : arrayTypes) {
// int[] generatedArray = generateArray(arrayType, size);
// long totalTime = 0;
// long totalMemoryUsed = 0;
// // Reset local counters
// totalComparisons = 0;
// for (int i = 0; i < 100; i++) {
// int[] arrayCopy = generatedArray.clone();
// U = new int[arrayCopy.length]; // Reset auxiliary array
// // Measure memory usage
// Runtime runtime = Runtime.getRuntime();
// runtime.gc(); // Request garbage collection
// long beforeMemory = runtime.totalMemory() - runtime.freeMemory();
// long startTime = System.currentTimeMillis();
// mergesort3(arrayCopy, arrayCopy.length);
// long endTime = System.currentTimeMillis();
// long afterMemory = runtime.totalMemory() - runtime.freeMemory();
// long memoryUsed = Math.max(0, afterMemory - beforeMemory); // Ensure non-negative
// totalMemoryUsed += memoryUsed;
// totalTime += (endTime - startTime);
// }
// long averageTimeMillis = totalTime / 100;
// double averageTimeSeconds = averageTimeMillis / 1000.0;
// long averageMemoryUsed = totalMemoryUsed / 100;
// // Update grand totals
// grandTotalTimeMillis += totalTime;
// grandTotalMemoryBytes += totalMemoryUsed;
// grandTotalComparisons += totalComparisons;
// System.out.println("\nAverage execution time for " + arrayType + " array of size " + size + ":");
// System.out.println(" " + averageTimeMillis + " milliseconds");
// System.out.println(" " + averageTimeSeconds + " seconds");
// System.out.println(" Average Memory Used: " + averageMemoryUsed + " bytes");
// System.out.println(" Total Comparisons: " + totalComparisons);
// detailedResults.add("Array Type: " + arrayType +
// ", Size: " + size +
// ", Average Time: " + averageTimeMillis + " ms" +
// ", Average Memory Used: " + averageMemoryUsed + " bytes");
// }
// }
// double grandTotalTimeSecondsFinal = grandTotalTimeMillis / 1000.0;
// long grandTotalMinutes = grandTotalTimeMillis / (60 * 1000);
// double grandTotalSeconds = (grandTotalTimeMillis % (60 * 1000)) / 1000.0;
// // Display grand totals
// System.out.println("\nGrand Total Execution Time for All Arrays and Sizes:");
// System.out.println(" " + grandTotalTimeMillis + " milliseconds");
// System.out.println(" " + grandTotalTimeSecondsFinal + " seconds");
// System.out.println(" " + grandTotalMinutes + " minutes and " + grandTotalSeconds + " seconds");
// System.out.println(" Grand Total Memory Used: " + grandTotalMemoryBytes + " bytes");
// System.out.println(" Grand Total Comparisons: " + grandTotalComparisons);
// System.out.println("\nDetailed Results Summary:");
// for (String result : detailedResults) {
// System.out.println(result);
// }
// }
// public static void mergesort3(int[] array, int n) {
// U = new int[n]; // Auxiliary array for merging
// for (int width = 1; width < n; width *= 2) {
// for (int i = 0; i < n; i += 2 * width) {
// int low = i;
// int mid = Math.min(i + width - 1, n - 1);
// int high = Math.min(i + 2 * width - 1, n - 1);
// merge(array, low, mid, high);
// }
// System.arraycopy(U, 0, array, 0, n);
// }
// }
// public static void merge(int[] array, int low, int mid, int high) {
// int i = low, j = mid + 1, k = low;
// while (i <= mid && j <= high) {
// totalComparisons++;
// if (array[i] <= array[j]) {
// U[k++] = array[i++];
// } else {
// U[k++] = array[j++];
// }
// }
// while (i <= mid) {
// U[k++] = array[i++];
// }
// while (j <= high) {
// U[k++] = array[j++];
// }
// }
// // public static void printArray(int[] array) {
// // int limit = Math.min(array.length, 25);
// // for (int i = 0; i < limit; i++) {
// // System.out.print(array[i] + " ");
// // }
// // if (array.length > 25) {
// // System.out.println("... (output limited to 25 elements)");
// // } else {
// // System.out.println();
// // }
// // }
// public static int[] generateArray(String type, int size) {
// int[] array = new int[size];
// switch (type) {
// case "Random":
// for (int i = 0; i < size; i++) array[i] = (int) (Math.random() * 10000);
// break;
// case "Sorted":
// for (int i = 0; i < size; i++) array[i] = i;
// break;
// case "Reversed":
// for (int i = 0; i < size; i++) array[i] = size - i;
// break;
// case "Nearly Sorted":
// for (int i = 0; i < size; i++) array[i] = i;
// int swaps = (int) (size * 0.05);
// for (int i = 0; i < swaps; i++) {
// int index1 = (int) (Math.random() * size);
// int index2 = (int) (Math.random() * size);
// int temp = array[index1];
// array[index1] = array[index2];
// array[index2] = temp;
// }
// break;
// }
// return array;
// }
// }
import java.util.ArrayList;
import java.util.List;
public class Mergesort3 {
static int[] S; // Primary array
static int[] U; // Auxiliary array
static long totalComparisons = 0; // Counter for comparisons
static long grandTotalComparisons = 0; // Grand total comparisons
static long grandTotalMemoryBytes = 0; // Grand total memory usage
static List<String> detailedResults = new ArrayList<>(); // Summary of results
public static void main(String[] args) {
System.out.println("Merge Sort Algorithm Used: MergeSort3\n"); // Display the algorithm used
// Test a custom array for demonstration
S = new int[]{16, 14, 5, 7, 1, 8, 12, 10, 1, 9}; // Custom array
int n = S.length; // Length of the custom array
U = new int[n]; // array for merging
System.out.println("Initial Custom Array:");
printArray(S);
mergesort3(S, n);
System.out.println("Sorted Custom Array:");
printArray(S);
// Performance testing
int[] sizes = {10, 100, 1000, 10000, 100000, 1000000}; // Array sizes to test
String[] arrayTypes = {"Random", "Sorted", "Reversed", "Nearly Sorted"}; // Array types to test
long grandTotalTimeMillis = 0;
for (int size : sizes) { // Loop through each size
for (String arrayType : arrayTypes) { // Loop through each array type
int[] generatedArray = generateArray(arrayType, size); // Generate the array
long totalTime = 0;
long totalMemoryUsed = 0;
// Reset counters
totalComparisons = 0;
for (int i = 0; i < 100; i++) { // Run 100 times
int[] arrayCopy = generatedArray.clone(); // Copy the array
U = new int[arrayCopy.length]; // Reset array
// Measure memory usage
Runtime runtime = Runtime.getRuntime();
runtime.gc(); // Request garbage collection
long beforeMemory = runtime.totalMemory() - runtime.freeMemory();
long startTime = System.nanoTime();
mergesort3(arrayCopy, arrayCopy.length); // Sort the array
long endTime = System.nanoTime();
long afterMemory = runtime.totalMemory() - runtime.freeMemory(); // Memory after sorting
long memoryUsed = Math.max(0, afterMemory - beforeMemory);
totalMemoryUsed += memoryUsed;
totalTime += (endTime - startTime);
}
long averageTimeNanos = totalTime / 100;
long averageTimeMillis = averageTimeNanos / 1_000_000;
double averageTimeSeconds = averageTimeNanos / 1_000_000_000.0;
long averageMemoryUsed = totalMemoryUsed / 100;
grandTotalTimeMillis += totalTime / 1_000_000;
grandTotalMemoryBytes += totalMemoryUsed;
grandTotalComparisons += totalComparisons;
detailedResults.add("Array Type: " + arrayType +
", Size: " + size +
", Average Time: " + averageTimeMillis + " ms" +
" (" + averageTimeSeconds + " s)" +
", Average Memory Used: " + averageMemoryUsed + " bytes");
System.out.println("\nAverage execution time for " + arrayType + " array of size " + size + ":");
System.out.println(" " + averageTimeMillis + " milliseconds");
System.out.println(" " + averageTimeSeconds + " seconds");
System.out.println(" Average Memory Used: " + averageMemoryUsed + " bytes");
System.out.println(" Total Comparisons: " + totalComparisons);
}
}
// Print grand totals
double grandTotalTimeSecondsFinal = grandTotalTimeMillis / 1000.0;
long grandTotalMinutes = grandTotalTimeMillis / (60 * 1000);
double grandTotalSeconds = (grandTotalTimeMillis % (60 * 1000)) / 1000.0;
System.out.println("\nGrand Total Execution Time for All Arrays and Sizes:");
System.out.println(" " + grandTotalTimeMillis + " milliseconds");
System.out.println(" " + grandTotalTimeSecondsFinal + " seconds");
System.out.println(" " + grandTotalMinutes + " minutes and " + grandTotalSeconds + " seconds");
System.out.println(" Grand Total Memory Used: " + grandTotalMemoryBytes + " bytes");
System.out.println(" Grand Total Comparisons: " + grandTotalComparisons);
System.out.println("\nDetailed Results Summary:");
for (String result : detailedResults) {
System.out.println(result);
}
}
public static void mergesort3(int[] array, int n) {
U = new int[n]; // Auxiliary array for merging
for (int width = 1; width < n; width *= 2) {
for (int i = 0; i < n; i += 2 * width) {
int low = i;
int mid = Math.min(i + width - 1, n - 1);
int high = Math.min(i + 2 * width - 1, n - 1);
merge(array, low, mid, high);
}
System.arraycopy(U, 0, array, 0, n);
}
}
public static void merge(int[] array, int low, int mid, int high) {
int i = low, j = mid + 1, k = low;
while (i <= mid && j <= high) { // Merge the two subarrays into U
totalComparisons++;
if (array[i] <= array[j]) { // Compare the elements
U[k++] = array[i++]; // Copy the smaller element
} else {
U[k++] = array[j++]; // Copy the smaller element
}
}
while (i <= mid) { // Copy remaining elements of the left subarray if any
U[k++] = array[i++]; // Copy the remaining elements
}
while (j <= high) { // Copy remaining elements of the right subarray if any
U[k++] = array[j++]; // Copy the remaining elements
}
}
public static void printArray(int[] array) {
int limit = Math.min(array.length, 25); // Limit the output to 25 elements
for (int i = 0; i < limit; i++) { // Loop through the array
System.out.print(array[i] + " "); // Print the element
}
if (array.length > 25) { // Check if the array is longer than 25 elements
System.out.println("... (output limited to 25 elements)"); // Print the message
} else {
System.out.println();
}
}
public static int[] generateArray(String type, int size) { // Generate an array based on the type
int[] array = new int[size];
switch (type) {
case "Random":
for (int i = 0; i < size; i++) array[i] = (int) (Math.random() * 10000);
break;
case "Sorted":
for (int i = 0; i < size; i++) array[i] = i;
break;
case "Reversed":
for (int i = 0; i < size; i++) array[i] = size - i;
break;
case "Nearly Sorted":
for (int i = 0; i < size; i++) array[i] = i;
int swaps = (int) (size * 0.05);
for (int i = 0; i < swaps; i++) {
int index1 = (int) (Math.random() * size);
int index2 = (int) (Math.random() * size);
int temp = array[index1];
array[index1] = array[index2];
array[index2] = temp;
}
break;
}
return array;
}
}