The fourth chapter divide and conquer strategy--the problem of maximal sub-array

Source: Internet
Author: User

Maximum sub-array problem

Method One: The method of solving violence

We can easily devise a brute force method to solve this problem: simply try not to have a possible sub-array, a total O (N2) species

#include <iostream>using namespace std; #define Int_min 0x80000000int Main () {    INT arr[10]={9,8,-3,-5,7,- 39,79,-37,8,9};    int i,j;    int sum=0,maxsum=int_min;    int IMAX;    for (i=0;i<10;i++)    {        sum=0;         for (j=i;j<10;j++)         {            sum+=arr[j];            if (maxsum<sum)            {                maxsum=sum;                Imax=j    ;    }}} cout<< "Maxsum:" <<maxsum<< "IMAX:" <<IMAX<<ENDL;}

Method Two: The solution method using the divide-and-conquer strategy (O (NLGN))

Let's consider how to solve the problem of maximal subarray with divide-and-conquer technique. Suppose we are looking for the largest subarray of sub-array A[low,high]. The use of divide-and-conquer technology means that we want to divide the Subarray into two sub-arrays of the same scale as possible. That is, find the central location of the sub-array, such as mid, and then consider solving two sub-arrays A[low,mid] and A[mid+1,high]. Any contiguous subarray of A[low,high] A[i: J] Location must be one of the following 3 situations:

    • Exactly in the Subarray A[low: Mid], so Low<=i<=j<=mid
    • is completely in the Subarray A[mid+1..high], so mid<i<=j<=high;
    • Across the midpoint, so low<=i<=mid<j<=high;

Therefore, A[low. The position of one of the largest sub-arrays of high] must be one of these three cases. In fact, one of the largest sub-arrays of A[low,high] must be completely located in A[low. Mid], which is completely in a[mid+1..high] or across all sub-arrays and the largest of the midpoint. We can solve the maximal sub-arrays of a[low,mid] and A[mid+1,high] recursively, because these two sub-problems are still the problem of the largest subarray, but smaller. Therefore, all that is left is to look for the largest subarray across the midpoint, and then select and Max in three cases.

The algorithm is as follows:

#include <iostream> #include <tuple>using namespace std; #define Int_min 0x80000000tuple<int,int,int    > findmaxcrossingsubarry (int arr[],int low,int mid,int high) {int leftsum=int_min,rightsum=int_min;    int sum=0;    int maxleft=low,maxright=mid+1;    int i;        for (i=mid;i!=low;i--) {sum+=arr[i];            if (leftsum<sum) {leftsum=sum;        maxleft=i;    }} sum=0;        for (i=mid+1;i!=high;i++) {sum+=arr[i];            if (rightsum<sum) {rightsum=sum;        maxright=i; }} cout<< "Maxleft:" <<maxleft<< "Maxright:" <<maxright<< "Leftsum+rightsum:" &LT;&L    t;leftsum+rightsum<<endl; Return tuple<int,int,int> (maxleft,maxright,leftsum+rightsum);} tuple<int,int,int> findmaximumsubarry (int arr[],int low,int high) {if (Low==high) return tuple<int,int,i    Nt> (Low,high,arr[low]);        else {int mid= (Low+high)/2; TuPle<int,int,int> Left=findmaximumsubarry (Arr,low,mid);        Tuple<int,int,int> Right=findmaximumsubarry (Arr,mid+1,high);        Tuple<int,int,int> Middle=findmaxcrossingsubarry (Arr,low,mid,high); if (get<2> (left) >=get<2> [right] &&get<2> (left) >=get<2> (middle)) return to left        ; else if (get<2> (right) >=get<2> [left] &&get<2> (right) >=get<2> (middle)) retur        n Right;    else return middle;    }}int Main () {int arr[10]={9,8,-3,-5,7,-39,79,-37,8,9};    Tuple<int,int,int> Result=findmaximumsubarry (arr,0,9); Cout<<get<0> (Result) << "" <<get<1> (Result) << "" <<get<2> (Result) <<endl;}

Method Three: Using the dynamic Programming algorithm (O (n))

#include <iostream>using namespace Std;int maxarraysum (int arr[],int n) {    int sum,maxsum,maxi;    int i;    maxsum=0;    maxi=0;    sum=0;    for (i=0;i<n;i++)    {        sum+=arr[i];        if (sum<0)        {            sum=0;            Continue;        }        if (maxsum<sum)        {            maxsum=sum;            maxi=i;        }    }    cout<< "Max sum:" <<maxSum<< "index:" <<maxi<<endl;    return maxsum;} int main () {    int arr[10]={9,8,-3,-5,7,-39,79,-37,8,9};    Cout<<maxarraysum (arr,10);}

The fourth chapter divide and conquer strategy--the problem of maximal sub-array

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.