#include <iostream> #include <vector>using namespace std;/********************************************* The method of dividing and administering, using the idea of recursion//Ugly_chen 2014.11.3 22:24//Description: Divides the array into two parts, the right part and the left part , or the sum of the right and left parts.//—————————————————————— "Time: O (NLOGN)/******************************************************** /int find_max_cross_subarray (int a[], int left, int mid, Int. right) {int left_sum = 0; int max_left_sum = 0;for (int i = mid; I >= left; i--) {left_sum + = a[i];if (Left_sum > Max_left_sum) {max_left_sum = Left_sum;}} int right_sum = 0;int max_right_sum = 0;for (int i = mid + 1; I <= right; i++) {right_sum + = a[i];if (Right_sum > Max _right_sum) {max_right_sum = Right_sum;}} return max_left_sum + max_right_sum;} int Find_max_subarray (int a[], int left, int. right) {int left_sum, right_sum, cross_sum;if (left = = right) {return a[left];} Else{int mid = (left + right)/2;left_sum = Find_max_subarray (A, left,mid); RIGht_sum = Find_max_subarray (A, mid + 1, right); cross_sum = Find_max_cross_subarray (A, left, Mid, right);} if (left_sum >= right_sum && left_sum >= cross_sum) return left_sum; else if (right_sum >= left_sum && right_sum >= cross_sum)//Right return right_sum; return cross_sum;} /* —————————————————————————————————————————————————————————————————————————————————————————— *///Method Two: The most violent method This uses the vector array time: O (n^3)//introduction: Sum each possible combination, then compare the largest and./* ——————————————————————————————————————————————————— ———————————————————————————————————————— */int find_max_array2 (const vector<int> &a) {int max_sum = 0;for ( size_t i = 0; I < a.size (); i++) {for (Auto J = i, J < A.size (), j + +) {int this_sum = 0;for (auto k = i; k <= J; k++) This_sum + = A[k];if (this_sum >max_sum) max_sum = This_sum;}} return max_sum;} /************************************************************************************************///method Three: Simplification of the above algorithm (actually removing the second layer of the above loop)//introduction: or all the combined and,Each cycle is calculated to take the largest and save in Max_sum.//—————————————————————— Time: O (n^2)/************************************************* /int find_max_array3 (const vector<int> &a) {int max_sum = 0 for (size_t i = 0; i < a.size (); i++) {int this_sum = 0;for (Auto j = i; J < A.size (); j + +) {this_sum + = a[j];if (thi s_sum>max_sum) max_sum = This_sum;}} return max_sum;} /*————————————————————————————————————————————————————————————————————————————————————————————————————————————— ——————————— *///method Four: The elements in the array to scan, with this_sum record scanning elements and, starting with the first element scanning, this_sum can not be less than 0, if less than 0, then//this_sum from the new record scanning elements and ( This time this_sum set to 0), if This_sum is not 0, then compared with max_sum (greater than max_sum that will this_sum the//value to max_sum, not more than the same) meaning is scanned to the largest and saved in Max_sum. —————————————————————— Time: O (N)/* ———————————————————————————————————————————————————————————————————————————— ————————————————————————————————————————————— */int find_max_array4 (const vector<int> &a) {int max_sum = 0; int this_sum = 0;for (size_t i = 0; i < A.SIze (); i++) {this_sum + = a[i];if (This_sum > Max_sum) max_sum = This_sum;else if (this_sum < 0) this_sum = 0;} return max_sum;} int main () {int a[] = {9, 6,-7, 1, 8,-20, 5, 3, 4, 0, 2};std::cout << Find_max_subarray (A, 0, ten) << Std::en Dl;vector<int> AVec = {9, 6,-7, 1, 8, -20, 5, 3, 4, 0, 2};std::cout << find_max_array2 (AVEC) << std:: Endl;std::cout << find_max_array3 (aVec) << std::endl;std::cout << find_max_array4 (AVEC) << std :: Endl;return 0;}
The introduction of the maximal subarray is only divided into recursive solution, brute force solution, and memory scanning method.