Код, который я написал и протестировал правильно.Думал, что это может быть полезно ...
#include <iostream.h>
void findMaxCrossSubArray(int a[], int low, int mid, int high, int crossCur[])
{
int leftSum = -9999;
int sum = 0;
int maxLeft, maxRite;
maxLeft = maxRite = 0;
for (int i = mid; i >= low; i--)
{
sum += a[i];
if (sum > leftSum)
{
leftSum = sum;
maxLeft = i;
}
}
int riteSum = -9999;
sum = 0;
for (int j = mid + 1; j <= high; j++)
{
sum += a[j];
if (sum > riteSum)
{
riteSum = sum;
maxRite = j;
}
}
crossCur[0] = maxLeft;
crossCur[1] = maxRite;
crossCur[2] = leftSum + riteSum;
return;
}
void findMaxSubArray (int a[], int low, int high, int curMax[])
{
if (high == low)
{
curMax[0] = low;
curMax[1] = high;
curMax[2] = a[low];
return;
}
int mid = (low + high)/2;
int leftCur[3], riteCur[3], crossCur[3];
findMaxSubArray(a, low, mid, leftCur);
findMaxSubArray(a, mid+1, high, riteCur);
findMaxCrossSubArray(a, low, mid, high, crossCur);
if ((leftCur[2] >= riteCur[2]) && (leftCur[2] >= crossCur[2]))
{
curMax[0] = leftCur[0];
curMax[1] = leftCur[1];
curMax[2] = leftCur[2];
return;
}
if ((riteCur[2] >= leftCur[2]) && (riteCur[2] >= crossCur[2]))
{
curMax[0] = riteCur[0];
curMax[1] = riteCur[1];
curMax[2] = riteCur[2];
return;
}
curMax[0] = crossCur[0];
curMax[1] = crossCur[1];
curMax[2] = crossCur[2];
return;
}
int main()
{
int a[] = {13, -3, -25, 20, -3, -16, -23, 18, 20,-7, 12, -5, -22, 15, -4, 7};
int curMax[] = {0,0,0};
findMaxSubArray(a, 0, 15, curMax);
cout << curMax[0] << "," << curMax[1] << "," << curMax[2] << endl;
}