Ejemplo n.º 1
0
        public static void Show()
        {
            int[] arr = new int[]
            {
                10000, 20000, 30000, 40000, 50000,
                100000, 200000, 500000,
                1000000, 2000000, 5000000, 10000000,
                100000000
            };

            foreach (int i in arr)
            {
                Console.WriteLine("Длина массива - " + i);

                int[] array = IntArrayGenerator.GenerateArray(i);
                Console.WriteLine("быстрая сортировка " + ", время выполнения - " +
                                  ActionTimeMeasurer.Measure(new Action(() =>
                                                                        HoareSorter.Sort(array))));

                array = IntArrayGenerator.GenerateArray(i);
                Console.WriteLine("встроенная сортировка " + ", время выполнения - " +
                                  ActionTimeMeasurer.Measure(new Action(() =>
                                                                        Array.Sort(array))));

                array = IntArrayGenerator.GenerateArray(i);
                Console.WriteLine("сортировка слиянием " + ", время выполнения - " +
                                  ActionTimeMeasurer.Measure(new Action(() =>
                                                                        MergeSorter.Sort(array))));
                GC.Collect();
                Console.WriteLine();
            }

            Console.ReadKey();
        }
Ejemplo n.º 2
0
 public void TestWithAnEmptyCollection()
 {
     List<int> collection = new List<int>();
     MergeSorter<int> sorter = new MergeSorter<int>();
     sorter.Sort(collection);
     Assert.AreEqual(0, collection.Count);
 }
Ejemplo n.º 3
0
        public static void Show()
        {
            int[] arr = new int[]
            {
                100, 500,
                1000, 2000, 5000
            };

            foreach (int i in arr)
            {
                Console.WriteLine("Длина массива - " + i);

                string[] array = TextGenerator.GenerateText(i).Split();
                Console.WriteLine("сортировка пузырьком O(n^2) " + ", время выполнения - " +
                                  ActionTimeMeasurer.Measure(new Action(() =>
                                                                        BubbleSorter.Sort(array))));

                array = TextGenerator.GenerateText(i).Split();
                Console.WriteLine("сортировка вставками O(log(n)) " + ", время выполнения - " +
                                  ActionTimeMeasurer.Measure(new Action(() =>
                                                                        MergeSorter.Sort(array))));

                GC.Collect();
                Console.WriteLine();
            }

            Console.ReadKey();
        }
 public void TestEmptyList()
 {
     var sorter = new MergeSorter();
     var list = new List<int> { };
     sorter.Sort(list);
     Assert.AreEqual(0, list.Count);
 }
Ejemplo n.º 5
0
        public void MergeSortZeroTest()
        {
            IList <int>       arr    = new List <int>();
            MergeSorter <int> sorter = new MergeSorter <int>();

            sorter.Sort(arr);
        }
        public void ThrowInvalidOperationException_WhenCollectionIsEmpty()
        {
            var collection = new List <int>();
            var sorter     = new MergeSorter <int>();

            Assert.Throws <InvalidOperationException>(() => sorter.Sort(collection));
        }
Ejemplo n.º 7
0
        public void When_sorting(int[] input, int[] answer)
        {
            var sorter = new MergeSorter <int>();
            var sorted = sorter.Sort(input);

            Assert.Equal(sorted, answer);
        }
Ejemplo n.º 8
0
        public void MergeSortNullTest()
        {
            IList <int>       arr    = null;
            MergeSorter <int> sorter = new MergeSorter <int>();

            sorter.Sort(arr);
        }
Ejemplo n.º 9
0
        public void Sort_NullArray_ReturnArgumentException()
        {
            int[] array = null;

            var sorter = new MergeSorter <int>();

            sorter.Sort(array);
        }
Ejemplo n.º 10
0
        public void PassingOneItemCollection()
        {
            List<int> numbers = new List<int>() {1};
            MergeSorter<int> sorter = new MergeSorter<int>(numbers);
            List<int> sortedList = new List<int>(sorter.Sort());

            Assert.AreEqual(numbers[0], sortedList[0]);
        }
Ejemplo n.º 11
0
        static void Main(string[] args)
        {
            int[] a = ArrayUtil.RandomIntArray(20, 100);
            Console.WriteLine(ArrayUtil.ToString(a));

            MergeSorter.Sort(a);
            Console.WriteLine(ArrayUtil.ToString(a));
        }
 public void TestProperList()
 {
     var sorter = new MergeSorter();
     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 }));
 }
