public void Insertion_Sorter_Should_Sort_Correctly()
        {
            var sorter     = new SelectionSorter <int>((a, b) => a.CompareTo(b));
            var input      = new int[] { 5, 2, 4, 6, 1, 3 };
            var emptyInput = new int[] { };

            sorter.Sort(input);
            sorter.Sort(emptyInput);
            Assert.Equal(new int[] { 1, 2, 3, 4, 5, 6 }, input);
            Assert.Equal(new int[] { }, emptyInput);
        }
        public void Insertion_Sort_Should_Correctly_Sort_In_Decreasing_Order()
        {
            var sorter     = new SelectionSorter <int>((a, b) => b.CompareTo(a));
            var input      = new int[] { 5, 2, 4, 6, 1, 3 };
            var emptyInput = new int[] { };

            sorter.Sort(input);
            sorter.Sort(emptyInput);
            Assert.Equal(new int[] { 6, 5, 4, 3, 2, 1 }, input);
            Assert.Equal(new int[] { }, emptyInput);
        }
 public void TestWithAnEmptyCollection()
 {
     List<int> collection = new List<int>();
     SelectionSorter<int> sorter = new SelectionSorter<int>();
     sorter.Sort(collection);
     Assert.AreEqual(0, collection.Count);
 }
Example #4
0
        static void Main(string[] args)
        {
            insertionsorter sorter1 = new insertionsorter();
            BubbleSorter    sorter2 = new BubbleSorter();
            SelectionSorter sorter3 = new SelectionSorter();
            int             icount  = 1000;
            Random          random  = new Random();

            sorter1.list = new int[icount];
            sorter2.list = new int[icount];
            sorter3.list = new int[icount];
            for (int i = 0; i < icount; ++i)
            {
                sorter1.list[i] = sorter2.list[i] = sorter3.list[i] = random.Next(); //随机数组
            }
            Stopwatch stwatch = new Stopwatch();                                     //计时器设置

            stwatch.Start();                                                         //计时器开始
            sorter1.Sort();                                                          //插入排序开始
            sorter2.Sort();                                                          ////冒泡排序开始
            sorter3.Sort();                                                          //选择排序开始
            stwatch.Stop();                                                          //计时器停止
            Console.WriteLine(stwatch.Elapsed.TotalMilliseconds);                    //输出时间
            Console.ReadKey();                                                       //按任意键继续
        }
Example #5
0
        public void SelectionSortNullTest()
        {
            IList <int>           arr    = null;
            SelectionSorter <int> sorter = new SelectionSorter <int>();

            sorter.Sort(arr);
        }
Example #6
0
        public void SelectionSortZeroTest()
        {
            IList <int>           arr    = new List <int>();
            SelectionSorter <int> sorter = new SelectionSorter <int>();

            sorter.Sort(arr);
        }
 public void TestEmptyList()
 {
     var sorter = new SelectionSorter();
     var list = new List<int> { };
     sorter.Sort(list);
     Assert.AreEqual(0, list.Count);
 }
 public void TestProperList()
 {
     var sorter = new SelectionSorter();
     var list = new List<int> { 3, 2, 1, 9, 7 };
     sorter.Sort(list);
     Assert.AreEqual(5, list.Count);
     Assert.IsTrue(list.SequenceEqual(new List<int> { 1, 2, 3, 7, 9 }));
 }
Example #9
0
        public void SelectionSorter_SortStringsDescending_SortsCorrectly(IList <string> actual)
        {
            var sorter = new SelectionSorter <string>();

            sorter.Sort(actual, SortOrder.Descending);

            SortAssert.IsSortedDescending(actual, Comparer <string> .Default);
        }
        static void Main(string[] args)
        {
            Coin[] arr = ArrayUtil.CreateCoinArr(10);
            Console.WriteLine(ArrayUtil.ToString(arr));

            SelectionSorter.Sort(arr);
            Console.WriteLine(ArrayUtil.ToString(arr));
        }
        public void PassingOneItemCollection()
        {
            List<int> numbers = new List<int>() {1};
            SelectionSorter<int> selectionSorter = new SelectionSorter<int>(numbers);
            List<int> sortedList = new List<int>(selectionSorter.Sort());

            Assert.AreEqual(numbers[0], sortedList[0]);
        }
Example #12
0
        static void Main(string[] args)
        {
            int[] a = ArrayUtil.CreateRandomArray(20, 100);
            Console.WriteLine(ArrayUtil.ToString(a));

            SelectionSorter.Sort(a);
            Console.WriteLine(ArrayUtil.ToString(a));
        }
