算法导论寻找最大子数组 (二)

2014-11-24 02:48:59 · 作者: · 浏览: 3
int tmp_right_low;
int tmp_right_high;
int tmp_right_sum;

Find_MaxiMum_SubArray(A,mid+1,high,tmp_right_low,tmp_right_high,tmp_right_sum);

int tmp_cross_low;
int tmp_cross_high;
int tmp_cross_sum;

Find_MAX_CROSSING_SUBARRAY(A,low,mid,high,tmp_cross_low,tmp_cross_high,tmp_cross_sum);

if ((tmp_left_sum>=tmp_right_sum)&&(tmp_left_sum>=tmp_cross_sum))
{
max_left = tmp_left_low;
max_right = tmp_left_high;
max_value = tmp_left_sum;
}
else if((tmp_right_sum>=tmp_left_sum)&&(tmp_right_sum>=tmp_cross_sum))
{
max_left = tmp_right_low;
max_right = tmp_right_high;
max_value = tmp_right_sum;
}
else
{
max_left = tmp_cross_low;
max_right = tmp_cross_high;
max_value = tmp_cross_sum;
}

}

return 0;
}
后边我再写了一个调用的代码:


[cpp]
int B[10] = {1,-10,2,4,6,-15,6,1,9,-8};

int main(int argc, char **argv)
{

cout<
int max_left,max_right,max_value;
Find_MaxiMum_SubArray(B,0,10,max_left,max_right,max_value);
cout<< max_left<<"\t"< cout< system("pause");
return 0;
}

int B[10] = {1,-10,2,4,6,-15,6,1,9,-8};

int main(int argc, char **argv)
{

cout<
int max_left,max_right,max_value;
Find_MaxiMum_SubArray(B,0,10,max_left,max_right,max_value);
cout<< max_left<<"\t"< cout< system("pause");
return 0;
}
好了,这个算法是分治算法的另一种典型的例子。因为再进行递归的时候要分情况递归,O(∩_∩)O~。