Ejemplo n.º 13
0
        public void NaiveMergeSorter_SortStringsDescending_SortsCorrectly(IList <string> actual)
        {
            var sorter = new MergeSorter <string>();

            sorter.Sort(actual, SortOrder.Descending);

            SortAssert.IsSortedDescending(actual, Comparer <string> .Default);
        }
Ejemplo n.º 14
0
        public static void Main()
        {
            List<int> numbers = new List<int>() { 1,3,4,5,1,4,6,7,3,3,6,8,9,2 };

            MergeSorter<int> mergeSorter = new MergeSorter<int>(numbers);
            numbers = mergeSorter.Sort();

            Console.WriteLine(string.Join(",", numbers));
        }
Ejemplo n.º 15
0
 public void Sort_hugeArrayTest()
 {
     int[] unsortedArray_1 = GenerateArray();
     int[] unsortedArray_2 = new int[1000];
     unsortedArray_1.CopyTo(unsortedArray_2, 0);
     Array.Sort(unsortedArray_1);
     MergeSorter.Sort(unsortedArray_2);
     CollectionAssert.AreEqual(unsortedArray_1, unsortedArray_2);
 }
Ejemplo n.º 16
0
        public void SortEmptyTest()
        {
            List <int>        testCollection  = new List <int>();
            MergeSorter <int> testMergeSorter = new MergeSorter <int>();

            testMergeSorter.Sort(testCollection);

            Assert.AreEqual(testCollection.Count, 0);
        }
Ejemplo n.º 17
0
        public void Sort_EmptyArray_Success()
        {
            var sorter = new MergeSorter <int>();
            var array  = new int[0];

            sorter.Sort(array);

            Assert.IsTrue(sorter.Ensure(array));
        }
Ejemplo n.º 18
0
        public void Sort_Should_Sort_Input(string input, string sortedInput)
        {
            var value         = input;
            var expectedValue = sortedInput;
            var sorter        = new MergeSorter();

            var result = sorter.Sort(value);

            Assert.AreEqual(expectedValue, result);
        }
Ejemplo n.º 19
0
        private static IList <T> UncheckedMergeSort <T>(
            this IList <T> list, int index, int count, Comparison <T> compare)
        {
            if (count == 0)
            {
                return(list);
            }

            MergeSorter.Sort(list.ToList(), list, index, index + count, compare);
            return(list);
        }
Ejemplo n.º 20
0
        public void PassingSortedItemCollection()
        {
            List<int> sortedNumbers = new List<int>() { 1, 1, 2, 2, 2, 3, 4, 5, 5, 7, 8, 9 };
            MergeSorter<int> sorter = new MergeSorter<int>(sortedNumbers);
            List<int> sortedList = new List<int>(sorter.Sort());

            for (int i = 0; i < sortedNumbers.Count; i++)
            {
                Assert.AreEqual(sortedNumbers[i], sortedList[i]);
            }
        }
Ejemplo n.º 21
0
        public void Sort_SingleElementArray_Success()
        {
            var sorter = new MergeSorter <int>();
            var array  = new int[1] {
                0
            };

            sorter.Sort(array);

            Assert.IsTrue(sorter.Ensure(array));
        }
Ejemplo n.º 22
0
        public void Sort_ValidArray_Success()
        {
            var sorter = new MergeSorter <int>();
            var array  = new int[10] {
                9, 8, 7, 6, 5, 4, 3, 2, 1, 0
            };

            sorter.Sort(array);

            Assert.IsTrue(sorter.Ensure(array));
        }
Ejemplo n.º 23
0
            public void TestWithASingleItem()
            {
                List<int> collection = new List<int>();
                collection.Add(3);

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

                Assert.AreEqual(1, collection.Count);
                Assert.AreEqual(3, collection[0]);
            }
        public void MergeSortTest()
        {
            ISorter sorter = new MergeSorter();

            sorter.Sort(data);

            for (int i = 0; i < result.Count; i++)
            {
                Assert.IsTrue(result[i] == data[i]);
            }
        }
Ejemplo n.º 25
0
        public void SimpleMergeSortTest()
        {
            List<int> arrayToSort = new List<int>() { 2, 5, 3, 1, 4 };
            MergeSorter<int> mergeSorter = new MergeSorter<int>();

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

            mergeSorter.Sort(arrayToSort);

            CollectionAssert.AreEqual(expectedArray, arrayToSort);
        }