Example #13
0
        public static void Main()
        {
            List<int> numbers = new List<int>(){ 1,3,4,6,7,3,2,2,3,5,7,9,9,1 };

            SelectionSorter<int> selectionSorter = new SelectionSorter<int>(numbers);
            numbers = selectionSorter.Sort();

            Console.WriteLine(string.Join(",", numbers));
        }
        public void SelectionSortTestWithOneMemberArray()
        {
            SelectionSorter<int> selectionSorter = new SelectionSorter<int>();
            IList<int> list = new List<int>() { 1, };
            IList<int> sortedList = new List<int>() { 1, };

            selectionSorter.Sort(list);
            Assert.AreEqual(string.Join(",", sortedList), string.Join(",", list));
        }
        public void SortEmptyTest()
        {
            List <int>            testCollection      = new List <int>();
            SelectionSorter <int> testSelectionSorter = new SelectionSorter <int>();

            testSelectionSorter.Sort(testCollection);

            Assert.AreEqual(testCollection.Count, 0);
        }
        public void SelectionSortTestWithFilledArray()
        {
            SelectionSorter<int> selectionSorter = new SelectionSorter<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 };

            selectionSorter.Sort(list);
            Assert.AreEqual(string.Join(",",sortedList), string.Join(",",list));
        }
        public void AlreadySortedNumsTest()
        {
            List<int> quickSorter = new List<int>() { 1, 2, 3, 4, 5 };
            SelectionSorter<int> selectionSorter = new SelectionSorter<int>();

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

            selectionSorter.Sort(quickSorter);

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

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

            selectionSorter.Sort(arrayToSort);

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

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

            quickSorter.Sort(arrayToSort);

            CollectionAssert.AreEqual(selectionSorter, arrayToSort);
        }
Example #20
0
        public void SelectionSorter_EmptyList_RemainsEmptyList()
        {
            var expected = new List <string>();
            var sorter   = new SelectionSorter <string>();

            var actual = new List <string>();

            sorter.Sort(actual);

            Assert.AreEqual(expected, actual);
        }
        public void SelectionSortTest()
        {
            ISorter sorter = new SelectionSorter();

            sorter.Sort(data);

            for (int i = 0; i < result.Count; i++)
            {
                Assert.IsTrue(result[i] == data[i]);
            }
        }
        public void PassingSortedItemCollection()
        {
            List<int> sortedNumbers = new List<int>() { 1, 1, 2, 2, 2, 3, 4, 5, 5, 7, 8, 9 };
            SelectionSorter<int> sorter = new SelectionSorter<int>(sortedNumbers);
            List<int> sortedList = new List<int>(sorter.Sort());

            for (int i = 0; i < sortedNumbers.Count; i++)
            {
                Assert.AreEqual(sortedNumbers[i], sortedList[i]);
            }
        }
        public void TestWithOneItemCollection()
        {
            List<int> collection = new List<int>();
            collection.Add(3);

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

            Assert.AreEqual(1, collection.Count);
            Assert.AreEqual(3, collection[0]);
        }
Example #24
0
        public static void Main()
        {
            var initialNumbers = NumbersGenerator.GenerateNumberArray(50);

            NumbersGenerator.ShuffleNumbers(initialNumbers);

            Console.WriteLine("Unsorted numbers\n" + string.Join(" ", initialNumbers) + Environment.NewLine);

            SelectionSorter.Sort(initialNumbers);

            Console.WriteLine("Sorted numbers\n" + string.Join(" ", initialNumbers));
        }
        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;
            SelectionSorter<int> sorter = new SelectionSorter<int>();
            sorter.Sort(collection);

            Assert.AreEqual(count, collection.Count);
        }
Example #26
0
        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;
            SelectionSorter <int> sorter = new SelectionSorter <int>();

            sorter.Sort(collection);

            Assert.AreEqual(count, collection.Count);
        }
Example #27
0
        public void SelectionSorter_Sort_PassNullCollection_ThrowArgumentNullException()
        {
            //
            // Arrange.
            //
            double[] array           = null;
            var      selectionSorter = new SelectionSorter <double>();

            //
            // Assert.
            //
            Assert.ThrowsException <ArgumentNullException>(() => selectionSorter.Sort(array));
        }
Example #28
0
        private static BinaryTreeNode ConvertToBalancedTree(int[] heap)
        {
            var sorter = new SelectionSorter <int>((i, j) => i < j);

            sorter.Sort(heap);
            var list = heap.ToList();

            list.RemoveAll(i => i == 0);

            var array = list.ToArray();

            return(ConvertToBalancedTree(array, 0, array.GetLength(0) - 1));
        }
        public void SortSingleTest()
        {
            List <int> testCollection = new List <int>()
            {
                1
            };
            SelectionSorter <int> testSelectionSorter = new SelectionSorter <int>();

            testSelectionSorter.Sort(testCollection);

            Assert.AreEqual(testCollection.Count, 1);
            Assert.AreEqual(testCollection[0], 1);
        }
        public void SortExample()
        {
            var sorter = new SelectionSorter<int>();

            var list = new List<int> {13, 5, 77, 9, 12};

            sorter.Sort(list, Comparer<int>.Default);

            Assert.AreEqual(5, list[0]);
            Assert.AreEqual(9, list[1]);
            Assert.AreEqual(12, list[2]);
            Assert.AreEqual(13, list[3]);
            Assert.AreEqual(77, list[4]);
        }
