二分法求数列的最大子段和问题

给定n个元素的整数数列,求出子段和最大的子段。

 

#include <stdio.h> int max_sub_sum(int a[],int left, int right,int *left_partion,int *right_partion){ int center,i,j,sum,left_sum,right_sum,s1,s2,lefts,rights; if(left == right){ /*二分法递归结束条件*/ if(a[left] > 0){ return a[left]; *left_partion = left; *right_partion = left; }else { return 0; } }else{ center = (left + right)/2; left_sum = max_sub_sum(a, left,center,left_partion,right_partion); /*求左边最大子段*/ right_sum = max_sub_sum(a, center + 1, right,left_partion,right_partion);/*求右边最大子段*/ /*下面求中间交叉部分最大子段*/ s1 = 0; lefts = 0; for(i=center; i>=left;i--){ lefts = lefts + a[i]; if(lefts > s1){ s1 = lefts; *left_partion = i; } } s2 =0; rights = 0; for(j = center + 1; j<=right;j++){ rights = rights + a[j]; if(rights > s2){ s2 = rights; *right_partion = j; } } /*选择最大子段*/ if((s1 + s2 < left_sum) && (right_sum < left_sum))return left_sum; if(s1 + s2 < right_sum)return right_sum; return s1+s2; } } int main(){ int a[] = {-2,1,-4,13,-5,6}; int left_partion = -1; /*左边界*/ int right_partion = -1;/*右边界*/ int i =0; int ret = max_sub_sum(a,1,5,&left_partion,&right_partion); printf("max_sub_sum is:%d,left_partion=%d,right_partion=%d/n",ret,left_partion,right_partion); printf("the partion is:["); for(i=left_partion; i <=right_partion; i++){ printf("%d",a[i]); if( i != right_partion){ printf(","); } } printf("]"); getch(); return 0; }

你可能感兴趣的:(二分法求数列的最大子段和问题)