Solving the problem of maximal sub-array by divide and conquer

Source: Internet
Author: User

Solving the problem of maximal sub-array by divide-and-conquer method (to get the maximum and sub-arrays in the array for the given array)

/ * For a given integer array A, the number of arrays with the largest and the largest and the left and right subscript * Ideas: The method of dividing the array into two parts, there are three cases of the largest and sub-arrays * on the left of the array, at the right of the array, across the midpoint of the array */#include <iostream>using namespace Std;//structure for storing left and right boundary values and sum values/ * Pay special attention to the use of structures!!!!!!!!!!! */struct sumborder{intLeft//Left Border subscript    intRight//Right Border subscript    int sum;array elements between the//left and right boundaries};//Find across mid-midpoint, in array A[low: High] and the maximum number of sub-arraysSumborder *findmaxcrosssubarray (int*ptra,intLowintMidintHigh) {/* Get the max and * * on the left of mid    intLeftsum = Ptra[mid];//Left Max and    intMaxleft = mid;//Left Border    int sum=0; for(inti = mid; I >= low; i--) {sum+ = Ptra[i];if(sum>= leftsum) {leftsum =sum;        Maxleft = i; }    }/ * Get mid+1 and Right max and */    intRightsum = Ptra[mid +1];intMaxright = mid +1;sum=0; for(intj = Mid +1; J <= High; J + +) {sum+ = Ptra[j];if(sum>= rightsum) {rightsum =sum;        Maxright = j; }    }/ * Statistics Summary * /Sumborder Sumborder;    Sumborder *ptr = &sumBorder;    Ptr->left = Maxleft;    Ptr->right = Maxright; Ptr->sum= Leftsum + rightsum; cout << Ptr->left <<"-"<< Ptr->right <<"sum:"<< ptr->sum<< Endl;returnPTR;}/* Get the maximum subarray of array a (from low to High) and/*Sumborder *findmaxsubarray (int*ptra,intLowintHigh) {Sumborder sumborder; Sumborder *sumborderptr = &sumBorder;if(High = = Low)        {sumborder.left = low;        Sumborder.right = high; Sumborder.sum= Ptra[low]; }Else{intMid = (low + high)/2;//Get structure pointer variables in three cases and make further judgmentsSumborder *left = Findmaxsubarray (PtrA, Low, mid); Sumborder *right = Findmaxsubarray (PtrA, Mid +1, high); Sumborder *cross = Findmaxcrosssubarray (PtrA, Low, Mid, high);if(left->sum>= right->sum&& left->sum>= cross->sum) {Sumborder.left = left->left;            Sumborder.right = left->right; Sumborder.sum= left->sum; }Else if(right->sum>= left->sum&& right->sum>= cross->sum) {Sumborder.left = right->left;            Sumborder.right = right->right; Sumborder.sum= right->sum; }Else{Sumborder.left = cross->left;            Sumborder.right = cross->right; Sumborder.sum= cross->sum; }} cout << sumborderptr->left <<"-"<< Sumborderptr->right <<"Maximum value is:"<< sumborderptr->sum<< Endl;returnSumborderptr;}intMain () {//The given array    intA[] = {0, -, -3, - -, -, -3, - -, - at, -, -, -7, A, -5, - A, the, -4, -7};    Sumborder Sumborder;    Sumborder *ptr = &sumBorder; Sumborder *test = Findmaxsubarray (A,1, -);//Eat a big loss! Ptr->left = test->left;    Ptr->right = test->right; Ptr->sum= test->sum;/ * Replace the above with this is wrong, carefully study the struct structure body!!    Sumborder *ptr = Findmaxsubarray (A, 1, 16);    cout << "Left:" << ptr->left << Endl;    cout << "right:" << ptr->right << Endl;    cout << "sum:" << ptr->sum << Endl; */cout <<"The largest subarray of array A and is:"<< Endl; cout <<"Left:"<< ptr->left << Endl; cout <<"Right:"<< ptr->right << Endl; cout <<"sum:"<< ptr->sum<< Endl; System"Pause >> cout");return 0;}

Solving the problem of maximal sub-array by divide and conquer

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.