4.1-1 What does FIND-MAXIMUM-SUBARRAY return when all elements of A are negative?
The biggest negative one.
4.1-2 Write pseudocode for the brute-force method of solving the maximum-subarray problem. Your procedure should run inΘ(n^2);
The biggest negative one.
4.1-2 Write pseudocode for the brute-force method of solving the maximum-subarray problem. Your procedure should run inΘ(n^2);
typedef struct{
int max_left;
int max_right;
int max;
}maxinfo;
maxinfo bf_MaxSubArray(int *a,int n)
{
maxinfo max;
max.max_left= 0;
max.max_right= 0;
max.max= MIN_INT;
int tmp=0;
for(int i=0;i<n;i++)
{
for(int j=i;j<n;j++)
{
tmp += a[j];
if(tmp > max.max)
{
max.max = tmp;
max.max_left = i;
max.max_right = j;
}
}
tmp = 0;
}
return max;
}