| 
 | 1 | +using System;  | 
 | 2 | +using System.Threading.Tasks;  | 
 | 3 | + | 
 | 4 | +namespace CompareSorts  | 
 | 5 | +{  | 
 | 6 | +    public static class CallSpecifiedSort  | 
 | 7 | +    {  | 
 | 8 | +        public const string a = "ascending",  | 
 | 9 | +                            d = "descending",  | 
 | 10 | +                            r = "random";  | 
 | 11 | + | 
 | 12 | +        public const string B = "Bubble",  | 
 | 13 | +                            S = "Selection",  | 
 | 14 | +                            I = "Insertion",  | 
 | 15 | +                            Q = "Quick",  | 
 | 16 | +                            M = "Merge",  | 
 | 17 | +                            A = "All";  | 
 | 18 | + | 
 | 19 | +        public static void Init(string sortingAlgorithm, string orderOfInitialArray, ulong numberOfSorts, int lengthOfArray, bool printUpdates)  | 
 | 20 | +        {  | 
 | 21 | +            /* Initialise specified number of arrays of ints  | 
 | 22 | +             * Pass each one to Bubble||Selection||Insertion||Quick sort  | 
 | 23 | +             * Count total number of comparisons/swaps/shuffles/insertions as required  | 
 | 24 | +             */  | 
 | 25 | +            for (int i = 0; i < (int)numberOfSorts; i++)  | 
 | 26 | +            {  | 
 | 27 | +                if (numberOfSorts > 1 && printUpdates == true)  | 
 | 28 | +                    Console.WriteLine("\n\nRound {0}:\n", (i + 1));  | 
 | 29 | + | 
 | 30 | +                // Create array  | 
 | 31 | +                int[] array = ArrayOperations.Make(lengthOfArray, orderOfInitialArray);  | 
 | 32 | + | 
 | 33 | +                if (printUpdates && sortingAlgorithm != A)  | 
 | 34 | +                    ArrayOperations.Print(array, "initial array");  | 
 | 35 | + | 
 | 36 | +                switch (sortingAlgorithm)  | 
 | 37 | +                {  | 
 | 38 | +                    case B:  | 
 | 39 | +                        Bubble.Sort(array, printUpdates);  | 
 | 40 | +                        break;  | 
 | 41 | +                    case S:  | 
 | 42 | +                        Selection.Sort(array, printUpdates);  | 
 | 43 | +                        break;  | 
 | 44 | +                    case I:  | 
 | 45 | +                        Insertion.Sort(array, printUpdates);  | 
 | 46 | +                        break;  | 
 | 47 | +                    case Q:  | 
 | 48 | +                        Quick.Sort(array, 0, array.Length - 1, printUpdates);  | 
 | 49 | +                        break;  | 
 | 50 | +                    case M:  | 
 | 51 | +                        Merge.Sort(array, printUpdates);  | 
 | 52 | +                        break;  | 
 | 53 | +                    default:  | 
 | 54 | +                        CallAllSorts(array, printUpdates);  | 
 | 55 | +                        break;  | 
 | 56 | +                }  | 
 | 57 | +            }  | 
 | 58 | + | 
 | 59 | +        }  | 
 | 60 | + | 
 | 61 | +        /* Calls all sorting algorithms and prints additional information to specify which sort is which */  | 
 | 62 | +        private static void CallAllSorts(int[] array, bool printUpdates)  | 
 | 63 | +        {  | 
 | 64 | +            // Copy each array (since they're arrays of ints, shallow copy is enough)  | 
 | 65 | +            int[] array1 = (int[])array.Clone();  | 
 | 66 | +            int[] array2 = (int[])array.Clone();  | 
 | 67 | +            int[] array3 = (int[])array.Clone();  | 
 | 68 | +            int[] array4 = (int[])array.Clone();  | 
 | 69 | + | 
 | 70 | +            if (printUpdates)  | 
 | 71 | +            {  | 
 | 72 | +                Console.WriteLine("\nBubble sort:" +  | 
 | 73 | +                                  "\n************");  | 
 | 74 | +                ArrayOperations.Print(array, "initial array");  | 
 | 75 | +            }  | 
 | 76 | +            Bubble.Sort(array, printUpdates);  | 
 | 77 | + | 
 | 78 | +            if (printUpdates)  | 
 | 79 | +            {  | 
 | 80 | +                Console.WriteLine("\nSelection sort:" +  | 
 | 81 | +                                  "\n***************");  | 
 | 82 | +                ArrayOperations.Print(array1, "initial array");  | 
 | 83 | +            }  | 
 | 84 | +            Selection.Sort(array1, printUpdates);  | 
 | 85 | + | 
 | 86 | +            if (printUpdates)  | 
 | 87 | +            {  | 
 | 88 | +                Console.WriteLine("\nInsertion sort:" +  | 
 | 89 | +                                  "\n***************");  | 
 | 90 | +                ArrayOperations.Print(array2, "initial array");  | 
 | 91 | +            }  | 
 | 92 | +            Insertion.Sort(array2, printUpdates);  | 
 | 93 | + | 
 | 94 | +            if (printUpdates)  | 
 | 95 | +            {  | 
 | 96 | +                Console.WriteLine("\nQuick sort:" +  | 
 | 97 | +                                  "\n***********");  | 
 | 98 | +                ArrayOperations.Print(array3, "initial array");  | 
 | 99 | +            }  | 
 | 100 | +            Quick.Sort(array3, 0, array3.Length - 1, printUpdates);  | 
 | 101 | + | 
 | 102 | +            if (printUpdates)  | 
 | 103 | +            {  | 
 | 104 | +                Console.WriteLine("\nMerge sort:" +  | 
 | 105 | +                                  "\n***********");  | 
 | 106 | +                ArrayOperations.Print(array4, "initial array");  | 
 | 107 | +            }  | 
 | 108 | +            Merge.Sort(array4, printUpdates);  | 
 | 109 | + | 
 | 110 | +            if (printUpdates)  | 
 | 111 | +                Console.WriteLine();  | 
 | 112 | +        }  | 
 | 113 | +    }  | 
 | 114 | +}  | 
0 commit comments