public void TestPivotAsFirstElement()
        {
            var       test      = RandomNumberGenerator.GenerateRandomIntArray(100000);
            Stopwatch stopwatch = Stopwatch.StartNew();
            var       sorter    = new QuickSortImpl(new PartitionerStart());

            sorter.QuickSort(test, 0, test.Length - 1);
            stopwatch.Stop();
            Console.WriteLine("elapsedTime: " + stopwatch.ElapsedMilliseconds);
        }
        public void Sort_IntCollection_Success()
        {
            var unsortedCollection = new Collection <int>()
            {
                3, 8, 2, 5, 1, 4, 7, 6
            };

            IPivotPositionFinder <int> pivotPositionFinder = new GetFirstIndexAsPivotImpl <int>();
            var sorter = new QuickSortImpl <int>(pivotPositionFinder);

            var sortedResultActual = sorter.Sort(unsortedCollection);

            CollectionAssert.AreEqual(unsortedCollection.OrderBy(item => item).ToList(), sortedResultActual.ToList());
        }
Beispiel #3
0
        public static IList <T> QuickSort <T>(this IList <T> source) where T : IComparable <T>
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            var sequence = new T[source.Count];

            source.CopyTo(sequence, 0);

            QuickSortImpl <T> .SortAroundPivot(sequence, 0, sequence.Length);

            return(sequence);
        }
        public void Sort_LargeDataSet_Pass()
        {
            //Arrange
            var fileLocation = @"..\..\TestData\QuickSort.txt";

            if (!File.Exists(fileLocation))
            {
                Assert.Fail("File not found");
            }
            var lines = File.ReadAllLines(fileLocation);
            var unsortedCollection = lines.Select(line => Convert.ToInt32(line)).ToList();
            IPivotPositionFinder <int> pivotPositionFinder = new GetFirstIndexAsPivotImpl <int>();
            var sorter = new QuickSortImpl <int>(pivotPositionFinder);
            //Act
            var sortedResultActual = sorter.Sort(unsortedCollection);

            //Assert
            //Assert.AreEqual(expectedComparison,sorter.ComparisonCount);
            CollectionAssert.AreEqual(unsortedCollection.OrderBy(item => item).ToList(), sortedResultActual.ToList());
        }
        public void Sort_10ItemDataSet_ComparisonCountsMatches()
        {
            //Arrange
            var fileLocation = @"..\..\TestData\input_dgrcode_06_10.txt";

            if (!File.Exists(fileLocation))
            {
                Assert.Fail("File not found");
            }
            var lines = File.ReadAllLines(fileLocation);
            var unsortedCollection = lines.Select(line => Convert.ToInt32(line)).ToList();
            IPivotPositionFinder <int> pivotPositionFinder = new GetMedianOfThreeGetRandomIndexAsPivotImpl <int>();
            var sorter             = new QuickSortImpl <int>(pivotPositionFinder);
            var expectedComparison = 21;
            //Act
            var sortedResultActual = sorter.Sort(unsortedCollection);

            //Assert
            Assert.AreEqual(expectedComparison, sorter.ComparisonCount);
        }
 public QuickSortImplTest()
 {
     sut = new QuickSortImpl();
 }