Find_Maximum_Subarray C++
Find_Maximum_Subarray
最大子数组一定在三种情况中:
A[low,mid] , A[mid+1,high] ,A[i,j]
Divide:
2分方式:
if (low == high)
return { low,high,A[low] };
else
{
int mid = (low + high) / 2;
//vec_f = left_low,left_high,left_sum
auto vec_f = Find_Maximum_Subarray(A, low, mid);
//vec_r = right_low,right_high,right_sum
auto vec_r = Find_Maximum_Subarray(A, mid + 1, high);
Conquer:
子问题找最大数组:
[i,j] 可以是 [low,mid], [mid+1,high]
std::vector<int> // v.front() = max_left,v[1]= max_right,v.back()=left_sum + right_sum
Find_Max_Crossing_Subarray(std::vector<int> A, int low, int mid, int high)
{
int left_sum = INT_MIN;
int sum = 0,max_left = mid, max_right=mid;
for (auto i = mid; i >= low;--i)
{
sum += A[i];
if (sum > left_sum)
{
left_sum = sum;
max_left = i;
}
}
int right_sum = INT_MIN;
sum = 0;
for (auto j = mid + 1;j <= high;++j)
{
sum += A[j];
if (sum > right_sum)
{
right_sum = sum;
max_right = j;
}
}
return { max_left,max_right,left_sum + right_sum };
}
最后找到三者中最大的
if ((vec_f.back() >= vec_r.back()) && (vec_f.back() >= vec_c.back()))
return { vec_f[0],vec_f[1],vec_f[2] };
if ((vec_r.back() >= vec_f.back()) && (vec_r.back() >= vec_c.back()))
return { vec_r[0],vec_r[1],vec_r[2] };
else
return { vec_c[0],vec_c[1],vec_c[2] };
c++ code
#include"FIND_MAXIMUM_SUBARRAY .h"
#pragma once
#include<vector>
#include<limits>
#include<string>
std::vector<int> // v.front() = max_left,v[1]= max_right,v.back()=left_sum + right_sum
Find_Max_Crossing_Subarray(const std::vector<int> &A,const int &low,const int &mid,const int &high)
{
int left_sum = INT_MIN;
int sum = 0,max_left = mid, max_right=mid;
for (auto i = mid; i >= low;--i)
{
sum += A[i];
if (sum > left_sum)
{
left_sum = sum;
max_left = i;
}
}
int right_sum = INT_MIN;
sum = 0;
for (auto j = mid + 1;j <= high;++j)
{
sum += A[j];
if (sum > right_sum)
{
right_sum = sum;
max_right = j;
}
}
return { max_left,max_right,left_sum + right_sum };
}
std::vector<int> //v.front() = low ,v[1] = high, v.back() = sum;
Find_Maximum_Subarray(const std::vector<int> &A,const int low,const int high)
{
//一用Divide方式 :带入子问题的下标(范围)
if (low == high)
return { low,high,A[low] };
else
{
int mid = (low + high) / 2;
//vec_f = left_low,left_high,left_sum
auto vec_f = Find_Maximum_Subarray(A, low, mid);
//vec_r = right_low,right_high,right_sum
auto vec_r = Find_Maximum_Subarray(A, mid + 1, high);
//Conquer部分:如何解决子问题
//vec_c = cross_low,cross_high,cross_sum
auto vec_c = Find_Max_Crossing_Subarray(A, low, mid, high);
//子问题的答案如何让处理: 不影响每次带入Find_c 的下标
if ((vec_f.back() >= vec_r.back()) && (vec_f.back() >= vec_c.back()))
return { vec_f[0],vec_f[1],vec_f[2] };
if ((vec_r.back() >= vec_f.back()) && (vec_r.back() >= vec_c.back()))
return { vec_r[0],vec_r[1],vec_r[2] };
else
return { vec_c[0],vec_c[1],vec_c[2] };
}
}
main.cpp
#include"FIND_MAXIMUM_SUBARRAY .h"
void find_maximum_subarray()
{
//13,-3,-25,20,-3,-16,-23,18,20,-7,12,-5,-22,15,-4,7
vector<int> arr = { 13,-3,-25,20,-3,-16,-23 };
cout << " array is " << endl;
for (auto const& i : arr)
cout << i << " ";
cout << endl;
auto v_a = Find_Maximum_Subarray(arr, 0, arr.size()-1);
cout << " Sum: " << v_a[2] << endl;
cout << " max subarray is " << endl;
for (auto i = v_a[0];i <= v_a[1];++i)
cout << arr[i] << " ";
cout << endl;
}
int main()
{
find_maximum_subarray();
}