Ejemplo n.º 26
0
        public void merge_sort_one_element_input_same_output()
        {
            int[] input = { 4 };

            var sorter = new MergeSorter <int>(input);

            int[] actual   = sorter.Sort();
            int[] expected = { 4 };

            actual.ShouldBeEquivalentTo(expected);
        }
Ejemplo n.º 27
0
        public void merge_sort_two_elements_sorts_correctly()
        {
            int[] input = { 8, 4 };

            var sorter = new MergeSorter <int>(input);

            int[] actual   = sorter.Sort();
            int[] expected = { 4, 8 };

            actual.ShouldBeEquivalentTo(expected);
        }
Ejemplo n.º 28
0
        public void merge_sort_empty_input_empty_output()
        {
            int[] input = { };

            var sorter = new MergeSorter <int>(input);

            int[] actual   = sorter.Sort();
            int[] expected = { };

            actual.ShouldBeEquivalentTo(expected);
        }
Ejemplo n.º 29
0
        public void AlreadySortedNumsTest()
        {
            List<int> arrayToSort = new List<int>() { 1, 2, 3, 4, 5 };
            MergeSorter<int> mergeSorter = new MergeSorter<int>();

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

            mergeSorter.Sort(arrayToSort);

            CollectionAssert.AreEqual(expectedArray, arrayToSort);
        }
Ejemplo n.º 30
0
        public void NaiveMergeSorter_EmptyList_RemainsEmptyList()
        {
            var expected = new List <string>();
            var sorter   = new MergeSorter <string>();

            var actual = new List <string>();

            sorter.Sort(actual);

            Assert.AreEqual(expected, actual);
        }
Ejemplo n.º 31
0
        public void merge_sort_generic_two_elements_sorted_same_output()
        {
            string[] input = { "fa", "la" };

            var sorter = new MergeSorter <string>(input);

            string[] actual   = sorter.Sort();
            string[] expected = { "fa", "la" };

            actual.ShouldBeEquivalentTo(expected);
        }
Ejemplo n.º 32
0
        public void merge_sort_generic_three_element_list()
        {
            string[] input = { "ba", "fa", "la" };

            var sorter = new MergeSorter <string>(input);

            string[] actual   = sorter.Sort();
            string[] expected = { "ba", "fa", "la" };

            actual.ShouldBeEquivalentTo(expected);
        }
Ejemplo n.º 33
0
        public void merge_sort_generic_ten_element_list()
        {
            string[] input = { "do", "re", "mi", "fa", "so", "la", "ti", "do", "boo", "yah" };

            var sorter = new MergeSorter <string>(input);

            string[] actual   = sorter.Sort();
            string[] expected = { "do", "do", "fa", "la", "mi", "re", "so", "ti", "boo", "yah" };

            actual.ShouldBeEquivalentTo(expected);
        }
Ejemplo n.º 34
0
        public void merge_sort_ten_element_list()
        {
            int[] input = { 9, 8, 7, 6, 5, 5, 4, 3, 2, 1 };

            var sorter = new MergeSorter <int>(input);

            int[] actual   = sorter.Sort();
            int[] expected = { 1, 2, 3, 4, 5, 5, 6, 7, 8, 9 };

            actual.ShouldBeEquivalentTo(expected);
        }
Ejemplo n.º 35
0
        public void ShouldSortCharactersCorrectly()
        {
            var array       = new char[] { 'a', 'c', 'b', 'z', 'a' };
            var mergeSorter = new MergeSorter <char>();

            mergeSorter.Sort(array);

            var expected = new char[] { 'a', 'a', 'b', 'c', 'z' };

            CollectionAssert.AreEqual(expected, array);
        }
Ejemplo n.º 36
0
        public void ShouldSortNegativeNumbersCorrectly()
        {
            var array       = new int[] { -103, -166, -42, -3, -1 };
            var mergeSorter = new MergeSorter <int>();

            mergeSorter.Sort(array);

            var expected = new int[] { -166, -103, -42, -3, -1 };

            CollectionAssert.AreEqual(expected, array);
        }
Ejemplo n.º 37
0
        public void ShouldSortNumbersCorrectly()
        {
            var array       = new int[] { 1, 4, 100, -3, 1 };
            var mergeSorter = new MergeSorter <int>();

            mergeSorter.Sort(array);

            var expected = new int[] { -3, 1, 1, 4, 100 };

            CollectionAssert.AreEqual(expected, array);
        }
Ejemplo n.º 38
0
        public void merge_sort_generic_empty_input_empty_output()
        {
            string[] input = { };

            var sorter = new MergeSorter <string>(input);

            string[] actual   = sorter.Sort();
            string[] expected = { };

            actual.ShouldBeEquivalentTo(expected);
        }
