This paper introduces the algorithm of Divide and Conquer (Divide-and-conquer) in the way of solving maximal subsequence and
Divide and conquer algorithm is a relatively fast algorithm running time O (LOGN)
The maximum number of sub-sequences and problems are as follows:
Give a set of integersA1 A2 ...AN
Please∑jk=i Ak
If all integers are negative then the maximum subsequence and 0
e.g. input-2, 11,-4, 13,-5,-2 output 20 ( a2 to a4)
The divide-and-conquer algorithm is the same as the literal description of the first division of Treatment
Refers to the problem of dividing the question into two parts almost the same sub-problem of recursive solution
The solution is to combine the solutions of the points by simple means to obtain the final solution.
For the above example, the series can be divided into two parts:
-2, 11,-4 (later referred to as left) and 13,-5,-2 (later referred to as the right part)
In this way, the occurrence of the maximum subsequence is 3 cases (which need to be asked separately in the program).
- All appear in the left section
- All appear in the right section
- Part of the left part is in the right part of the other part
For a third special case, you can include the last element of the left part and the sub-sequence of the first element of the right part, and the left part is linked together
Next solve the recursion problem
- In order to handle recursive input functions, we need to receive the position of the left and right boundary, then the left and right boundary is 0 and N-1 the first time the function is called.
The most important thing is the basic situation: when the left and right edges return the element of this overlapping position (of course, if it is greater than 0)
That's basically it.
On the Code
#include <stdio.h>int imax (int a, int b, int c) {return (A>B?A:B) >c? ( A>B?A:B): C;} static int maxsubsum (const int a[], int left, int. right) {if (left = right) {if (A[left] > 0) return A[left]; else return 0; } int Center = (left + right)/2; int maxleftsum = Maxsubsum (A, left, Center); 1 int maxrightsum = Maxsubsum (A, center+1, right); 2 int maxleftbordersum = 0, leftbordersum = 0; 3 for (int i = Center, I >= left; i--) {leftbordersum + = A[i]; if (Leftbordersum > maxleftbordersum) maxleftbordersum = leftbordersum; } int maxrightbordersum = 0, rightbordersum = 0; for (int i = center+1, I <= right; i++) {rightbordersum + = A[i]; if (Rightbordersum > maxrightbordersum) maxrightbordersum = rightbordersum; }//4 return Imax (maxleftSum, Maxrightsum, Maxleftbordersum + maxrightbordersum);} int maxsubsequencesum (const int a[], int N) {return maxsubsum (A, 0, N-1);} int main () {int a[] = {-2, 11,-4, 13,-5,-2}; printf ("%d\n", Maxsubsequencesum (A, sizeof (a)/sizeof (A[0]))); return 0;}
1 is the case that all appears in the left part
2 is the case that all appears in the right section
It's a third case.
And then we'll compare it to find the maximum value.
The book says
Short program does not mean that the program is good
The advantage of this program is that fast
My csdn:http://blog.csdn.net/oblivion1221.
"Data structure and algorithm analysis C language description" Reading notes--Divide and conquer algorithm