forked from algorithm-visualizer/algorithm-visualizer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode.js
More file actions
29 lines (22 loc) · 757 Bytes
/
Copy pathcode.js
File metadata and controls
29 lines (22 loc) · 757 Bytes
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
var maxSubarraySum = (function maxSubarray (array) {
var maxSoFar = 0,
maxEndingHere = 0;
tracer._print ('Initializing maxSoFar = 0 & maxEndingHere = 0');
for (var i = 0; i < array.length; i++) {
tracer._select (i);
tracer._print (maxEndingHere + ' + ' + array [i]);
maxEndingHere += array [i];
tracer._print ('=> ' + maxEndingHere);
if (maxEndingHere < 0) {
tracer._print ('maxEndingHere is negative, set to 0');
maxEndingHere = 0;
}
if (maxSoFar < maxEndingHere) {
tracer._print ('maxSoFar < maxEndingHere, setting maxSoFar to maxEndingHere (' + maxEndingHere + ')');
maxSoFar = maxEndingHere;
}
tracer._deselect (i);
}
return maxSoFar;
}) (D);
tracer._print ('Maximum Subarray\'s Sum is: ' + maxSubarraySum);