Example #31
0
        public void Sort_DoesNotChange_OriginalInput()
        {
            //arrange
            const int firstElement = 5, lastElement = 3;
            var       original = new List <int> {
                firstElement, 2, 4, 6, 1, lastElement
            };
            var sorter = new SelectionSorter <int>();
            //act
            var sorted = sorter.Sort(original);

            //assert
            original[0].Should().Be(firstElement);
            original[5].Should().Be(lastElement);
        }
Example #32
0
        public void ArraySorted([Random(0, 1000, 100, Distinct = true)] int n)
        {
            // Arrange
            var sorter      = new SelectionSorter <int>();
            var intComparer = new IntComparer();

            var(correctArray, testArray) = RandomHelper.GetArrays(n);

            // Act
            sorter.Sort(testArray, intComparer);
            Array.Sort(correctArray, intComparer);

            // Assert
            Assert.AreEqual(testArray, correctArray);
        }
Example #33
0
        public void SelectionSortSortedTest()
        {
            var arr = new List <int>()
            {
                0, 11, 22, 33, 101, 101
            };
            SelectionSorter <int> sorter = new SelectionSorter <int>();

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

            CollectionAssert.AreEqual(arr, expected);
        }
        public void SortExample()
        {
            var sorter = new SelectionSorter <int>();

            var list = new List <int> {
                13, 5, 77, 9, 12
            };

            sorter.Sort(list, Comparer <int> .Default);

            Assert.AreEqual(5, list[0]);
            Assert.AreEqual(9, list[1]);
            Assert.AreEqual(12, list[2]);
            Assert.AreEqual(13, list[3]);
            Assert.AreEqual(77, list[4]);
        }
Example #35
0
        public static void Main(string[] args)
        {
            Console.Out.WriteLine("Running Bubble Sort");
            var sorter = new BubbleSorter();

            sorter.Sort(new int[] { 4, 16, 28, 34, 23, 24, 16, 17, 4, 33, 9, 90, 87, 34, 26, 27, 28, 17, 2, 5, 6, 62 });
            Console.Out.WriteLine();

            Console.Out.WriteLine("Running Selection Sort");
            var sorter2 = new SelectionSorter();

            sorter2.Sort(new int[] { 4, 16, 28, 34, 23, 24, 16, 17, 4, 33, 9, 90, 87, 34, 26, 27, 28, 17, 2, 5, 6, 62 });
            Console.Out.WriteLine();

            Console.Out.WriteLine("Complete, press any key.");
            Console.ReadKey();
        }
        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 
            };
            SelectionSorter<int> sorter = new SelectionSorter<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);
        }
Example #37
0
        public void SortMethodShould_SortElementsProperly_IfTheyAreOfTypeDecimal()
        {
            // Arrange
            IList <decimal> decimalElements = new List <decimal> {
                10.2m, 8.46m, 12m, -14m, 1m, -18m, 6m
            };
            IList <decimal> sortedDecimalElements = new List <decimal> {
                -18m, -14m, 1m, 6m, 8.46m, 10.2m, 12m
            };

            SelectionSorter <decimal> sorter = new SelectionSorter <decimal>();

            // Act
            IList <decimal> actualSortedElements = sorter.Sort(decimalElements);

            // Assert
            Assert.AreEqual(sortedDecimalElements, actualSortedElements);
        }
Example #38
0
        public void Sort_Returns_AscOrderedCollection()
        {
            //arrange
            var original = new List <int> {
                5, 2, 4, 6, 1, 3
            };
            var sorter = new SelectionSorter <int>();
            //act
            var sorted = sorter.Sort(original).ToList();
            //assert
            var prev = sorted[0];

            for (var i = 1; i < sorted.Count; i++)
            {
                prev.Should().BeLessOrEqualTo(sorted[i]);
                prev = sorted[i];
            }
        }
Example #39
0
        public void SortMethodShould_SortElementsProperly_IfTheyAreOfTypeString()
        {
            // Arrange
            IList <string> stringElements = new List <string> {
                "gg", "ga", "p", "v", "c"
            };
            IList <string> sortedStringElements = new List <string> {
                "c", "ga", "gg", "p", "v"
            };

            SelectionSorter <string> sorter = new SelectionSorter <string>();

            // Act
            IList <string> actualSortedElements = sorter.Sort(stringElements);

            // Assert
            Assert.AreEqual(sortedStringElements, actualSortedElements);
        }
