Ejemplo n.º 1
0
        public void TestPerformanceForAllSorters()
        {
            int[] original       = GetLargeIntArrayForPerformanceTest();
            int[] bubbleArray    = original.Clone() as int[];
            int[] selectionArray = original.Clone() as int[];
            int[] insertionArray = original.Clone() as int[];
            int[] shellArray     = original.Clone() as int[];
            int[] mergeArray     = original.Clone() as int[];
            int[] quickArray     = original.Clone() as int[];

            int[] almostOrderedArray = original.Clone() as int[];
            IntArraySorter.QuickSort(almostOrderedArray);
            int upperBound = Math.Min(10, almostOrderedArray.Length >> 1);

            for (int i = 0; i < upperBound; i++)
            {
                almostOrderedArray.Swap(i, almostOrderedArray.Length - i - 1);
            }

            int iteration = 1;

            CodeTimer.Time("BubbleSorter", iteration,
                           () => IntArraySorter.BubbleSort(bubbleArray)
                           );

            CodeTimer.Time("SelectionSorter", iteration,
                           () => IntArraySorter.SelectionSort(selectionArray)
                           );

            // Insertion is good for an almost ordered array.
            CodeTimer.Time("InsertionSort_For_Almost_Ordered_Array", iteration,
                           () => IntArraySorter.SelectionSort(almostOrderedArray)
                           );

            CodeTimer.Time("InsertionSort", iteration,
                           () => IntArraySorter.InsertionSort(insertionArray)
                           );

            CodeTimer.Time("ShellSort", iteration,
                           () => IntArraySorter.ShellSort(shellArray)
                           );

            CodeTimer.Time("MergeSort", iteration,
                           () => IntArraySorter.MergeSort(quickArray)
                           );

            // TODO: Make no sense, the array has been changed after one sorting?
            CodeTimer.Time("QuickSort", iteration,
                           () => IntArraySorter.QuickSort(quickArray)
                           );
        }
Ejemplo n.º 2
0
        public void TestShellSort()
        {
            Console.WriteLine("Testing {0} Start...", ObjectHelper.GetMethodName());

            int[] array  = GetIntArray();
            int[] backup = array.Clone() as int[];

            Console.WriteLine("Original Array: ");
            backup.PrintToConsole();

            IntArraySorter.ShellSort(array);
            CollectionAssert.AreEquivalent(backup, array);
            Assert.That(array.IsAscOrdered(), Is.True);

            Console.WriteLine("Ordered Array: ");
            array.PrintToConsole();

            Console.WriteLine("Testing {0} End...", ObjectHelper.GetMethodName());
        }