Exemplo n.º 1
0
        public void BubbleSortTest(int[] nums)
        {
            // var nums = new int[] { 5, 3, 8, 2, 1, 4 };
            // var nums = new int[] { 3, -9, -12, -1, 8 };
            var acutal = Sorts.BubbleSort(nums);

            Array.Sort(nums);
            var expected = nums;

            Assert.AreEqual(acutal, expected);
        }
Exemplo n.º 2
0
        public void BubbleSort()
        {
            for (int x = 0; x < 100; x++)
            {
                var nums = Randomize();
                Sorts <int> .BubbleSort(nums);

                for (int i = 0; i < nums.Length - 1; i++)
                {
                    Assert.True(nums[i] <= nums[i + 1]);
                }
            }
        }
Exemplo n.º 3
0
        public void BubbleSortTest()
        {
            int[] items = new int[] { 5, 8, 1, 14, 82, 95, 12 };

            Sorts.BubbleSort(items);

            int[] correctItems = new int[] { 1, 5, 8, 12, 14, 82, 95 };


            Assert.AreEqual(items.Length, correctItems.Length);

            for (int i = 0; i < items.Length; i++)
            {
                Assert.AreEqual(items[i], correctItems[i]);
            }
        }
Exemplo n.º 4
0
        public void BubbleSortTest()
        {
            int[] expectedResult = new int[5];
            for (int i = 0; i < expectedResult.Length; i++)
            {
                expectedResult[i] = i + 1;
            }
            int[] numbers = new int[5];
            numbers[0] = 3;
            numbers[1] = 2;
            numbers[2] = 5;
            numbers[3] = 1;
            numbers[4] = 4;

            Sorts.BubbleSort(numbers);
            Assert.Equal(expectedResult, numbers);
        }
Exemplo n.º 5
0
        public void SortByDescendingOrderOfMaxElementsTests(int[] source1, int[] source2, int[] source3, int[] source4, int[] source5)
        {
            int[][] actual = new int[5][];
            actual[0] = source1;
            actual[1] = source2;
            actual[2] = source3;
            actual[3] = source4;
            actual[4] = source5;

            int[][] expected = new int[5][];
            expected[0] = source2;
            expected[1] = source4;
            expected[2] = source5;
            expected[3] = source1;
            expected[4] = source3;

            Sorts.BubbleSort(actual, new TestComparers.MaxArrayRowDecComparer());

            CollectionAssert.AreEqual(expected, actual);
        }
Exemplo n.º 6
0
        public void SortByAscendingOrderOfMinElementsTestsUsingDelegate(int[] source1, int[] source2, int[] source3, int[] source4, int[] source5)
        {
            int[][] actual = new int[5][];
            actual[0] = source1;
            actual[1] = source2;
            actual[2] = source3;
            actual[3] = source4;
            actual[4] = source5;

            int[][] expected = new int[5][];
            expected[0] = source5;
            expected[1] = source4;
            expected[2] = source3;
            expected[3] = source1;
            expected[4] = source2;

            Sorts.BubbleSort(actual, new TestComparers.MinArrayRowIncComparer().Compare);

            CollectionAssert.AreEqual(expected, actual);
        }
        public void SortByMaxIncreasing(int[] array1, int[] array2, int[] array3, int[] array4)
        {
            int[][] actualArray = new int[7][];
            actualArray[0] = array1;
            actualArray[1] = array2;
            actualArray[2] = null;
            actualArray[3] = null;
            actualArray[4] = null;
            actualArray[5] = array3;
            actualArray[6] = array4;

            int[][] expectedArray = new int[7][];
            expectedArray[0] = array3;
            expectedArray[1] = array2;
            expectedArray[2] = array1;
            expectedArray[3] = array4;
            expectedArray[4] = null;
            expectedArray[5] = null;
            expectedArray[6] = null;
            Sorts.BubbleSort(actualArray, new SortIncreasingMaxElements());
            CollectionAssert.AreEqual(expectedArray, actualArray);
        }
Exemplo n.º 8
0
 public void SortsArrayNullExceptionTests()
 => Assert.Throws <ArgumentNullException>(() => Sorts.BubbleSort(null, new TestComparers.MinArrayRowIncComparer()));
Exemplo n.º 9
0
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");

            // Sort Algorithms
            var sortedArray = Sorts <int> .BubbleSort(new[] { 5, 4, 3, 2, 1 });

            var sortedArray2 = Sorts <int> .SelectionSort(new[] { 5, 4, 3, 2, 1 });

            var sortedArray3 = Sorts <int> .InsertionSort(new[] { 5, 4, 3, 2, 1 });

            var kthLargest = Sorts <int> .FindKthLargest(new[] { 3, 2, 1, 5, 6, 4 }, 2);

            // Recursions
            var triangularNumbers = Recursions.TriangularNumbers(7);
            var factoril          = Recursions.Factorial(4);

            // Binary Search Tree
            var tree = new TreeNode(1)
            {
                Left = new TreeNode(2)
                {
                    Left = new TreeNode(3)
                    {
                        Left  = new TreeNode(5),
                        Right = new TreeNode(6)
                    },
                    Right = new TreeNode(4)
                    {
                        Left  = new TreeNode(6),
                        Right = new TreeNode(5)
                    }
                },
                Right = new TreeNode(2)
                {
                    Left = new TreeNode(4)
                    {
                        Left  = new TreeNode(5),
                        Right = new TreeNode(6)
                    },
                    Right = new TreeNode(3)
                    {
                        Left  = new TreeNode(6),
                        Right = new TreeNode(5)
                    }
                }
            };
            var bst = new TreeNode(5)
            {
                Left  = new TreeNode(4),
                Right = new TreeNode(6)
            };
            var isBst         = Trees.ValidateBst(tree);
            var isBst2        = Trees.ValidateBstRecursive(tree);
            var isSymetricRec = Trees.IsSymmetricRecursive(tree);
            var isSymetric    = Trees.IsSymmetric(tree);
            var closestVal    = Trees.ClosestValue(bst, 4.5);
            var sumRoutes     = Trees.SumNumbers(tree);

            var test = Trees.SumNumbers(bst);

            // Linked Lists
            ListNode node1 = new ListNode(8)
            {
                Next = new ListNode(7)
                {
                    Next = new ListNode(9)
                }
            };
            ListNode node2 = new ListNode(5)
            {
                Next = new ListNode(8)
                {
                    Next = new ListNode(4)
                }
            };

            var res  = LinkedLists.AddTwoNumbers(node1, node2);
            var res2 = LinkedLists.AddTwoNumbers2(node1, node2);

            var res3 = LinkedLists.GetIntersectionNode(node1, node2);

            var res4 = LinkedLists.MergeKLists(new ListNode[] { node1 });

            // Arrays

            var arrRes = Arrays.TwoSum(new[] { 3, 2, 4 }, 6);
        }