Exemplo n.º 1
1
 public void TestWithAnEmptyCollection()
 {
     List<int> collection = new List<int>();
     Quicksorter<int> sorter = new Quicksorter<int>();
     sorter.Sort(collection);
     Assert.AreEqual(0, collection.Count);
 }
Exemplo n.º 2
0
        public void QuickSortNullTest()
        {
            IList <int>       arr    = null;
            Quicksorter <int> sorter = new Quicksorter <int>();

            sorter.Sort(arr);
        }
Exemplo n.º 3
0
        public void QuickSortZeroTest()
        {
            IList <int>       arr    = new List <int>();
            Quicksorter <int> sorter = new Quicksorter <int>();

            sorter.Sort(arr);
        }
        public void TestQuickSortWithOneElement()
        {
            ISorter<int> sorter = new Quicksorter<int>();
            collection.Items = new List<int>() { 1 };
            collection.Sort(sorter);

            Assert.AreEqual(collection.Items[0], 1);
        }
        public void QuickSortTestWithOneMemberArray()
        {
            Quicksorter<int> quicksorter = new Quicksorter<int>();
            IList<int> list = new List<int>() { 1, };
            IList<int> sortedList = new List<int>() { 1, };

            quicksorter.Sort(list);
            Assert.AreEqual(string.Join(",", sortedList), string.Join(",", list));
        }
        public void QuickSortTestWithFilledArray()
        {
            Quicksorter<int> quicksorter = new Quicksorter<int>();
            IList<int> list = new List<int>() { 9, 3, 2, 1, 4, 6, 5, 0, 8, 7 };
            IList<int> sortedList = new List<int>() { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };

            quicksorter.Sort(list);
            Assert.AreEqual(string.Join(",", sortedList), string.Join(",", list));
        }
        public void TestQuickSortShouldReturnCorrectElements()
        {
            ISorter<int> sorter = new Quicksorter<int>();
            collection.Sort(sorter);

            Assert.AreEqual(collection.Items[0], 0);
            Assert.AreEqual(collection.Items[2], 22);
            Assert.AreEqual(collection.Items[collection.Items.Count - 1], 101);
        }
        public void TestWithEmptyCollectionShouldReturnMissingElement()
        {
            var collection = new SortableCollection<int>();
            var sorter = new Quicksorter<int>();
            collection.Sort(sorter);
            var result = collection.InterpolationSearch(collection.ToArray(), 0);
            var expected = Array.BinarySearch(collection.ToArray(), 0);

            Assert.AreEqual(expected, result, "No elements are present in an empty collection; method should return -1.");
        }
        public void AlreadySortedNumsTest()
        {
            List<int> quickSorter = new List<int>() { 1, 2, 3, 4, 5 };
            Quicksorter<int> mergeSorter = new Quicksorter<int>();

            List<int> expectedArray = new List<int>() { 1, 2, 3, 4, 5 };

            mergeSorter.Sort(quickSorter);

            CollectionAssert.AreEqual(expectedArray, quickSorter);
        }
        public void ReversedNumberSortTest()
        {
            List<int> arrayToSort = new List<int>() { 5, 4, 3, 2, 1 };
            Quicksorter<int> quickSorter = new Quicksorter<int>();

            List<int> expectedArray = new List<int>() { 1, 2, 3, 4, 5 };

            quickSorter.Sort(arrayToSort);

            CollectionAssert.AreEqual(expectedArray, arrayToSort);
        }
        public void SimpleQuickSortTest()
        {
            List<int> arrayToSort = new List<int>() { 2, 5, 3, 1, 4 };
            Quicksorter<int> quickSorter = new Quicksorter<int>();

            List<int> expectedArray = new List<int>() { 1, 2, 3, 4, 5 };

            quickSorter.Sort(arrayToSort);

            CollectionAssert.AreEqual(expectedArray, arrayToSort);
        }
 private void PopulateAndSort(Quicksorter <int> quicksorter)
 {
     quicksorter.Add(4);
     quicksorter.Add(5);
     quicksorter.Add(8);
     quicksorter.Add(6);
     quicksorter.Add(3);
     quicksorter.Add(7);
     quicksorter.Add(9);
     quicksorter.Sort();
 }