Ejemplo n.º 39
0
        public void merge_sort_three_element_list()
        {
            int[] input = { 5, 6, 7 };

            var sorter = new MergeSorter <int>(input);

            int[] actual   = sorter.Sort();
            int[] expected = { 5, 6, 7 };

            actual.ShouldBeEquivalentTo(expected);
        }
Ejemplo n.º 40
0
        public void ReversedNumberSortTest()
        {
            List<int> arrayToSort = new List<int>() { 5, 4, 3, 2, 1 };
            MergeSorter<int> mergeSorter = new MergeSorter<int>();

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

            mergeSorter.Sort(arrayToSort);

            CollectionAssert.AreEqual(expectedArray, arrayToSort);
        }
        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;
            MergeSorter<int> sorter = new MergeSorter<int>();
            sorter.Sort(collection);

            Assert.AreEqual(count, collection.Count);
        }
Ejemplo n.º 42
0
        static void Main(string[] args)
        {
            double[] a = new double[100000];
            for(int i=0; i<100000; i++)
            {
                a[i] = Math.Cos(i);
            }

            MergeSorter<double> ms = new MergeSorter<double>();
            a = ms.Sort(a);
            Console.WriteLine("Done!");
        }
Ejemplo n.º 43
0
        /// <summary>
        /// エントリーポイント
        /// </summary>
        /// <param name="args"></param>
        static void Main(string[] args)
        {
            // データ準備
            int[] array = { 9, 7, 8, 5, 6, 3, 4, 1, 2, 0 };
            DebugPrint(array.ToList());
            Debug.WriteLine("--------");

            // バブルソート生成
            var sorter = new MergeSorter();
            sorter.OnUpdate += (sender, e) => DebugPrint(array.ToList());
            // ソート実行
            sorter.Sort(array);
        }
Ejemplo n.º 44
0
            public void TestWithTwoItems()
            {
                List<int> collection = new List<int>();
                collection.Add(3);
                collection.Add(0);

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

                Assert.AreEqual(2, collection.Count);
                Assert.AreEqual(0, collection[0]);
                Assert.AreEqual(3, collection[1]);
            }
        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 
            };
            MergeSorter<int> sorter = new MergeSorter<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);
        }
Ejemplo n.º 46
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);

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

                Assert.AreEqual(7, collection.Count);
                Assert.IsTrue(SortableCollection<int>.IsSorted(collection));
            }
Ejemplo n.º 47
0
 public void MergeSorterSortInt()
 {
     int[] s = new int[6];
     s[0] = 3;
     s[1] = 2;
     s[2] = 4;
     s[3] = 1;
     s[4] = 0;
     s[5] = 5;
     MergeSorter<int> ms = new MergeSorter<int>();
     int[] sorted_s = ms.Sort(s);
     Assert.AreEqual(sorted_s[0], 0);
     Assert.AreEqual(sorted_s[1], 1);
     Assert.AreEqual(sorted_s[2], 2);
     Assert.AreEqual(sorted_s[3], 3);
     Assert.AreEqual(sorted_s[4], 4);
     Assert.AreEqual(sorted_s[5], 5);
 }
        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 
            };
            MergeSorter<int> sorter = new MergeSorter<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 MergeSorter();
     sorter.Sort<int>(null);
 }
Ejemplo n.º 50
0
 public void TestWithANullCollection()
 {
     List<int> collection = null;
     MergeSorter<int> sorter = new MergeSorter<int>();
     sorter.Sort(collection);
 }
Ejemplo n.º 51
0
        public void MergeSortTestWithOneMemberArray()
        {
            MergeSorter<int> mergesorter = new MergeSorter<int>();
            IList<int> list = new List<int>() { 1, };
            IList<int> sortedList = new List<int>() { 1, };

            mergesorter.Sort(list);
            Assert.AreEqual(string.Join(",", sortedList), string.Join(",", list));
        }
Ejemplo n.º 52
0
        public void MergeSortTestWithFilledArrayOddRecords()
        {
            MergeSorter<int> mergesorter = new MergeSorter<int>();
            IList<int> list = new List<int>() { 9, 3, 2, 1, 4, 6, 5, 0, 8, 7, 10 };
            IList<int> sortedList = new List<int>() { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

            mergesorter.Sort(list);
            Assert.AreEqual(string.Join(",", sortedList), string.Join(",", list));
        }