Example #40
0
        public void SortMethodShould_SortElementsProperly_IfTheyAreOfTypeFloat()
        {
            // Arrange
            IList <float> floatElements = new List <float> {
                10.2f, -14f, 8.46f, 12f, 1f, -18f, 6f
            };
            IList <float> sortedFloatElements = new List <float> {
                -18f, -14f, 1f, 6f, 8.46f, 10.2f, 12
            };

            SelectionSorter <float> sorter = new SelectionSorter <float>();

            // Act
            IList <float> actualSortedElements = sorter.Sort(floatElements);

            // Assert
            Assert.AreEqual(sortedFloatElements, actualSortedElements);
        }
Example #41
0
        public void SortMethodShould_SortElementsProperly_IfTheyAreOfTypeChar()
        {
            // Arrange
            IList <char> charElements = new List <char> {
                'b', 'a', 'c', 'f', 'e'
            };
            IList <char> sortedCharElements = new List <char> {
                'a', 'b', 'c', 'e', 'f'
            };

            SelectionSorter <char> sorter = new SelectionSorter <char>();

            // Act
            IList <char> actualSortedElements = sorter.Sort(charElements);

            // Assert
            Assert.AreEqual(sortedCharElements, actualSortedElements);
        }
Example #42
0
        public void SortMethodShould_SortElementsProperly_IfTheyAreOfTypeInteger()
        {
            // Arrange
            IList <int> integerElements = new List <int> {
                10, 2, 8, 4, -6, 12, -20, 14, -18
            };
            IList <int> sortedIntegerElements = new List <int> {
                -20, -18, -6, 2, 4, 8, 10, 12, 14
            };

            SelectionSorter <int> sorter = new SelectionSorter <int>();

            // Act
            IList <int> actualSortedElements = sorter.Sort(integerElements);

            // Assert
            Assert.AreEqual(sortedIntegerElements, actualSortedElements);
        }
Example #43
0
        public void SortMethodShould_SortElementsProperly_IfTheyAreOfTypeDouble()
        {
            // Arrange
            IList <double> doubleElements = new List <double> {
                10.2d, 8.46d, 12d, -14d, 1d, -18d, 6d
            };
            IList <double> sortedDoubleElements = new List <double> {
                -18d, -14d, 1d, 6d, 8.46d, 10.2d, 12d
            };

            SelectionSorter <double> sorter = new SelectionSorter <double>();

            // Act
            IList <double> actualSortedElements = sorter.Sort(doubleElements);

            // Assert
            Assert.AreEqual(sortedDoubleElements, actualSortedElements);
        }
        public void SortStringsTest()
        {
            List <string> testCollection = new List <string>()
            {
                "Z", "a", "A", "z", "abv", "abc"
            };
            SelectionSorter <string> testSelectionSorter = new SelectionSorter <string>();

            testSelectionSorter.Sort(testCollection);

            Assert.AreEqual(testCollection.Count, 6);

            string[] expectedSortedResult = new[] { "a", "A", "abc", "abv", "z", "Z" };
            for (int i = 0; i < testCollection.Count; i++)
            {
                Assert.AreEqual(testCollection[i], expectedSortedResult[i]);
            }
        }
        public void SortReverseSortedTest()
        {
            List <int> testCollection = new List <int>()
            {
                int.MaxValue, 7, 1, 0, -1, int.MinValue
            };
            SelectionSorter <int> testSelectionSorter = new SelectionSorter <int>();

            testSelectionSorter.Sort(testCollection);

            Assert.AreEqual(testCollection.Count, 6);

            int[] expectedSortedResult = new[] { int.MinValue, -1, 0, 1, 7, int.MaxValue };
            for (int i = 0; i < testCollection.Count; i++)
            {
                Assert.AreEqual(testCollection[i], expectedSortedResult[i]);
            }
        }
        public void TestWithMultipleItems()
        {
            List<int> collection = new List<int>();
            collection.Add(3);
            collection.Add(-111);
            collection.Add(0);
            collection.Add(24);
            collection.Add(-14);
            collection.Add(-1);
            collection.Add(32);
            collection.Add(-1);
            collection.Add(7);

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

            Assert.AreEqual(9, collection.Count);
            Assert.IsTrue(SortableCollection<int>.IsSorted(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 
            };
            SelectionSorter<int> sorter = new SelectionSorter<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 TestNullList()
 {
     var sorter = new SelectionSorter();
     sorter.Sort<int>(null);
 }
 public void TestWithNullCollection()
 {
     List<int> collection = null;
     SelectionSorter<int> sorter = new SelectionSorter<int>();
     sorter.Sort(collection);
 }