divide-conquer-combine(4.1 from the introduction to algorithm)

this example is from chapter 4 in 《the introduction to algorithm》

the main idea is all showed in the book , i think maybe realizing the algorithm is a good way to understand it.

/*
from : introduction to algorithm chapter four
algorithm:divide and conquer
the time complexity of this algorithm is O(n * logn) */
#include <stdio.h> int Find_max_crossing_subarray(int *a, int low, int mid, int high)
{
int left_sum = -0xffff;
int sum = ;
for (int i = mid; i >= low; i--)
{
sum += a[i];
if (sum > left_sum)
left_sum = sum;
}
int right_sum = -0xffff;
sum = ;
for (int j = mid+; j <= high; j++)
{
sum += a[j];
if (sum > right_sum)
right_sum = sum;
}
return left_sum + right_sum;
} int find_maximum_subarray(int *a, int low, int high)
{
if (high == low)
{
return *(a+high); // base case: only one element
}
else
{
int mid = (high + low)/;
if ((find_maximum_subarray(a, low, mid) >= find_maximum_subarray(a, mid+, high))&&(find_maximum_subarray(a, low, mid)>=Find_max_crossing_subarray(a,low,mid,high)))
{
return find_maximum_subarray(a, low, mid);
}
else if ((find_maximum_subarray(a, mid+, high) >= find_maximum_subarray(a, low, mid))&&(find_maximum_subarray(a, mid+, high) >= Find_max_crossing_subarray(a,low,mid,high)))
{
return find_maximum_subarray(a, mid+, high);
}
else
{
return Find_max_crossing_subarray(a, low, mid, high);
}
}
} int main()
{
int a[]={,-,-,,-,-,-,,,-,,-,-,,-,};
int b[]={,,,,};
printf("%d\n",find_maximum_subarray(a,,));
printf("%d\n",find_maximum_subarray(b,,));
}
上一篇:【云周刊】第206期:2018年阿里云云攻略十二篇精选好文


下一篇:2016huasacm暑假集训训练四 递推_A