Пример #1
0
 private static void MergeSort(int[] sequence, int p, int r, SortingCalibrator calibrator)
 {
     if (p < r)
     {
         if (r - p <= calibrator.K)
         {
             // Using insertion sort on low N values is a faster operation than merge sort.
             InsertionSort(sequence, p, r);
         }
         else
         {
             int q = (p + r) / 2;
             MergeSort(sequence, p, q);
             MergeSort(sequence, q + 1, r);
             Merge(sequence, p, q, r);
         }
     }
 }
Пример #2
0
        static void Main(string[] args)
        {
            //sequence = new int[50000000];
            bool      isActive  = true;
            Stopwatch stopWatch = new Stopwatch();

            Console.WriteLine("SequenceSort: A project by Chase Franz\n");

            SortingCalibrator calibrator = new SortingCalibrator();

            //calibrator.Calibrate();

            do
            {
                AskUserForSequenceSize();
                AskUserForSortAlgorithmChoice();
                bool validInput = CollectAndValidateUserInput();

                // TODO: Use Enum instead of 'magic numbers'
                if (validInput)
                {
                    try
                    {
                        switch (userInput)
                        {
                        case -1:
                            // User wants to end loop
                            isActive = false;
                            break;

                        case 1:
                            sequence.PopulateWithRandomValues();
                            stopWatch.Reset();
                            stopWatch.Start();

                            // Execute Sort
                            sequence.InsertionSort();

                            stopWatch.Stop();
                            LogSortDetails(stopWatch.Elapsed, "Insertion Sort");
                            break;

                        case 2:
                            sequence.PopulateWithRandomValues();
                            stopWatch.Reset();
                            stopWatch.Start();

                            // Execute Sort
                            sequence.SelectionSort();

                            stopWatch.Stop();
                            LogSortDetails(stopWatch.Elapsed, "Selection Sort");
                            break;

                        case 3:
                            sequence.PopulateWithRandomValues();

                            stopWatch.Reset();
                            stopWatch.Start();

                            // Execute Sort
                            sequence.MergeSort(false);

                            stopWatch.Stop();
                            LogSortDetails(stopWatch.Elapsed, "Merge Sort");
                            break;

                        case 4:
                            sequence.PopulateWithRandomValues();

                            stopWatch.Reset();
                            stopWatch.Start();

                            // Execute Sort
                            sequence.MergeSort(true);

                            stopWatch.Stop();
                            LogSortDetails(stopWatch.Elapsed, "Merge Sort");
                            break;

                        default:
                            throw new ArgumentException("Unhandled user input");
                        }
                    }
                    catch (Exception ex)
                    {
                        LogException(ex);
                    }
                }
                else
                {
                    Console.WriteLine("Invalid Input");
                }
            }while (isActive);
            Console.WriteLine("Closing Project...");
        }