-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMaxSumTest.cpp
More file actions
152 lines (127 loc) · 4.16 KB
/
MaxSumTest.cpp
File metadata and controls
152 lines (127 loc) · 4.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#include <iostream.h>
#include "vector.h"
/* START: Fig02_05.txt */
/**
* Cubic maximum contiguous subsequence sum algorithm.
*/
int maxSubSum1( const vector<int> & a )
{
/* 1*/ int maxSum = 0;
/* 2*/ for( int i = 0; i < a.size( ); i++ )
/* 3*/ for( int j = i; j < a.size( ); j++ )
{
/* 4*/ int thisSum = 0;
/* 5*/ for( int k = i; k <= j; k++ )
/* 6*/ thisSum += a[ k ];
/* 7*/ if( thisSum > maxSum )
/* 8*/ maxSum = thisSum;
}
/* 9*/ return maxSum;
}
/* END */
/* START: Fig02_06.txt */
/**
* Quadratic maximum contiguous subsequence sum algorithm.
*/
int maxSubSum2( const vector<int> & a )
{
/* 1*/ int maxSum = 0;
/* 2*/ for( int i = 0; i < a.size( ); i++ )
{
/* 3*/ int thisSum = 0;
/* 4*/ for( int j = i; j < a.size( ); j++ )
{
/* 5*/ thisSum += a[ j ];
/* 6*/ if( thisSum > maxSum )
/* 7*/ maxSum = thisSum;
}
}
/* 8*/ return maxSum;
}
/* END */
/**
* Return maximum of three integers.
*/
int max3( int a, int b, int c )
{
return a > b ? a > c ? a : c : b > c ? b : c;
}
/* START: Fig02_07.txt */
/**
* Recursive maximum contiguous subsequence sum algorithm.
* Finds maximum sum in subarray spanning a[left..right].
* Does not attempt to maintain actual best sequence.
*/
int maxSumRec( const vector<int> & a, int left, int right )
{
/* 1*/ if( left == right ) // Base case
/* 2*/ if( a[ left ] > 0 )
/* 3*/ return a[ left ];
else
/* 4*/ return 0;
/* 5*/ int center = ( left + right ) / 2;
/* 6*/ int maxLeftSum = maxSumRec( a, left, center );
/* 7*/ int maxRightSum = maxSumRec( a, center + 1, right );
/* 8*/ int maxLeftBorderSum = 0, leftBorderSum = 0;
/* 9*/ for( int i = center; i >= left; i-- )
{
/*10*/ leftBorderSum += a[ i ];
/*11*/ if( leftBorderSum > maxLeftBorderSum )
/*12*/ maxLeftBorderSum = leftBorderSum;
}
/*13*/ int maxRightBorderSum = 0, rightBorderSum = 0;
/*14*/ for( int j = center + 1; j <= right; j++ )
{
/*15*/ rightBorderSum += a[ j ];
/*16*/ if( rightBorderSum > maxRightBorderSum )
/*17*/ maxRightBorderSum = rightBorderSum;
}
/*18*/ return max3( maxLeftSum, maxRightSum,
/*19*/ maxLeftBorderSum + maxRightBorderSum );
}
/**
* Driver for divide-and-conquer maximum contiguous
* subsequence sum algorithm.
*/
int maxSubSum3( const vector<int> & a )
{
return maxSumRec( a, 0, a.size( ) - 1 );
}
/* END */
/* START: Fig02_08.txt */
/**
* Linear-time maximum contiguous subsequence sum algorithm.
*/
int maxSubSum4( const vector<int> & a )
{
/* 1*/ int maxSum = 0, thisSum = 0;
/* 2*/ for( int j = 0; j < a.size( ); j++ )
{
/* 3*/ thisSum += a[ j ];
/* 4*/ if( thisSum > maxSum )
/* 5*/ maxSum = thisSum;
/* 6*/ else if( thisSum < 0 )
/* 7*/ thisSum = 0;
}
/* 8*/ return maxSum;
}
/* END */
/**
* Simple test program.
*/
int main( )
{
vector<int> a( 8 );
a[ 0 ] = 4; a[ 1 ] = -3; a[ 2 ] = 5; a[ 3 ] = -2;
a[ 4 ] = -1; a[ 5 ] = 2; a[ 6 ] = 6; a[ 7 ] = -2;
int maxSum;
maxSum = maxSubSum1( a );
cout << "Max sum is " << maxSum << endl;
maxSum = maxSubSum2( a );
cout << "Max sum is " << maxSum << endl;
maxSum = maxSubSum3( a );
cout << "Max sum is " << maxSum << endl;
maxSum = maxSubSum4( a );
cout << "Max sum is " << maxSum << endl;
return 0;
}