Learning Algorithms - Divide and Conquer Algorithms

The program that implements the first algorithm of Introduction to Algorithms is the following:

#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;i<high;i++){
        sum=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 Clever use of recursion, I think recursion is very difficult to master. But after using it, it seems to be very effective

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

1. When reading a file, in order not to waste the stack space I applied for, I wanted to use pointer operations, but encountered difficulties. No matter where I put the new operation of the application address, where is the delete. There will always be errors. I don't understand the reason, when I know how to do it, I will improve this article of mine.

2. The algorithm in the book ignores a problem, that is, when low+1=high, a judgment needs to be made at this time, otherwise the end point of the recursion is unclear. But I have written a lot of sentences to make this judgment. I don't know if there are some places I didn't pay attention to.

3. I think I am more successful in that I thought of using structs to follow the pseudocode written in the book. Perhaps this is the benefit of using structs. Another point is that I thought about the difference between the function in the class and this kind of function and some functional advantages and disadvantages. The functions in the class are actually very simple to operate for multiple return values, that is, define the private variables of the values ​​you want to return, and then assign the private variables, which is naturally very concise. But then maybe recursion is not possible? There may be some places I didn't understand. I really haven't done recursion in the class~~~

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325360950&siteId=291194637