简体   繁体   English

使用分治法的Maxsub数组

[英]Maxsub array using divide and conquer

I am having trouble implementing max sub array problem using divide and conquer. 我在使用分而治之实现最大子数组问题时遇到了麻烦。

Lets say I have an array [3,6,-1,2] and I want to find the max sum of this array in contiguous order. 可以说我有一个数组[3,6,-1,2],我想按连续顺序找到此数组的最大和。 We can look at this and see that the sum is 10 from [0,3]. 我们可以看一下,发现[0,3]的总和为10。

I tried implementing the pseudo code from my book but the answer is not correct. 我尝试实现本书中的伪代码,但答案不正确。

// return (max-left, max-right, max sum left + right)
public static int[] maxcross(int[] array, int low, int mid, int high) {
    int leftSum = -10000000;
    int rightSum = -10000000;
    int sum = 0;
    int maxLeft=0;
    int maxRight=0;
    for(int i=mid;i<mid-low;i--){
        sum = sum + array[i];
        if(leftSum < sum){
            leftSum = sum;
            maxLeft = i;
        }
    }
    sum=0;
    for(int i=mid+1;i<high;i++){
        sum = sum + array[i];
        if(rightSum < sum){
            rightSum = sum;
            maxRight = i;
        }
    }
    int cross[] = {maxLeft,maxRight,leftSum+rightSum};
    return cross;
}

public static int[] maxsub(int array[], int low, int high){
    int[] maxSubLeft = new int[3];
    int[] maxSubRight = new int[3];
    int[] maxSub = new int[3];
    int[] maxSubCross = new int[3];
    int mid;
    if (high==low){
        maxSub[0] = low;
        maxSub[1] = high;
        maxSub[2] = array[low];
        return maxSub;
    }
    else{
        mid = (int) Math.floor((low+high)/2);
        maxSubLeft = maxsub(array,low,mid);
        maxSubRight = maxsub(array,mid+1,high);
        maxSubCross = maxcross(array,low,mid,high);

        if(maxSubLeft[2] >= maxSubRight[2] && maxSubLeft[2] >= maxSubCross[2])
            return maxSubLeft;
        else if(maxSubRight[2] >= maxSubLeft[2] && maxSubRight[2] >= maxSubCross[2])
            return maxSubRight;
        else
            return maxSubCross;
    }
}

I am getting this as the output 我得到这个作为输出

1 1

1 1

6 6

Can someone help me? 有人能帮我吗?

The recursive initial output is wrong in maxsub(...), return 0 when high=low and array[low] < 0; 递归的初始输出在maxsub(...)中是错误的,当high=lowarray[low] <0时return 0 0。

public static int[] maxsub(int array[], int low, int high){
    int[] maxSubLeft = new int[3];
    int[] maxSubRight = new int[3];
    int[] maxSub = new int[3];
    int[] maxSubCross = new int[3];
    int mid;
    if (high==low){
        maxSub[0] = low;
        maxSub[1] = high;
        maxSub[2] = array[low];
        return maxSub; // if (array[low] < 0) return 0;
    }
    else{
        mid = (int) Math.floor((low+high)/2);
        maxSubLeft = maxsub(array,low,mid);
        maxSubRight = maxsub(array,mid+1,high);
        maxSubCross = maxcross(array,low,mid,high);

        if(maxSubLeft[2] >= maxSubRight[2] && maxSubLeft[2] >= maxSubCross[2])
            return maxSubLeft;
        else if(maxSubRight[2] >= maxSubLeft[2] && maxSubRight[2] >= maxSubCross[2])
            return maxSubRight;
        else
            return maxSubCross;
    }
}

By the way, your recursive algorithm is O(NlnN), a more effective and easy to implement algorithm is O(N), which applies the dynamic programming. 顺便说一句,您的递归算法是O(NlnN),一种更有效,更易于实现的算法是O(N),它应用了动态编程。

public static int maxSum(int array[], int low, int high) {
   int maxsum = 0, maxleftsum = 0;
   for (int i = low; i < high; i++) {
       maxsum = max(maxsum, array[i] + maxleftSum);
       maxleftSum = max(0, maxleftSum+array[i]);
   }
   return maxsum; // return the index if necessary. 
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM