예제 #1
0
        private void TestSort(Action <int[]> sortMethod)
        {
            var random = new Random();

            for (int i = 0; i < 10000; i++)
            {
                var array       = CommonArrayFunctions.InitIntArray(i / 100 + 1, 0, random.Next(0, i));
                var sortedArray = new int[array.Length];
                array.CopyTo(sortedArray, 0);

                sortMethod(sortedArray);

                Assert.IsTrue(IsArraysEqual(sortedArray, array.OrderBy(x => x).ToArray()));
            }
        }
예제 #2
0
        public void DutchProblemSortTest()
        {
            var random = new Random();

            for (int i = 0; i < 10000; i++)
            {
                var array       = CommonArrayFunctions.InitIntArray(i / 100 + 1, 0, 2);
                var sortedArray = new int[array.Length];
                array.CopyTo(sortedArray, 0);

                SortArray.DutchProblemSort(sortedArray);

                Assert.IsTrue(IsArraysEqual(sortedArray, array.OrderBy(x => x).ToArray()));
            }
        }
예제 #3
0
        public void QuickSelectionSortTest()
        {
            var random = new Random();

            for (int i = 0; i < 10000; i++)
            {
                var array       = CommonArrayFunctions.InitIntArray(i / 100 + 1, 0, random.Next(0, i));
                var sortedArray = new int[array.Length];
                array.CopyTo(sortedArray, 0);
                var kIndex = random.Next(0, sortedArray.Length - 1);

                var kValue = SortArray.QuickSelectionSort(sortedArray, kIndex, 0, sortedArray.Length - 1);

                Assert.AreEqual(array.OrderBy(x => x).ElementAt(kIndex), kValue);
            }
        }
예제 #4
0
        public void TestBinarySearch()
        {
            var random = new Random();

            for (int t = 0; t < 100000; t++)
            {
                var array       = CommonArrayFunctions.InitIntArray(random.Next(50, 150), 0, random.Next(50, 150));
                var sortedArray = new int[array.Length];
                array.CopyTo(sortedArray, 0);
                SortArray.SelectionSort(sortedArray);

                for (int t2 = 0; t2 < array.Length; t2++)
                {
                    var result = SearchInArray.BinarySearchRecursive(sortedArray, 0, array.Length, array[t2]);

                    Assert.IsTrue(array[t2] == sortedArray[result]);
                }
            }
        }