예제 #1
0
        // (nlgn)
        public static MaximumSubarrayResult Find_Maximum_Subarray(int[] array, int low, int high)
        {
            var result = new MaximumSubarrayResult();

            if (low == high)
            {
                result.max_left  = low;
                result.max_right = high;
                result.max_sum   = array[low];
                return(result);
            }
            else
            {
                int mid = Convert.ToInt32(Math.Floor((low + high) / 2.0d));

                var leftResult  = Find_Maximum_Subarray(array, low, mid);
                var rightResult = Find_Maximum_Subarray(array, mid + 1, high);
                var crossResult = Find_Max_Crossing_Subarray(array, low, mid, high);
                if (leftResult.max_sum >= rightResult.max_sum && leftResult.max_sum >= crossResult.max_sum)
                {
                    return(leftResult);
                }
                if (rightResult.max_sum >= leftResult.max_sum && rightResult.max_sum >= crossResult.max_sum)
                {
                    return(rightResult);
                }
                return(crossResult);
            }
        }
예제 #2
0
        //暴力解法
        public static MaximumSubarrayResult Find_Maximum_Subarray_Violent(int[] array, int low, int high)
        {
            var result = new MaximumSubarrayResult();

            result.max_sum = int.MinValue;

            int sum = 0;

            for (int i = low; i <= high; i++)
            {
                for (int j = high; j >= i; j--)
                {
                    sum = 0;
                    for (int k = i; k <= j; k++)
                    {
                        sum += array[k];
                    }
                    if (sum > result.max_sum)
                    {
                        result.max_sum   = sum;
                        result.max_left  = i;
                        result.max_right = j;
                    }
                }
            }

            return(result);
        }
예제 #3
0
        public static MaximumSubarrayResult Find_Max_Crossing_Subarray(int[] array, int low, int mid, int high)
        {
            if (array == null || array.Length == 0 || low > high || low > mid || mid > high)
            {
                return(null);
            }

            var result = new MaximumSubarrayResult();
            int left_sum = int.MinValue, right_sum = int.MinValue;
            int sum = 0;

            for (int i = mid; i >= low; i--)
            {
                sum += array[i];
                if (sum > left_sum)
                {
                    left_sum        = sum;
                    result.max_left = i;
                }
            }

            sum = 0;
            for (int j = mid + 1; j <= high; j++)
            {
                sum += array[j];
                if (sum > right_sum)
                {
                    right_sum        = sum;
                    result.max_right = j;
                }
            }

            result.max_sum = left_sum + right_sum;
            return(result);
        }