最大子列和的四种算法比较

本文将会介绍最大子列和的四种不同复杂度算法。
算法一:暴力枚举法,算法复杂度为O(N三次方)

int maxsequence1(int A[], int N)
{
    int ThisSum , MaxSum=0;
    int i, j, k;
    for (i = 0; i <= N; i++) {

        for (j = i; j <= N; j++) {
            ThisSum = 0;
            for (k = i; k <= j; k++) 
                ThisSum += A[k];
                if (ThisSum > MaxSum)
                    MaxSum = ThisSum;

        }
    }

    return MaxSum;
}

算法二:算法一的改进,减少一个for循环,复杂度为O(N平方)

int maxsequence2(int A[], int N)
{
    int ThisSum, MaxSum = 0;
    int i, j;
    for (i = 0; i <= N; i++) {
        ThisSum = 0;
        for (j = i; j <= N; j++) {
            ThisSum += A[j];
            if (ThisSum > MaxSum)
                MaxSum = ThisSum;
        }
    }
    return MaxSum;
}

算法三:分而治之。算法复杂度为O(NlogN)

int Max(int A, int B, int C)
{
    /*return A > B ? A > C ? A :C: B > C ? B : C;*/
    if (A > B) {
        if (A > C)
            return A;
        else
            return C;
    }
    else if (B > C)
        return B;
    else return C;

}
int DivideAndConquer(int List[], int left, int right) {
    int MaxLeftSum, MaxRightSum;
    int MaxLeftBoardSum, MaxRightBoardSum;
    int LeftBoardSum, RightBoardSum;
    int center,i;
    /*递归终止条件*/
    if (left == right) {
        if (List[left] > 0)
            return List[left];
        else
            return 0;
    }


    center = (right + left) / 2;
    MaxLeftSum = DivideAndConquer(List, left, center);
    MaxRightSum= DivideAndConquer(List, center+1, right);

    MaxLeftBoardSum = 0; LeftBoardSum = 0;
    for (i = center;i >= left; i--)
        LeftBoardSum += List[i];
    if (LeftBoardSum > MaxLeftBoardSum)
        MaxLeftBoardSum = LeftBoardSum;
    MaxRightBoardSum = 0; RightBoardSum = 0;

    for (i = center + 1; i <= right; i++)
        RightBoardSum += List[i];
    if (RightBoardSum > MaxRightBoardSum)
        MaxRightBoardSum = RightBoardSum;

    return Max(MaxLeftSum, MaxRightSum, MaxRightBoardSum + MaxLeftBoardSum);


}
int maxsequence3(int A[], int N)
{
    return DivideAndConquer(A, 0, N-1);
}

算法四:在线处理算法,代码最简单,且复杂度最低,为O(N)

int maxsequence4(int A[], int N)
{
    int ThisSum,MaxSum ;
    int i;
    ThisSum = MaxSum = 0;
    for (i = 0; i <= N; i++) {
        ThisSum += A[i];
        if (ThisSum > MaxSum)
            MaxSum = ThisSum;
        else if (ThisSum < 0)
            ThisSum = 0;
    }
    return MaxSum;


}

主程序如下

#include 
int maxsequence1(int A[], int N);
int maxsequence2(int A[], int N);
int maxsequence3(int A[], int N);
int maxsequence4(int A[], int N);
int main(void)
{
    int K,j=0;
    int a,max;
    int num[100000];
        printf("请输入数字个数:\n");
        scanf("%d", &K);
        printf("请输入%d个数字\n", K);
        for (a = 0; a < K; a++) {
            scanf("%d", &num[a]);
            if (num[a] < 0)
                j++;
        }
        if (j == K)
            printf("0\n");
        else {
            /*用哪个选哪个*/
            max = maxsequence1(num, K);
            max = maxsequence2(num, K);
            max = maxsequence3(num, K);
            max = maxsequence4(num, K);
            printf("最大子列和为:%d\n", max);
        }

}

以上即为求最大子列和的四种算法。

你可能感兴趣的:(数据结构(c实现))