1
1
using System ;
2
2
using System . Threading . Tasks ;
3
3
4
- namespace PsetS3
4
+ namespace CompareSorts
5
5
{
6
6
class Program
7
7
{
@@ -13,6 +13,7 @@ class Program
13
13
const string S = "Selection" ;
14
14
const string I = "Insertion" ;
15
15
const string Q = "Quick" ;
16
+ const string M = "Merge" ;
16
17
const string A = "All" ;
17
18
18
19
// Variables required to count the number of operations
@@ -30,16 +31,19 @@ class Program
30
31
//Quick:
31
32
static ulong totalComparisonsQuick = 0 ;
32
33
static ulong totalSwapsQuick = 0 ;
34
+ //Merge:
35
+ static ulong totalComparisonsMerge = 0 ;
36
+ static ulong totalSwapsMerge = 0 ;
33
37
34
38
static void Main ( string [ ] args )
35
39
{
36
40
/*
37
41
* Edit these variables to set up!
38
42
*/
39
- string sortingAlgorithm = A ; // B/S/I/Q/A
43
+ string sortingAlgorithm = A ; // B/S/I/Q/M/ A
40
44
string orderOfArray = r ; // a/d/r
41
45
ulong numberOfSorts = 1 ;
42
- int lengthOfArray = 50 ;
46
+ int lengthOfArray = 10 ;
43
47
bool printUpdates = true ;
44
48
45
49
Console . WriteLine ( "Sorting algorithm: {0}\n Results of {1} passes of {2}-int {3} arrays:\n " ,
@@ -57,7 +61,10 @@ static void Main(string[] args)
57
61
// Create array
58
62
int [ ] array = MakeArray ( lengthOfArray , orderOfArray ) ;
59
63
60
- // Call Bubble||Selection||Insertion||Quick sort, and add correct variables
64
+ if ( printUpdates && sortingAlgorithm != A )
65
+ PrintArray ( array , "initial array" ) ;
66
+
67
+ // Call Bubble||Selection||Insertion||Quick||Merge sort, and add correct variables
61
68
if ( sortingAlgorithm == B )
62
69
BubbleSort ( array , printUpdates ) ;
63
70
else if ( sortingAlgorithm == S )
@@ -66,29 +73,51 @@ static void Main(string[] args)
66
73
InsertionSort ( array , printUpdates ) ;
67
74
else if ( sortingAlgorithm == Q )
68
75
QuickSort ( array , 0 , array . Length - 1 , printUpdates ) ;
69
- else if ( sortingAlgorithm == A )
76
+ else if ( sortingAlgorithm == M )
77
+ MergeSort ( array , printUpdates ) ;
78
+ else if ( sortingAlgorithm == A )
70
79
{
71
80
// Copy each array (since they're arrays of ints, shallow copy is enough)
72
81
int [ ] array1 = ( int [ ] ) array . Clone ( ) ;
73
82
int [ ] array2 = ( int [ ] ) array . Clone ( ) ;
74
83
int [ ] array3 = ( int [ ] ) array . Clone ( ) ;
84
+ int [ ] array4 = ( int [ ] ) array . Clone ( ) ;
75
85
76
86
if ( printUpdates )
77
- Console . WriteLine ( "Bubble sort:" ) ;
87
+ {
88
+ Console . WriteLine ( "\n Bubble sort:" ) ;
89
+ PrintArray ( array , "initial array" ) ;
90
+ }
78
91
BubbleSort ( array , printUpdates ) ;
79
92
80
93
if ( printUpdates )
94
+ {
81
95
Console . WriteLine ( "\n Selection sort:" ) ;
96
+ PrintArray ( array , "initial array" ) ;
97
+ }
82
98
SelectionSort ( array1 , printUpdates ) ;
83
99
84
100
if ( printUpdates )
101
+ {
85
102
Console . WriteLine ( "\n Insertion sort:" ) ;
103
+ PrintArray ( array , "initial array" ) ;
104
+ }
86
105
InsertionSort ( array2 , printUpdates ) ;
87
106
88
107
if ( printUpdates )
108
+ {
89
109
Console . WriteLine ( "\n Quick sort:" ) ;
110
+ PrintArray ( array , "initial array" ) ;
111
+ }
90
112
QuickSort ( array3 , 0 , array3 . Length - 1 , printUpdates ) ;
91
113
114
+ if ( printUpdates )
115
+ {
116
+ Console . WriteLine ( "\n Merge sort:" ) ;
117
+ PrintArray ( array , "initial array" ) ;
118
+ }
119
+ MergeSort ( array4 , printUpdates ) ;
120
+
92
121
if ( printUpdates )
93
122
Console . WriteLine ( ) ;
94
123
}
@@ -143,9 +172,6 @@ private static int[] MakeArray(int length, string orderOfArray)
143
172
*/
144
173
private static int [ ] BubbleSort ( int [ ] array , bool printUpdates )
145
174
{
146
- if ( printUpdates )
147
- PrintArray ( array , "initial array" ) ;
148
-
149
175
ulong comparisons = 0 ;
150
176
ulong swaps = 0 ;
151
177
// Get (length - 1) because we compare with (j + 1)
@@ -212,9 +238,6 @@ private static int[] BubbleSort(int[] array, bool printUpdates)
212
238
*/
213
239
private static int [ ] SelectionSort ( int [ ] array , bool printUpdates )
214
240
{
215
- if ( printUpdates )
216
- PrintArray ( array , "initial array" ) ;
217
-
218
241
ulong comparisons = 0 ;
219
242
ulong swaps = 0 ;
220
243
int n = array . Length ;
@@ -276,9 +299,6 @@ private static int[] SelectionSort(int[] array, bool printUpdates)
276
299
*/
277
300
private static int [ ] InsertionSort ( int [ ] array , bool printUpdates )
278
301
{
279
- if ( printUpdates )
280
- PrintArray ( array , "initial array" ) ;
281
-
282
302
// Since elements aren't directly swapped, it doesn't make sense to count swaps like the other algorithms.
283
303
// Instead, we count shuffles (when an element moves up one index)
284
304
// and insertions (where the currentElement is inserted into its correct index for that pass)
@@ -365,9 +385,6 @@ private static int[] InsertionSort(int[] array, bool printUpdates)
365
385
*/
366
386
private static void QuickSort ( int [ ] array , int start , int end , bool printUpdates )
367
387
{
368
- if ( start == 0 && end == array . Length - 1 && printUpdates )
369
- PrintArray ( array , "initial array" ) ;
370
-
371
388
// Base case
372
389
if ( start >= end )
373
390
{
@@ -431,6 +448,81 @@ private static int Partition(int[] array, int start, int end, bool printUpdates)
431
448
return partitionIndex ;
432
449
}
433
450
451
+ private static void MergeSort ( int [ ] array , bool printUpdates )
452
+ {
453
+ int n = array . Length ;
454
+ int mid = n / 2 ;
455
+
456
+ // Base case
457
+ if ( n < 2 )
458
+ {
459
+ if ( printUpdates )
460
+ Console . WriteLine ( " Base case reached" ) ;
461
+ return ;
462
+ }
463
+
464
+ // Split the array into two halves
465
+ if ( printUpdates )
466
+ Console . WriteLine ( "Recursive call on left side, from index 0 to {0}:" , mid - 1 ) ;
467
+ int [ ] left = new int [ mid ] ;
468
+ Array . Copy ( array , 0 , left , 0 , mid ) ;
469
+ if ( printUpdates )
470
+ Console . WriteLine ( "Recursive call on right side, from index {0} to {1}:" , mid , n - 1 ) ;
471
+ int [ ] right = new int [ n - mid ] ;
472
+ Array . Copy ( array , mid , right , 0 , n - mid ) ;
473
+
474
+ //Recursive call to sort the two halves
475
+ MergeSort ( left , printUpdates ) ;
476
+ MergeSort ( right , printUpdates ) ;
477
+
478
+ // Call to merge the sorted halves into a sorted whole
479
+ Merge ( array , left , right , printUpdates ) ;
480
+ }
481
+
482
+ // Merges two sorted arrays into one sorted array
483
+ private static void Merge ( int [ ] array , int [ ] leftArray , int [ ] rightArray , bool printUpdates )
484
+ {
485
+ ulong comparisons = 0 ;
486
+ ulong swaps = 0 ;
487
+
488
+ int leftCount = leftArray . Length ;
489
+ int rightCount = rightArray . Length ;
490
+
491
+ int l = 0 ; // index of left sub-array
492
+ int r = 0 ; // index of right sub-array
493
+ int m = 0 ; // index of merged array
494
+
495
+
496
+ // Copy the next value in left and right sub-arrays to the main array
497
+ while ( l < leftCount && r < rightCount )
498
+ {
499
+ comparisons ++ ;
500
+ swaps ++ ;
501
+ if ( leftArray [ l ] < rightArray [ r ] )
502
+ array [ m ++ ] = leftArray [ l ++ ] ;
503
+ else
504
+ array [ m ++ ] = rightArray [ r ++ ] ;
505
+ }
506
+ // If end of right subarray reached first, copy remaining elements from left subarray
507
+ while ( l < leftCount )
508
+ {
509
+ swaps ++ ;
510
+ array [ m ++ ] = leftArray [ l ++ ] ;
511
+ }
512
+ // If end of left subarray reached first, copy remaining elements from right subarray
513
+ while ( r < rightCount )
514
+ {
515
+ swaps ++ ;
516
+ array [ m ++ ] = rightArray [ r ++ ] ;
517
+ }
518
+
519
+ if ( printUpdates )
520
+ PrintArray ( array , "<- After a completed Merge of two sub-arrays" ) ;
521
+
522
+ totalComparisonsMerge += comparisons ;
523
+ totalSwapsMerge += swaps ;
524
+ }
525
+
434
526
// Prints the current array with a message to describe the situation
435
527
private static void PrintArray ( int [ ] array , string text )
436
528
{
@@ -492,6 +584,15 @@ private static void DisplayResults(string sortingAlgorithm, ulong numberOfSorts,
492
584
Console . WriteLine ( "Total comparisons: {0}\n Average comparisons: {1}\n " , totalComparisonsQuick , averageComparisonsQuick ) ;
493
585
Console . WriteLine ( "Total swaps: {0}\n Average swaps: {1}\n " , totalSwapsQuick , averageSwapsQuick ) ;
494
586
}
587
+ if ( sortingAlgorithm == "Merge" || sortingAlgorithm == "All" )
588
+ {
589
+ ulong averageComparisonsMerge = totalComparisonsMerge / numberOfSorts ;
590
+ ulong averageSwapsMerge = totalSwapsMerge / numberOfSorts ;
591
+ if ( sortingAlgorithm == "All" )
592
+ Console . WriteLine ( "Merge sort results:\n " ) ;
593
+ Console . WriteLine ( "Total comparisons: {0}\n Average comparisons: {1}\n " , totalComparisonsMerge , averageComparisonsMerge ) ;
594
+ Console . WriteLine ( "Total swaps: {0}\n Average swaps: {1}\n " , totalSwapsMerge , averageSwapsMerge ) ;
595
+ }
495
596
}
496
597
}
497
598
}
0 commit comments