</p> return maxSum;
; G$ Q- l9 [5 m1 i1 N) a; e" ` }
! a1 H$ c4 F# u! E& E% j; L1 b7 V5 [0 T /**
( M- E) s$ o. j R6 H9 t * Quadratic maximum contiguous subsequence sum algorithm.; ~, ~% Y! o( o1 b+ v
* seqStart and seqEnd represent the actual best sequence.
& r; }2 S* D7 E$ T1 K4 H */
* G% U8 l/ K1 P9 d public static int maxSubSum2( int [ ] a )% z# m1 z; Q& _, g6 S& k* G
{
0 b- P' C1 ?% i int maxSum = 0;
4 @3 v5 o! A* @" W* C for( int i = 0; i < a.length; i++ )
, A s4 c" P1 I, p* F' x {/ W9 k+ f" o/ ?% U- ?
int thisSum = 0;+ B# \- D* l+ t+ o6 I2 T7 k( M
for( int j = i; j < a.length; j++ )
3 T6 s9 b8 U+ y- Z {
6 g/ l/ `4 Y2 l4 `8 C7 A5 x) q thisSum += a[ j ];9 t. P4 \, v, ]. h
if( thisSum > maxSum )
$ I' _0 w. w2 u& s7 h, \ {- R0 o9 M4 T% Y/ @0 Y5 U$ W
maxSum = thisSum;8 G0 l) g( w: A( d4 n
seqStart = i;" F C4 Z( u1 O( ]3 [
seqEnd = j; i& _" Q( c' X8 b2 b/ X2 U
}
0 B% A8 Y5 `- H9 E }* i( M7 h, E1 b, c8 W$ i, o7 p
}
( |# s. a. a- N6 n |: @' e4 K return maxSum;5 ]# D$ w' a& [/ p3 i6 a
}
8 U' J% E4 l8 j4 X4 M /**6 H1 Z2 S7 [: h9 i1 K6 e
* Linear-time maximum contiguous subsequence sum algorithm.
' j) a% |$ c# \ _/ V5 p9 @ k * seqStart and seqEnd represent the actual best sequence.6 l. N/ U" y+ ^1 |
*/
3 J/ x _- X$ {9 V public static int maxSubSum3( int [ ] a )
& W W. F% B3 P) x, D! L {
2 W+ Y% {5 P0 S9 w6 J int maxSum = 0; ^9 u" Y0 o/ P8 s+ z
int thisSum = 0;+ O# x' w" o3 m! ]% K
for( int i = 0, j = 0; j < a.length; j++ )
3 @6 N+ a4 D- F. y {" T3 t+ y9 z/ `! r
thisSum += a[ j ];
& k, a4 w0 U/ D$ o if( thisSum > maxSum )
8 ?) b+ T6 c: S) Z {3 Y6 |0 b3 B; g5 L1 r1 h7 B, x
maxSum = thisSum;; Z& I" T6 {, X X- W8 h* c
seqStart = i;
, N+ M; \; t* h" `, L$ o, _4 `# l seqEnd = j;
! e y- C. m+ C8 v2 P: L1 I }
( e/ s" g9 k; q# V& m7 a- A else if( thisSum < 0 )
* n( u9 m$ P' k5 I; T$ {! u {6 N6 _% H' E: Q5 V3 B I' j2 \
i = j + 1;+ m( H9 C) I9 W% I
thisSum = 0;
" z# X2 |1 d; Q4 @8 T }7 G3 Q3 u' n$ @7 @5 z
}+ _, z/ l7 `- O( T* O7 n# g2 H
return maxSum;: e$ k7 Z' H) K
}
7 t6 b+ z D7 K4 S8 G/ v! h /**7 @; x% E: ^ p% R
* Recursive maximum contiguous subsequence sum algorithm.0 A. P2 A) J$ m
* Finds maximum sum in subarray spanning a[left..right].! h8 V8 [6 j$ b& N: p
* Does not attempt to maintain actual best sequence.- C: Z" Z3 B7 z* `) h( x
*/8 o1 n0 |4 P2 m/ k
private static int maxSumRec( int [ ] a, int left, int right ): V7 K; D4 V+ C" r# n+ }' `1 A5 y
{! m% x) F" v$ V. N0 m& b9 ~% \
int maxLeftBorderSum = 0, maxRightBorderSum = 0;* E9 ~$ O. W, [2 N5 f: m6 M
int leftBorderSum = 0, rightBorderSum = 0;
5 q4 m; [$ c2 X& {1 K int center = ( left + right ) / 2;' j* i/ E) ~) Z! P3 t3 |* c5 ]) h; R
if( left == right ) // Base case
8 z4 [' d( O" P) V return a[ left ] > 0 ? a[ left ] : 0;& s4 Z+ z4 z0 b* Z* `
int maxLeftSum = maxSumRec( a, left, center );! V7 z0 f7 W: P
int maxRightSum = maxSumRec( a, center + 1, right );! ^+ U0 K6 d5 ~; Q9 l
for( int i = center; i >= left; i– )' k! \$ C) [" l+ w1 x7 s) E& E
{
+ p6 ?1 y) N6 I leftBorderSum += a[ i ];7 x$ O3 A% M) o
if( leftBorderSum > maxLeftBorderSum )
6 _$ B: G) z$ r ]" Y0 f maxLeftBorderSum = leftBorderSum;$ G0 ?7 K3 |$ r- f9 f/ r
8 ~& M* r% f$ C, l' S. W- w } |