示例#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)
                           );
        }
示例#2
0
        public void TestInsertionSortPart()
        {
            int[] array = { 9, 3, 8, 7, 2, 8 };
            Console.WriteLine("Original Array: ");
            array.PrintToConsole();

            IntArraySorter.InsertionSort(array, 1, 4);
            Assert.AreEqual(array[0], 9);
            Assert.AreEqual(array[1], 2);
            Assert.AreEqual(array[4], 8);

            Console.WriteLine("Ordered Array: ");
            array.PrintToConsole();
        }
示例#3
0
        public void TestIsOrdered()
        {
            int[] array = { 2 };
            Assert.That(IntArraySorter.IsOrdered(array), Is.True);

            array = new int[] { 2, 3, 5 };
            Assert.That(IntArraySorter.IsOrdered(array), Is.True);

            array = new int[] { 2, 3, 3, 5, 5 };
            Assert.That(IntArraySorter.IsOrdered(array), Is.True);

            array = new int[] { 2, 3, 5, 4, 1 };
            Assert.That(IntArraySorter.IsOrdered(array), Is.False);
        }
示例#4
0
        public void Sort_An_Int_Array()
        {
            // Arrange
            IntArraySorter s = new IntArraySorter();

            int[] expected = new int[] { 1, 1, 2, 2, 2, 4, 5, 9 };
            int[] input    = new int[] { 2, 1, 2, 2, 5, 1, 9, 4 };

            // Act
            int[] actual = s.Sort(input);

            // Assert
            CollectionAssert.AreEqual(expected, actual);
        }
示例#5
0
        public void TestBubbleSort()
        {
            Console.WriteLine("Testing {0} Start...", ObjectHelper.GetMethodName());

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

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

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

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

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