PackageALG;/*** Find the largest sub-series **/ Public classSubmaxarray { Public Static voidMain (string[] args) {int[] A =New int[] {1,-2, 3, 10,-4, 7, 2,-5 }; int[] B =New int[] {-6, 2, 4,-7, 5, 3, 2,-1, 6,-9, 10,-2 }; intMax =Maxsum (A, a.length); SYSTEM.OUT.PRINTLN (max); intMAXB =Maxsum (b, b.length); System.out.println (MAXB); LongWmax =MaxSubSum4 (a); System.out.println (Wmax); LongMALXB =MaxSubSum4 (b); System.out.println (MALXB); } //the cycle of violence Static intMaxsum (int[] A,intN) {intMaximum =Integer.min_value; intsum = 0; for(inti = 0; I < n; i++) { for(intj = i; J < N; J + +) { for(intK = i; K <= J; k++) {sum+=A[k]; } if(Sum >maximum) {Maximum=sum; } Sum= 0;//remember to clear the zeros here, otherwise the sum of all the sub-arrays is finally stored. } } returnmaximum; } //Omit duplicate Calculations Static intMaxsummodify (int[] a) {intMaxsum = 0; for(inti = 0; i < a.length; i++) { intThissum = 0; for(intj = i; J < A.length; J + +) {thissum+=A[j]; if(Thissum >maxsum) Maxsum=thissum; } } returnmaxsum; } /*** The largest sub-series of the divide-and- conquer algorithm, in the first half, in the second half or from the middle of the calculation **/ Static LongMaxsumrec (int[] A,intLeftintRight ) { if(left = =Right ) { if(A[left] > 0) returnA[left]; Else return0; } intCenter = (left + right)/2; LongMaxleftsum =Maxsumrec (A, left, center); LongMaxrightsum = Maxsumrec (A, center + 1, right); //find the maximum value of the sequence ending with the last digit on the left LongMaxleftbordersum = 0, leftbordersum = 0; for(inti = center; I >= left; i--) {leftbordersum+=A[i]; if(Leftbordersum >maxleftbordersum) Maxleftbordersum=leftbordersum; } //find the maximum value of the sequence ending with the next number on the right LongMaxrightbordersum = 0, rightbordersum = 0; for(intj = center + 1; J <= Right; J + +) {rightbordersum+=A[j]; if(Rightbordersum >maxrightbordersum) Maxrightbordersum=rightbordersum; } returnmax3 (Maxleftsum, maxrightsum, Maxleftbordersum+maxrightbordersum); } Static LongMAXSUBSUM3 (int[] a) {returnMaxsumrec (A, 0, a.length-1); } Static LongMAX3 (LongALongBLongc) {if(A <b) {a=b; } if(A >c)returnA; Else returnC; } //linear algorithm O (N) /*** The basis of the algorithm: if A[i] is a negative number, then he cannot be the beginning of the maximal subsequence, the inference is that the maximum number of substrings of the beginning of the array and cannot be a negative number, * that is, a negative number of the subsequence can not be the beginning of the largest sub-sequence. * */ Static LongMAXSUBSUM4 (int[] a) {LongMaxsum = 0, thissum = 0; for(intj = 0; J < A.length; J + +) {thissum+=A[j]; if(Thissum >maxsum) Maxsum=thissum; Else if(Thissum < 0) Thissum= 0; } returnmaxsum; } /*** If all negative, the largest subsequence is 0 for one element, * or the largest negative number? * */ Static intMaxsum (int[] a) {intmax = a[0];//full negative condition, return maximum number intsum = 0; for(intj = 0; J < A.length; J + +) { if(Sum >= 0) {//If you add an element, sum>=0, addSum + =A[j]; }Else{sum= A[j];//If you add an element to the sum<0, you don't add } if(Sum >max) Max=sum; } returnMax; }}
Maximum sub-sequence