The idea comes from the introduction of the algorithm, the array is divided into two sub-arrays of left and right, then the largest sub-array may be in the left sub-array, may also be in the rightmost sub-array, but also possible to cross the middle element, only three cases. For the first two cases, recursive solution can be used, for the third case, the linear time complexity can be used to solve the function, see Code.
#include <iostream> #include <map>using namespace std;//the thought of this algrithm is Devide-and-couqueint I Nt_max = maximum value of the 0x7fffffff;//int type, the first digit of the binary represents the symbol int int_min = 0x80000000;//int The minimum value of the type struct array_data{int low; int high; int sum;}; array_data* Find_max_subarray (int* A, int low,int high); array_data* Find_max_crossing_subarray (int* A, Int. low, int.) //find the Max Subarray which crosses middle element//the complexity of this code is linear (O (n)) array_data* Find_max_crossing_subarray (int* A, int low, int. high) {int mid = (Low+high) >> 1; int left_sum = int_min; int sum = 0; int max_left = 0;//The initial point selection does not matter, because the sum of the array is greater than int_min//from the middle to the left to find the sum of the largest sub-tree group for (int i=mid; i>=low; i--) {sum = Sum + a[i]; if (Sum > left_sum) {left_sum = sum; Max_left = i; }}//Start from the middle to the right to find the sum of the largest sub-tree Group int right_sum = int_min; int max_right = mid+1;//initially thinks the position of the first element in the middle to the right is the end point of the largest subtree group, sum = 0; for (int i=mid+1; i<=high; i++) {sum = Sum + a[i]; if (Sum > right_sum) {right_sum = sum; Max_right = i; }} array_data* arr = new Array_data (); Arr->low = Max_left; Arr->high = Max_right; Arr->sum = Left_sum + right_sum; return arr;} array_data* Find_max_subarray (int* a,int low,int High) {if (low = = high) {array_data* arr = new Array_data (); Arr->low = low; Arr->high = high; Arr->sum = A[low]; Return arr;//base case:only one element}else{int mid = (Low+high) >> 1; Recursion left array_data* arr_left = Find_max_subarray (A,low,mid); Recursion right array_data* Arr_right = Find_max_subarray (A,mid+1,high); Calc Crossing middle array_data* arr_cross = Find_max_crossing_subarray (A,low,high); Compare three situation int left_sum = arr_left->sum; int right_sum = arr_right->sum; int cross_sum = arr_cross->sum; IF (left_sum >= right_sum && left_sum >= cross_sum) {return arr_left; }else if (right_sum >= left_sum && right_sum >= cross_sum) {return arr_right; }else{return arr_cross; }}}int Main () {int a[] = { -1,2,4,-2,-4,4,9,-3,2};//Note Initialization must be an array of int low = 0; int high = sizeof (A)/sizeof (a[0])-1; array_data* Arr_done = Find_max_subarray (A,low,high); Output a[] for (int i=0;i<= high;i++) {cout<<a[i]<< ""; } cout<<endl; cout<< "Low:" <<arr_done->low<< "High:" <<arr_done->high<< "sum:" <<arr_ done->sum<<endl; return 0;}
Divide-and-conquer method to find the largest and most continuous sub-arrays