Exemplo n.º 1
0
        static void Main(string[] args)
        {
            List <long> arr = new List <long> {
                13, -3, -25, 20, -3, -16, -23, 18, 20, -7, 12, -5, -22, 15, -4, 7
            };
            //List<long> arr = new List<long> { 13, 3, 25, 20, 3, 16, 23, 18, 20, 7, 12, 5, 22, 15, 4, 7 };
            //List<long> arr = new List<long> { -13, -3, -25, -20, -3, -16, -23, -18, -20, -7, -12, -5, -22, -15, -4, -7 };

            var maxSubarrayDnC = MaximumSubarray.maximumSubarrayDivideNConquer(arr);

            Console.WriteLine("Divide-n-Conquer\nLow index = {0}\nHigh index = {1}\nSum = {2}\n",
                              maxSubarrayDnC.lowMaxIdx, maxSubarrayDnC.highMaxIdx, maxSubarrayDnC.totalSum
                              );

            var maxSubarrayBF = MaximumSubarray.maximumSubarrayBruteForce(arr);

            Console.WriteLine("Brute-force\nLow index = {0}\nHigh index = {1}\nSum = {2}\n",
                              maxSubarrayBF.lowMaxIdx, maxSubarrayBF.highMaxIdx, maxSubarrayBF.totalSum
                              );

            var maxSubarrayHybrid = MaximumSubarray.maximumSubarrayHybrid(arr);

            Console.WriteLine("Hybrid\nLow index = {0}\nHigh index = {1}\nSum = {2}\n",
                              maxSubarrayHybrid.lowMaxIdx, maxSubarrayHybrid.highMaxIdx, maxSubarrayHybrid.totalSum
                              );

            var maxSubarrayDynamic = MaximumSubarray.maximumSubarrayDynamic(arr);

            Console.WriteLine("Dynamic programming (Kadane's algorithm)\nLow index = {0}\nHigh index = {1}\nSum = {2}\n",
                              maxSubarrayDynamic.lowMaxIdx, maxSubarrayDynamic.highMaxIdx, maxSubarrayDynamic.totalSum
                              );

            Console.ReadKey();
        }
        maximumSubarrayDivideNConquer
            (IList <long> inArr)
        {
            //List<long> inArr = new List<long> { 13, -3, -25, 20, -3, -16, -23, 18, 20, -7, 12, -5, -22, 15, -4, 7 };

            return(MaximumSubarray.maximumSubarrayDivideNConquer(inArr));
        }
        maximumSubarrayHybrid
            (IList <long> inArr)
        {
            //List<long> inArr = new List<long> { 13, -3, -25, 20, -3, -16, -23, 18, 20, -7, 12, -5, -22, 15, -4, 7 };

            return(MaximumSubarray.maximumSubarrayHybrid(inArr));
        }
Exemplo n.º 4
0
        public void MaximumSubArray()
        {
            int[] nums    = { -2, 1, -3, 4, -1, 2, 1, -5, 4 };
            int   maximum = MaximumSubarray.MaxSubArray(nums);

            Assert.AreEqual(6, maximum);
        }
Exemplo n.º 5
0
        public void GetMax_WhenCalled_ReturnMaxSubarraySum(int[] nums, int expected)
        {
            var helper = new MaximumSubarray();

            var result = helper.GetMax(nums);

            Assert.That(result, Is.EqualTo(expected));
        }
Exemplo n.º 6
0
        public void MaximumSubarray()
        {
            MaximumSubarray MaximumSubarray = new MaximumSubarray();

            int[] array  = new[] { -2, 1, -3, 4, -1, 2, 1, -5, 4 };
            var   answer = MaximumSubarray.MaxSubArray(array);

            Assert.AreEqual(answer, 6);
        }
        public void MaxSubArrayTest()
        {
            foreach (MaximumSubarrayTestData testData in TestDataList)
            {
                Console.WriteLine("Test iutput: " + string.Join(",", testData.InputArray));

                Assert.AreEqual(testData.OutputInt, MaximumSubarray.MaxSubArray(testData.InputArray));
            }
        }
Exemplo n.º 8
0
        public void GetMaximumSubarray_ShouldReturn_MaximumSubarrayInArray(
            int[] numbers, int expectedSumOfMaximumSubarray)
        {
            // Arrange
            var maximumSubArray = new MaximumSubarray();

            // Act
            var sumOfMaximumSubarray = maximumSubArray.GetMaximumSubarray(numbers);

            // Assert
            sumOfMaximumSubarray.Should().Be(expectedSumOfMaximumSubarray);
        }
Exemplo n.º 9
0
        public void MaximumSubarrayTest()
        {
            for (int i = 0; i < 10; i++)
            {
                for (int j = 0; j < 10; j++)
                {
                    int[] data = Utilities.ArrayUtilities.CreateRandomArray(j, -20, 20);

                    int expected = MaximumSubarrayTests.CalculateMaximumSubarray(data);
                    int actual   = MaximumSubarray.Run(data);

                    Assert.AreEqual(expected, actual);
                }
            }
        }
Exemplo n.º 10
0
 public void BeforeEach()
 {
     maximumSubarray = new MaximumSubarray();
 }
Exemplo n.º 11
0
        public void Given_array_When_find_Then_return()
        {
            var nums = new int[] { -2, 1, -3, 4, -1, 2, 1, -5, 4 };

            Assert.AreEqual(6, MaximumSubarray.MaxSubArray(nums));
        }
Exemplo n.º 12
0
        public void TestMaximumSubarray()
        {
            var r = MaximumSubarray.GetMs(new [] { -2, 1, -3, 4, -1, 2, 1, -5, 4 });

            Assert.AreEqual(r, 6);
        }
Exemplo n.º 13
0
 static void Main(string[] args)
 {
     int[] A = { 13, -3, -25, 20, -3, -16, -23, 18, 20, -7, 12, -5, -22, 15, -4, 7 };
     (int low, int high, int maxSum) = MaximumSubarray.findMaximumSubarray(A, 0, A.Length - 1);
     System.Console.WriteLine("low={0},high={1},maxSum={2}", low, high, maxSum);
 }
Exemplo n.º 14
0
        public void MaxSubarrayKadanesTest()
        {
            var maxSubarrayValue = MaximumSubarray.MaxSubArrayKadanes(new int[] { -1, 2, -1, 3, 1, -2 });

            maxSubarrayValue.Should().Be(5);
        }
Exemplo n.º 15
0
        public void MaxSubArrayTest(int[] nums, int expected)
        {
            var actual = new MaximumSubarray().MaxSubArray(nums);

            Assert.Equal(expected, actual);
        }
Exemplo n.º 16
0
        public void TestSolution(int[] input1, int expectedResult)
        {
            var result = new MaximumSubarray().Resolve(input1);

            Assert.AreEqual(expectedResult, result);
        }
Exemplo n.º 17
0
        public void MaxSubarrayTest()
        {
            var maxSubarrayValue = MaximumSubarray.MaxSubArray(new int[] { -1, 2, -1, 3, -2 });

            maxSubarrayValue.Should().Be(4);
        }