Exemplo n.º 13
0
        public void TestWithASingleItem()
        {
            List<int> collection = new List<int>();
            collection.Add(3);

            Quicksorter<int> sorter = new Quicksorter<int>();
            sorter.Sort(collection);

            Assert.AreEqual(1, collection.Count);
            Assert.AreEqual(3, collection[0]);
        }
        public void TestWithItemToTheRightOfMidpoint()
        {
            var collection = new SortableCollection<int>(1, 2, 3, 4, 5);

            var sorter = new Quicksorter<int>();
            collection.Sort(sorter);
            var result = collection.InterpolationSearch(collection.ToArray(), 4);
            var expected = Array.BinarySearch(collection.ToArray(), 4);

            Assert.AreEqual(expected, result);
        }
        public void TestWithMissingElement()
        {
            var collection = new SortableCollection<int>(-1, 1, 5, 12, 50);

            var sorter = new Quicksorter<int>();
            collection.Sort(sorter);
            var result = collection.InterpolationSearch(collection.ToArray(), 0);
            var expected = -1;

            Assert.AreEqual(expected, result, "Missing element should return -1.");
        }
Exemplo n.º 16
0
        public void SortShould_NotThrowIfTheCollectionIsEmpty_AndReturnEmptyCollection()
        {
            // Arange
            var sorter     = new Quicksorter <string>();
            var collection = new List <string>(0);

            // Act
            sorter.Sort(collection);

            // Assert
            Assert.AreEqual("", string.Join(", ", collection));
        }
        public void TestSortLengthOfCollection()
        {
            List<int> collection = new List<int>()
            {
                3, 5, 21, 543, 2, 12, 3, 65, 34, 234, 23, 12, 3, 43, 12, 32, 341, 24, 23 
            };
            int count = collection.Count;
            Quicksorter<int> sorter = new Quicksorter<int>();
            sorter.Sort(collection);

            Assert.AreEqual(count, collection.Count);
        }
        internal static void Main(string[] args)
        {
            var collection = new SortableCollection<int>(new[] { 22, -2, 300, 11, 55, 33, 10, -15, 88, 101, 33, 0, 101, 44, 33 });
            Console.WriteLine("All items before sorting:");
            collection.PrintAllItemsOnConsole();
            Console.WriteLine();

            Console.WriteLine("SelectionSorter result:");
            collection.Sort(new SelectionSorter<int>());
            collection.PrintAllItemsOnConsole();
            Console.WriteLine();

            collection = new SortableCollection<int>(new[] { 22, -2, 300, 11, 55, 33, 10, -15, 88, 101, 33, 0, 101, 44, 33 });
            Console.WriteLine("Quicksorter result:");
            var quickSorter = new Quicksorter<int>();
            collection.Sort(quickSorter);
            quickSorter.PrintResults();
            //collection.PrintAllItemsOnConsole();
            Console.WriteLine();

            collection = new SortableCollection<int>(new[] { 22, -2, 300, 11, 55, 33, 10, -15, 88, 101, 33, 0, 101, 44, 33 });
            Console.WriteLine("MergeSorter result:");
            var mergeSort = new MergeSorter<int>();
            collection.Sort(mergeSort);
            mergeSort.ShowResults();
            //collection.PrintAllItemsOnConsole();
            Console.WriteLine();

            Console.WriteLine("Linear search 101:");
            Console.WriteLine(collection.LinearSearch(101));
            Console.WriteLine();

            Console.WriteLine("Binary search 101:");
            Console.WriteLine(collection.BinarySearch(101));
            Console.WriteLine();

            collection = new SortableCollection<int>(new int[20]);
            for (int i = 1; i <= 20; i++)
            {
                collection.Items[i - 1] = i;
            }
            Console.WriteLine("All items before sorting:");
            collection.PrintAllItemsOnConsole();
            Console.WriteLine("Shuffle:");
            collection.Shuffle();
            collection.PrintAllItemsOnConsole();
            Console.WriteLine();

            Console.WriteLine("Shuffle again:");
            collection.Shuffle();
            collection.PrintAllItemsOnConsole();
        }
        public void TestSortLengthOfCollection()
        {
            List <int> collection = new List <int>()
            {
                3, 5, 21, 543, 2, 12, 3, 65, 34, 234, 23, 12, 3, 43, 12, 32, 341, 24, 23
            };
            int count = collection.Count;
            Quicksorter <int> sorter = new Quicksorter <int>();

            sorter.Sort(collection);

            Assert.AreEqual(count, collection.Count);
        }
Exemplo n.º 20
0
        public void TestWithTwoItems()
        {
            List<int> collection = new List<int>();
            collection.Add(3);
            collection.Add(0);

            Quicksorter<int> sorter = new Quicksorter<int>();
            sorter.Sort(collection);

            Assert.AreEqual(2, collection.Count);
            Assert.AreEqual(0, collection[0]);
            Assert.AreEqual(3, collection[1]);
        }
