Maximum sub-array problem (to find the maximum and number of contiguous sub-arrays)

Source: Internet
Author: User

In the introduction of < Algorithm > The fourth chapter of the Division strategy (Divider and Conquer) 4.1, the maximum sub-array problem is proposed. Its transformation is to find the maximum and the array a={1, 2, 3, 10,-4, 7, 2,-5} in the contiguous subarray.

For this problem, one can think of a brute-force solution: simply try to sum all possible combinations. An array of n exists in a n-1/2 combination, and then each combination is summed. That is, three for loop traversal, the number of each subarray in the array, and finally find out the maximum value of these sub-arrays. Sum[i,..., J] The sum of the elements of element A to the J element and (wherein 0<=i<=j<n), traversing all possible sum[i,..., J], then the time complexity (maximum runtime running time) is 0 (n^3).

1  Packagealgorithms;2 3 /**4  * @author Public5 * The code of this section refers to the beauty of programming6  */7  Public classMaxsubarraysum {8     9     Static int[] A = {1,-2, 3, 10,-4, 7, 2,-5};Ten     Static intsum; One     Static intMaxsum = 0; A      Public Static voidMain (string[] args) { -         //TODO auto-generated Method Stub -          for(inti = 0; i < a.length; i++) { the              for(intj = i; J < A.length; J + +) { -                  for(intK = i; K <= J; k++) { -Sum + =A[k]; -                 } +                 if(Sum >maxsum) { -Maxsum =sum;  +                 } Asum = 0;//Both Sum>maxsum and sum<=maxsum have to clear the sum 0 operation at             } -         } -          -System.out.println ("maxsum=" +maxsum); -          -     } in  -}

Operation Result:

Maxsum=18

In the introduction to the algorithm 4.1-5 exercises refer to the following ideas: maximum word group problem design a non-recursive, linear time algorithm. Starting from the left edge of the array, processing from left to right, records the maximum number of word groups that have been processed so far. If the largest subarray of A[1,..., J] is known, the solution is extended to the largest subarray of A[1,..., J+1] based on the following properties: The largest subarray of A[1,..., j+1] is either the largest sub-array of a[1,..., J] or a sub-array a[i,..., j+1] ( 1≤i≤j+1). In the case of the largest subarray of known A[1,..., J], it is possible to find the largest sub-array of shapes such as A[i,..., j+1] in linear time .

Implementation method:

1  Packagealgorithms;2 3  Public classFindmaxsubarray {4     5     Static int[] Array = {1,-2, 3, 10,-4, 7, 2,-5};6 7      Public Static voidMain (string[] args) {8 maxsum (array);9     }Ten  One     Private Static voidMaxsum (int[] arraytmp) { A         //TODO auto-generated Method Stub -         if(arraytmp==NULL) { -             return; the         } -          -         intCursum = 0, maxsum = 0; -          +         intIndexstart = 0, indexend = 0;//Initialize the maximum and subscript of the word set -          +          for(intj = 0; J < Arraytmp.length; J + +) { ACursum + = Arraytmp[j];//Accumulate at              -             if(Cursum < 0) {//current and less than 0, reset to 0 -Cursum = 0; -Indexstart = j+1;//Adjust the maximum and start subscript of the word set -             } -              in             if(Cursum > Maxsum) {//current and greater than maximum and, then reset Max and -Maxsum =cursum; toIndexend = j;//adjust the maximum and end subscript of a word set +             } -              the         } *          $         if(Maxsum = = 0) {//maximum and remains 0, indicating that all elements in the array are negativePanax NotoginsengMaxsum = arraytmp[0]; -Indexstart = Indexend = 0;//Initialize sub-array max and subscript the              for(intj = 0; J < Arraytmp.length; J + +) { +                 if(Arraytmp[j] >maxsum) { AMaxsum =Arraytmp[j]; theIndexend = Indexstart = j;//Adjust Sub-array maximum and subscript +                 } -             } $         } $          -          for(inti = Indexstart; I <= indexend; i++) { -System.out.print (arraytmp[i]+ "");//output maximum and sub-arrays the         } - System.out.println ();WuyiSystem.out.println ("maxsum=" +maxsum);//output sub-array Max and the     } -  Wu}

Operation Result:

3 10-4 7 2 maxsum=18

Maximum sub-array problem (to find the maximum and number of contiguous sub-arrays)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.