Algorithm for Finding the maximum child segment and
Public class MaxSubSeqSum {/*** algorithm 1, search for */public static final int maxSubSeqSum1 (int seq []) {int length = seq. length; int sum = 0; for (int I = 0; I <length; I ++) {for (int j = I; j <length; j ++) {int theSum = 0; for (int k = I; k <= j; k ++) {theSum + = seq [k];} if (sum <theSum) sum = theSum ;}} return sum;}/*** algorithm 2, improved algorithm 1 */public static final int maxSubSeqSum2 (int seq []) {int length = seq. length; int sum = 0; for (int I = 0; I <length; I ++) {int theSum = 0; for (int j = I; j <length; j ++) {theSum + = seq [j]; if (theSum> sum) {sum = theSum ;}} return sum ;}/ *** algorithm 3: division and control */public static final int maxSubSeqSum3 (int seq []) {return maxSubSeqSum31 (seq, 0, seq. length-1);} private static int max (int... args) {int max = Integer. MIN_VALUE; for (int I: args) {if (I> max) max = I;} return max;} private static final int maxSubSeqSum31 (int [] seq, int left, int right) {if (right-left = 1) {// There are only two left: return max (seq [left], seq [right], seq [left] + seq [right], 0);} else if (left = right) {return max (seq [left], 0 );} else {int middle = (left + right)/2; int maxLeft = maxSubSeqSum31 (seq, left, middle); int maxRight = maxSubSeqSum31 (seq, middle + 1, right ); int maxMiddle = maxSubSeqMiddle (seq, left, right, middle); return max (maxLeft, maxRight, maxMiddle);} private static final int maxSubSeqMiddle (int [] seq, int left, int right, int middle) {int maxLeft = 0, maxRight = 0; int temp = 0; for (int I = middle; I> = left; I --) {temp + = seq [I]; if (maxLeft <temp) {maxLeft = temp;} temp = 0; for (int I = middle + 1; I <= right; I ++) {temp + = seq [I]; if (maxRight <temp) {maxRight = temp;} return max (maxLeft, maxRight, maxLeft + maxRight );} /*** fastest Calculation Method * @ param seq * @ return */public static final int maxSubSeqSum4 (int seq []) {int sum = 0, theSum = 0; int length = seq. length; for (int I = 0; I <length; I ++) {theSum = max (theSum + seq [I], 0); if (sum <theSum) {sum = theSum ;}} return sum;} public static void main (String [] args) throws Exception {int length = 100000; int [] arr = new int [length]; random rand = new Random (10); for (int I = 0; I <length; I ++) {arr [I] = rand. nextInt (11)-5;} long t = System. currentTimeMillis (); // int ret1 = maxSubSeqSum1 (arr); // System. out. println ("algorithm 1 time consumed:" + (System. currentTimeMillis ()-t); // System. out. println ("result 1:" + ret1); // t = System. currentTimeMillis (); int ret2 = maxSubSeqSum2 (arr); System. out. println ("algorithm 2 time consumed:" + (System. currentTimeMillis ()-t); System. out. println ("result 2:" + ret2); t = System. currentTimeMillis (); int ret3 = maxSubSeqSum3 (arr); System. out. println ("algorithm 3 time consumed:" + (System. currentTimeMillis ()-t); System. out. println ("result 3:" + ret3); t = System. currentTimeMillis (); int ret4 = maxSubSeqSum4 (arr); System. out. println ("algorithm 4 time consumed:" + (System. currentTimeMillis ()-t); System. out. println ("Result 4:" + ret4 );}}