Algorithm learning--Divide and conquer algorithm

Source: Internet
Author: User

The first algorithm to implement the introduction of the algorithm is the following one:

#include <iostream>
#include <fstream>
#include <string>
using namespace Std;
struct number{
int low;
int high;
int sum;
};
Number Find_max_crossing_subarray (int num[25],int low,int Mid,int high) {
Number Cross;
int leftsum=0,rightsum=0,maxright,maxleft;
int sum=0;
for (int i=mid;i>low;i--) {
Sum=sum+num[i];
if (sum>leftsum) {
Leftsum=sum;
maxleft=i;
}
}
sum=0;
for (int i=mid+1;iSum=sum+num[i];
if (sum>rightsum) {
Rightsum=sum;
maxright=i;
}
}
Cross.low=maxleft;
Cross.high=maxright;
Cross.sum=leftsum+rightsum;
return cross;
}
Number Find_maxmum_subarray (int num[25],int low,int high) {
Number Endo;
Number Left,right,cross;
int mid;
if (High==low) {
Endo.low=low;
Endo.high=high;
Endo.sum=num[low];
Return Endo;
}
else if (high==low+1) {
if (num[low]<0&&num[high]>0) {
Endo.low=high;
Endo.high=high;
Endo.sum=num[high];
}
else if (num[low]>0&&num[high]<0) {
Endo.low=low;
Endo.high=low;
Endo.sum=num[low];
}
else if (num[low]>0&&num[high]>0) {
Endo.low=low;
Endo.high=high;
Endo.sum=num[low];
}
else if (Num[low]<0&&num[high]<0&&num[low]<num[high]) {
Endo.low=high;
Endo.high=high;
Endo.sum=num[high];
}
else {
Endo.low=low;
Endo.high=low;
Endo.sum=num[low];
}
Return Endo;
}
else {mid= (Low+high)/2;}
Left=find_maxmum_subarray (Num,low,mid);
Right=find_maxmum_subarray (Num,mid,high);
Cross=find_max_crossing_subarray (Num,low,mid,high);
if (left.sum>=right.sum&&left.sum>=cross.sum) {
Endo=left;
}
else if (right.sum>=left.sum&&right.sum>=cross.sum) {
Endo=right;
}
else {Endo=cross;}
Return Endo;
cout<<endo.high<<endl<<endo.low<<endl<<endo.sum;
}
int main ()
{
Number CROSS,JK;
Ifstream infile;
Ofstream outfile;
int num[25],i=0;
Infile.open ("Number.txt");
while (Infile>>num[i]) {
i++;
}
Infile.close ();
Cross=find_max_crossing_subarray (num,0, (i-1)/2,i-1);
Jk=find_maxmum_subarray (num,0,i-1);
cout<<cross.high<<endl<<cross.low<<endl<<cross.sum;
cout<<jk.high<<endl<<jk.low<<endl<<jk.sum;
return 0;
}//Summary, this algorithm skillfully use recursion, I think recursion this thing very difficult to grasp. But when it's done, it still looks very magical.

During the implementation of this algorithm. Several problems were encountered.

1. When the file is read, I want to use the pointer operation in order not to waste the stack space I am applying for, but I am having difficulty. No matter where I put the new operation of the application address, where the delete is placed. Errors are always present. Do not understand the original, when I know how to do, I would like to improve my this article.

2. The algorithm in the book ignores the problem that when Low+1=high, this time also needs to make a judgment, otherwise this recursive end is unclear. But I have written a lot of statements about this judgment, and I don't know if there are some places I didn't notice.

3. I think the point of my success is that I think about using structs to follow up on the pseudo-code written in the book. Perhaps this is the benefit of the use of structs. The other thing is that I think of the difference between the functions in the class and the functions, and the pros and cons of some of them. The function in the class is very simple for multiple return values, which is to define the value you want to return as a private variable, and then the assignment of the private variable is naturally very concise. But that might not make recursion possible? I may not be able to understand some places. I don't really have recursion in class.

Algorithm learning--Divide and conquer algorithm

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.