# 数据结构---最大子序列算法（不同时间复杂度 C语言）

## 最大自序列的算法

#include<stdio.h>
#define N 10                              //N是序列的的个数
int num[N]={5,6,7,8,-9,1,2,3,4,0};
void main(){
int thissum,maxsum = 0;
int i,j,k;
for(i=0;i<N;i++){
for(j=i;j<N;j++){
thissum=0;
for(k=i;k<=j;k++)
thissum += num[k];
if(thissum>maxsum)
maxsum = thissum;
}
}
printf("%d",maxsum);
}


#include<stdio.h>
#define N 10                              //N是序列的的个数
int num[N]={5,6,7,8,-9,1,2,3,4,0};
void main(){
int thissum,maxsum = 0;
int i,j;
for(i=0;i<N;i++){
thissum = 0;
for(j=i;j<N;j++){
thissum += num[j];
if(thissum>maxsum)
maxsum = thissum;
}
}
printf("%d",maxsum);
}


int DivideAndConquer( int List[], int left, int right )
{ /* 分治法求List[left]到List[right]的最大子列和 */
int MaxLeftSum, MaxRightSum; /* 存放左右子问题的解 */
int MaxLeftBorderSum, MaxRightBorderSum; /*存放跨分界线的结果*/

int LeftBorderSum, RightBorderSum;
int center, i;

if( left == right )  { /* 递归的终止条件，子列只有1个数字 */
if( List[left] > 0 )  return List[left];
else return 0;
}

/* 下面是"分"的过程 */
center = ( left + right ) / 2; /* 找到中分点 */
/* 递归求得两边子列的最大和 */
MaxLeftSum = DivideAndConquer( List, left, center );
MaxRightSum = DivideAndConquer( List, center+1, right );

/* 下面求跨分界线的最大子列和 */
MaxLeftBorderSum = 0; LeftBorderSum = 0;
for( i=center; i>=left; i-- ) { /* 从中线向左扫描 */
LeftBorderSum += List[i];
if( LeftBorderSum > MaxLeftBorderSum )
MaxLeftBorderSum = LeftBorderSum;
} /* 左边扫描结束 */

MaxRightBorderSum = 0; RightBorderSum = 0;
for( i=center+1; i<=right; i++ ) { /* 从中线向右扫描 */
RightBorderSum += List[i];
if( RightBorderSum > MaxRightBorderSum )
MaxRightBorderSum = RightBorderSum;
} /* 右边扫描结束 */

/* 下面返回"治"的结果 */
return Max3( MaxLeftSum, MaxRightSum, MaxLeftBorderSum + MaxRightBorderSum );
}

int MaxSubseqSum3( int List[], int N )
{ /* 保持与前2种算法相同的函数接口 */
return DivideAndConquer( List, 0, N-1 );
}



#include<stdio.h>
#define N 10                              //N是序列的的个数
int num[N]={5,6,7,8,-9,1,2,3,4,0};
void main(){
int thissum=0,maxsum = 0;
int i;
for(i=0;i<N;i++){
thissum+=num[i];
if(thissum>maxsum)
maxsum = thissum;
else if(thissum<0)
thissum=0;
}
printf("%d",maxsum);
}