Exemplo n.º 21
0
    public static void Main(string[] args)
    {
        Random R = new Random(42);

        for (int i = 10000000;; i += 3)
        {
            int[] A = new int[i];
            int[] B = new int[i];
            for (int j = 0; j < i; ++j)
            {
                int v = R.Next();
                A[j] = (v);
                B[j] = (v);
            }

            Stopwatch sw = new Stopwatch();
            sw.Start();


            Quicksorter <int> .init();

            Quicksorter <int> .qs(A, 0, A.Length,
                                  (int x, int y) => { return(x - y); }
                                  );

            lock (Quicksorter <int> .lck){
                while (Quicksorter <int> .done == false)
                {
                    Monitor.Wait(Quicksorter <int> .lck);
                }
            }

            sw.Stop();
            Console.WriteLine(sw.Elapsed);

            Array.Sort(B, 0, B.Length);
            for (int j = 0; j < A.Length; ++j)
            {
                if (A[j] != B[j])
                {
                    throw new Exception("Mismatch");
                }
            }
            if (i > 0)
            {
                break;
            }
        }
        Console.WriteLine("OK");
    }
Exemplo n.º 22
0
        public void QuickSortSortedTest()
        {
            var arr = new List <int>()
            {
                0, 11, 22, 33, 101, 101
            };
            Quicksorter <int> sorter = new Quicksorter <int>();

            sorter.Sort(arr);
            var expected = new List <int>()
            {
                0, 11, 22, 33, 101, 101
            };

            CollectionAssert.AreEqual(arr, expected);
        }
Exemplo n.º 23
0
        public void SortShould_SortThePassedCollectionOfIntegers()
        {
            // Arange
            var sorter     = new Quicksorter <int>();
            var collection = new List <int>()
            {
                2, 1, 4, 5, 6, 3
            };
            var result = "1, 2, 3, 4, 5, 6";

            // Act
            sorter.Sort(collection);

            // Assert
            Assert.AreEqual(result, string.Join(", ", collection));
        }
Exemplo n.º 24
0
        public void SortShould_SortThePassedCollectionOfStrings()
        {
            // Arange
            var sorter     = new Quicksorter <string>();
            var collection = new List <string>()
            {
                "2", "1", "4", "5", "6", "3"
            };
            var result = "1, 2, 3, 4, 5, 6";

            // Act
            sorter.Sort(collection);

            // Assert
            Assert.AreEqual(result, string.Join(", ", collection));
        }
Exemplo n.º 25
0
        public void QuickSort_ShouldSortGivenCollectionProperly_WithStrings()
        {
            var sorter           = new Quicksorter <string>();
            var testedCollection = new SortableCollection <string>(new[] { "Ivan", "Petyr", "Gosho", "Pencho", "Dragan" });

            testedCollection.Sort(sorter);
            var result = new StringBuilder();

            for (int i = 0; i < testedCollection.Items.Count; i++)
            {
                result.Append(testedCollection.Items[i]);
                result.Append(" ");
            }
            var expectedResult = "Dragan Gosho Ivan Pencho Petyr ";

            Assert.AreEqual(expectedResult, result.ToString());
        }
        public void TestSortIsSortedWithListSort()
        {
            List<int> collection = new List<int>()
            {
                3, 5, 21, 543, 2, 12, 3, 65, 34, 234, 23, 12, 3, 43, 12, 32, 341, 24, 23 
            };
            Quicksorter<int> sorter = new Quicksorter<int>();
            sorter.Sort(collection);

            List<int> collection2 = new List<int>()
            {
                3, 5, 21, 543, 2, 12, 3, 65, 34, 234, 23, 12, 3, 43, 12, 32, 341, 24, 23 
            };
            collection2.Sort();

            CollectionAssert.AreEqual(collection2, collection);
        }
Exemplo n.º 27
0
        public void TestWithOddNumberOfItems()
        {
            List<int> collection = new List<int>();
            collection.Add(3);
            collection.Add(0);
            collection.Add(11);
            collection.Add(-3);
            collection.Add(5);
            collection.Add(0);
            collection.Add(4);

            Quicksorter<int> sorter = new Quicksorter<int>();
            sorter.Sort(collection);

            Assert.AreEqual(7, collection.Count);
            Assert.IsTrue(SortableCollection<int>.IsSorted(collection));
        }
