Beispiel #1
0
        protected override MaxSubArray Search(int[] values, int start, int end)
        {
            var max = new MaxSubArray(0, 0, 0);

            for (var i = 0; i < values.Length; i++)
            {
                var currentSum = 0;
                var currentMax = 0;
                var currentMaxStart = i;
                for (var j = i; j >= 0; j--)
                {
                    currentSum += values[j];
                    if (currentSum <= 0) break; // An opportunity to break out early as a negative/zero value indicates that any gains in this sub-array have already been wiped out
                    if (currentSum > currentMax)
                    {
                        currentMax = currentSum;
                        currentMaxStart = j;
                    }
                }

                if (currentMax > max.Sum)
                {
                    max.Sum = currentMax;
                    max.Start = currentMaxStart;
                    max.End = i + 1;
                }
            }

            return max;
        }
    private static MaxSubArray FindMaxSubarray(int[] a, int low, int high)
    {
        var res = new MaxSubArray {
            Low  = low,
            High = high,
            Sum  = a[low]
        };

        if (low == high)
        {
            return(res);
        }
        var mid              = (low + high) / 2;
        var leftSubarray     = FindMaxSubarray(a, low, mid);
        var rightSubarray    = FindMaxSubarray(a, mid + 1, high);
        var crossingSubarray = FindMaxCrossingSubarray(a, low, mid, high);

        if (leftSubarray.Sum >= rightSubarray.Sum &&
            leftSubarray.Sum >= crossingSubarray.Sum)
        {
            return(leftSubarray);
        }
        if (rightSubarray.Sum >= leftSubarray.Sum &&
            rightSubarray.Sum >= crossingSubarray.Sum)
        {
            return(rightSubarray);
        }
        return(crossingSubarray);
    }
        public void Test()
        {
            int[] items = { 13, -3, -25, 20, -3, -16, -23, 18, 20, -7, 12, -5, -22, 15, -4, 7 };
            //int[] items = { -13, -3, -25, -20, -3, -16, -23, -18, -20, -7, -12, -5, -22, -15, -4, -7 };

            var result2 = new MaxSubArray3().FindMaximumSubArray(items, 0, items.Length - 1);
            var result  = new MaxSubArray().FindMaximumSubArray(items, 0, items.Length - 1);
        }
Beispiel #4
0
        public void MaxSubArrayTest1()
        {
            int[]       array    = new int[] { -2, 1, -3, 4, -1, 2, 1, -5, 4 };
            MaxSubArray ms       = new MaxSubArray();
            int         actual   = ms.Max(array);
            int         expected = 6;

            Assert.AreEqual(actual, expected);
        }
Beispiel #5
0
        static void TestMaxSubArraySoln()
        {
            Console.WriteLine(" ***** MaxSubArray Solutions Test ***** ");

            int[] sampleAAA         = { 13, -3, -25, 20, -3, -16, -23, 18, 20, -7, 12, -5, -22, 15, -4, 7 };
            CollectionToString CtoS = new CollectionToString();
            MaxSubArray        MSA  = new MaxSubArray();

            Console.WriteLine("Brute sum result: {0}", MSA.BruteForceMax(sampleAAA));
            Console.WriteLine("Brute start array result: {0}", MSA.BruteForceStartPos(sampleAAA));
        }
Beispiel #6
0
        public void Excution1Test()
        {
            var func = new MaxSubArray();

            Assert.AreEqual(6, func.Excution1(new[] { -2, 1, -3, 4, -1, 2, 1, -5, 4 }));

            Assert.AreEqual(0, func.Excution1(new int[] { }));

            Assert.AreEqual(-2, func.Excution1(new int[] { -2 }));

            Assert.AreEqual(-1, func.Excution1(new int[] { -2, -1, -3 }));

            Assert.AreEqual(5, func.Excution1(new int[] { 5, -2, -1, -3, 1 }));
        }
        public void GetTest()
        {
            var source = new[] { -2, -3, 4, -1, -2, 1, 5, -3 };

            var target = new MaxSubArray();

            var results = target.Get(source);

            Assert.AreEqual(7, results.Key);
            Assert.AreEqual(2, results.Value[0]);
            Assert.AreEqual(6, results.Value[1]);


            source  = new[] { -1, -2, -3, 4, 5, -10, -1 };
            results = target.Get(source);

            Assert.AreEqual(9, results.Key);
            Assert.AreEqual(3, results.Value[0]);
            Assert.AreEqual(4, results.Value[1]);
        }
Beispiel #8
0
        private static void callMaxSubArray()
        {
            MaxSubArray prb = new MaxSubArray();

            Console.WriteLine(prb.GetMaxSubArrayDC(new int[] { -2, 3, 0, 2, -2, 3 }));
        }
 void BaseTest(Type type, MaxSubArray expected, params int[] values)
 {
     var searcher = Activator.CreateInstance(type) as ISearcher;
     var r = searcher.Search(values);
     Assert.AreEqual(expected.Start, r.Start, "Start index differs");
     Assert.AreEqual(expected.End, r.End, "End index differs");
     Assert.AreEqual(expected.Sum, r.Sum, "Sum value differs");
 }