Algorithm entry: four algorithms (Java) for maximum subsequence and algorithm java
I have recently studied algorithms and Data structures. I recommend a book: Data structures and Algorithm analysis in Java 3rd.
The following four algorithms are from this book:
The four largest subsequences and algorithms:
Problem description
Given (possibly negative) integers a (1), a (2 ),...... A (n), calculate a (1) + a (2) + ...... + The maximum value of a (j. For convenience, if all integers are negative, the maximum subsequence is 0.
That is, to find several consecutive integers in a series of integers, the sum of these integers is the largest.
First, the runtime O (N ^ 3) due to nested three-layer for Loop)
package demo1;public class Demo1 { public static void main(String[] args) { int[] a = { -2, 4, -3, 5, 7, -1, 8, 1 }; int max = maxSubSum1(a); System.out.println(max); // 21 } private static int maxSubSum1(int[] a) { int maxSum = 0; for (int i = 0; i < a.length; i++) { for (int j = i; j < a.length; j++) { int thisSum = 0; for (int k = i; k <= j; k++) { thisSum += a[k]; } if (thisSum > maxSum) { maxSum = thisSum; } } } return maxSum; }}
Solution 2: simplify the process based on the first method, remove a layer of for loop, and run time O (N ^ 2)
package demo1;public class Demo2 { public static void main(String[] args) { int[] a = { -2, 4, -3, 5, 7, -1, 8, 1 }; int max = maxSubSum2(a); System.out.println(max); // 21 } private static int maxSubSum2(int[] a) { int maxSum = 0; for (int i = 0; i < a.length; i++) { int thisSum = 0; for (int j = i; j < a.length; j++) { thisSum += a[j]; if (thisSum > maxSum) { maxSum = thisSum; } } } return maxSum; }}
These two algorithms are essentially similar, and the latter two algorithms will greatly improve efficiency.
Third: the solution concept has completely changed. The time is only O (NlogN)
It divides the number of groups into the first half and the last half, and then processes the two parts separately (divide and conquer)
Obviously: the largest subsequence and must be one of the first or later sections or the middle section, and then use recursive cyclic computing.
Note: The shorter the code, the better, but the algorithm may not be. This algorithm may be long, but it is better than the first two algorithms.
The Code is as follows:
Package demo1; public class Demo3 {public static void main (String [] args) {int [] a = {-2, 4,-3, 5, 7,-1, 8, 1}; int max = maxSubSum3 (a); System. out. println (max); // 21} private static int maxSubSum3 (int [] a) {// recursive initialization parameter return maxSumRec (a, 0,. length-1);} private static int maxSumRec (int [] a, int left, int right) {// determine whether there is only one element if (left = right) {if (a [left]> 0) {return a [left];} else {return 0 ;}} int center = (left + right)/2; int maxLeftSum = maxSumRec (a, left, center); int maxRightSum = maxSumRec (a, center + 1, right); // process int maxLeftBorderSum = 0 on the left side; int leftBoarderSum = 0; for (int I = center; I> = left; I --) {leftBoarderSum + = a [I]; if (leftBoarderSum> maxLeftBorderSum) {maxLeftBorderSum = leftBoarderSum ;}// the right side handles int maxRightBoarderSum = 0; int rightBoarderSum = 0; for (int I = center + 1; I <= right; I ++) {rightBoarderSum + = a [I]; if (rightBoarderSum> maxRightBoarderSum) {maxRightBoarderSum = rightBoarderSum ;}// return the maximum return Math. max (Math. max (maxLeftSum, maxRightSum), maxLeftBorderSum + maxRightBoarderSum );}}
Method 4: The best algorithm: O (N)
This method is clever and hard to come up with. It can only be thought of by programmers who have deep programming skills.
package demo1;public class Demo4 { public static void main(String[] args) { int[] a = { -2, 4, -3, 5, 7, -1, 8, 1 }; int max = maxSubSum4(a); System.out.println(max); // 21 } private static int maxSubSum4(int[] a) { int maxSum = 0; int thisSum = 0; for (int i = 0; i < a.length; i++) { thisSum += a[i]; if (thisSum > maxSum) { maxSum = thisSum; } else if (thisSum < 0) { thisSum = 0; } return maxSum; } return 0; }}