Exemplo n.º 28
0
        public void QuickSort_ShouldSortGivenCollectionProperly_WithNumbers()
        {
            var sorter           = new Quicksorter <int>();
            var testedCollection = new SortableCollection <int>(new[] { 22, 11, 101, 33, 0, 101 });

            testedCollection.Sort(sorter);

            var result = new StringBuilder();

            for (int i = 0; i < testedCollection.Items.Count; i++)
            {
                result.Append(testedCollection.Items[i]);
                result.Append(" ");
            }
            var expectedResult = "0 11 22 33 101 101 ";

            Assert.AreEqual(expectedResult, result.ToString());
        }
        public void TestSortIsSortedWithListSort()
        {
            List <int> collection = new List <int>()
            {
                3, 5, 21, 543, 2, 12, 3, 65, 34, 234, 23, 12, 3, 43, 12, 32, 341, 24, 23
            };
            Quicksorter <int> sorter = new Quicksorter <int>();

            sorter.Sort(collection);

            List <int> collection2 = new List <int>()
            {
                3, 5, 21, 543, 2, 12, 3, 65, 34, 234, 23, 12, 3, 43, 12, 32, 341, 24, 23
            };

            collection2.Sort();

            CollectionAssert.AreEqual(collection2, collection);
        }
        public void TestSortIsSortedWithCheck()
        {
            List<int> collection = new List<int>()
            {
                3, 5, 21, 543, 2, 12, 3, 65, 34, 234, 23, 12, 3, 43, 12, 32, 341, 24, 23 
            };
            Quicksorter<int> sorter = new Quicksorter<int>();
            sorter.Sort(collection);

            bool isSorted = true;
            for (int i = 0; i < collection.Count - 1; i++)
            {
                if (collection[i] > collection[i + 1])
                {
                    isSorted = false;
                }
            }

            Assert.IsTrue(isSorted);
        }
        public void TestSortIsSortedWithCheck()
        {
            List <int> collection = new List <int>()
            {
                3, 5, 21, 543, 2, 12, 3, 65, 34, 234, 23, 12, 3, 43, 12, 32, 341, 24, 23
            };
            Quicksorter <int> sorter = new Quicksorter <int>();

            sorter.Sort(collection);

            bool isSorted = true;

            for (int i = 0; i < collection.Count - 1; i++)
            {
                if (collection[i] > collection[i + 1])
                {
                    isSorted = false;
                }
            }

            Assert.IsTrue(isSorted);
        }
 public void TestStringQuicksorter_RandomNumberOfItems()
 {
     ISorter<string> sorter = new Quicksorter<string>();
     TestsHelper.TestStringSorter(sorter);
 }
        public void TestWithMultipleMissingKeysSmallerThanMinimum()
        {
            const int NumberOfChecks = 10000;
            const int NumberOfElements = 1000;

            var elements = new int[NumberOfElements];

            for (int i = 0; i < NumberOfElements; i++)
            {
                elements[i] = Random.Next(int.MinValue / 2, int.MaxValue / 2);
            }

            Array.Sort(elements);
            
            var collection = new SortableCollection<int>(elements);
            var sorter = new Quicksorter<int>();
            collection.Sort(sorter);
            var sortedArray = collection.ToArray();

            for (int i = 0; i < NumberOfChecks; i++)
            {
                var item = Random.Next(int.MinValue, collection.Items[0]);

                var result = collection.InterpolationSearch(sortedArray, item);

                Assert.AreEqual(-1, result);
            }
        }
Exemplo n.º 34
0
        public void TestQuicksorterWhenCollectionIsNull_ThrowsException()
        {
            ISorter <int> sorter = new Quicksorter <int>();

            TestsHelper.TestInt32SorterWhenCollectionIsNull(sorter);
        }
Exemplo n.º 35
0
 public void TestWithANullCollection()
 {
     List<int> collection = null;
     Quicksorter<int> sorter = new Quicksorter<int>();
     sorter.Sort(collection);
 }
Exemplo n.º 36
0
        public void QuicksortTest()
        {
            IQuicksorter quicksorter = new Quicksorter();

            RunTests(quicksorter);
        }
 public static void InitilizeQuickSorter(TestContext context)
 {
     sorter = new Quicksorter<int>();
 }
Exemplo n.º 38
0
        public void TestInt32Quicksorter_RandomNumberOfItems()
        {
            ISorter <int> sorter = new Quicksorter <int>();

            TestsHelper.TestInt32Sorter(sorter);
        }
Exemplo n.º 39
0
        public void TestStringQuicksorter_RandomNumberOfItems()
        {
            ISorter <string> sorter = new Quicksorter <string>();

            TestsHelper.TestStringSorter(sorter);
        }
 public void TestQuicksorterWhenCollectionIsNull_ThrowsException()
 {
     ISorter<int> sorter = new Quicksorter<int>();
     TestsHelper.TestInt32SorterWhenCollectionIsNull(sorter);
 }
Exemplo n.º 41
0
        public override IEnumerable <TElement> Sort(IEnumerable <TElement> collection, IComparer <TElement> comparer, bool parallel)
        {
            var quicksorter = new Quicksorter(collection, comparer, parallel);

            return(quicksorter.Sort());
        }
 public void TestInt32Quicksorter_RandomNumberOfItems()
 {
     ISorter<int> sorter = new Quicksorter<int>();
     TestsHelper.TestInt32Sorter(sorter);
 }