最大子序列问题程序实现算法---N的三次方复杂度

这个代码是《数据结构与算法C语言实现》的第2章的练习,代码如下:

#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<iostream>

#define N 5000
using namespace std;


long int max3(long int a, long int b, long int c)
{
  return max(max(a, b), c);
}

// methed three algorithm
long int maxSubSum(const int A[], long int left, long int right)
{
  if (left == right)
  {
    if (A[left] < 0)
      return 0;
    else
      return A[left];
  }
  // cout<< "sub sequence is "<<endl;
  // for(int i = left; i <= right; i++)
  // {
  //   cout<<A[i]<<endl;
  // }

  long int center = (left + right) / 2;
  // cout << "left is " << left << endl;
  // cout << "right is " << right <<endl;
  // cout << "center is " << center << endl <<endl;

  long int maxLeftSum = maxSubSum(A, left, center);
  long int maxRightSum = maxSubSum(A, center + 1, right);

  long int tempLeftSum = 0; long int leftBorderSum = 0;
  for(long int i = center; i >= left; i--)
  {
    tempLeftSum += A[i];
    if (leftBorderSum < tempLeftSum)
    {
      leftBorderSum = tempLeftSum;
    }
  }

  long int tempRightSum = 0; long int rightBorderSum = 0;
  for(long int i = center + 1; i <= right; i++)
  {
    tempRightSum += A[i];
    if (tempRightSum > rightBorderSum)
    {
      rightBorderSum = tempRightSum;
    }
  }

  return max3(maxLeftSum, maxRightSum, rightBorderSum + leftBorderSum);

}

int main() {

  // generate a sequence with 50 integers
  int * sequence = (int *)malloc(sizeof(int)*N);
  // printf("sequence is ");
  for(int i = 0; i < N; i++)
  {
    *(sequence + i) = (int) (rand() % 100) * pow(-1.0 , i);
    // printf("%d ", *(sequence + i));
  }  
  // printf("\n");
  long int maxSum = 0;
  int startIndex = 0;
  int endIndex = 0;

  /*
  * mothed one
  */
  printf("======================= mothed one =========================\n");
  // get k sub sequence which sum is maximum
  // int * maxSubSequence = (int *)malloc(sizeof(int)*k);

  for(int i = 0; i < N; i++)
  {
    for(int j = i; j < N; j++)
    {
      long int tempSum = 0;
      for(int m = i; m <= j; m++)
      {
        tempSum += *(sequence + m);
      }

      if (maxSum < tempSum)
      {
        maxSum = tempSum;
        startIndex = i;
        endIndex = j;
      }
    }
  }

  // print the sum and the sequence
  printf("\n the sum is %d \n", maxSum);
  // printf("and the sequence is ");
  // for (int i = startIndex; i <= endIndex; i++)
  // {
  //   printf("%d ",*(sequence + i));
  // }
  // printf("\n");


  /*
  * mothed tree
  */
  printf("======================= mothed three =========================\n");
  long int finalSum = maxSubSum(sequence, 0, N - 1);
  printf("\n the sum is %d \n" , finalSum);


  /*
  * mothed two
  */
  printf("======================= mothed two =========================\n");

  maxSum = 0;
  startIndex = 0;
  endIndex = 0;

  for(int i = 0 ; i < N ; i++)
  {
    int tempSum = 0;
    for(int j = i; j < N; j++ )
    {
      tempSum += *(sequence + j);

      if (tempSum > maxSum)
      {
        maxSum = tempSum;
        startIndex = i;
        endIndex = j;
      }
    }
  }

  // print the sum and the sequence
  printf("\n the sum is %d \n", maxSum);
  // printf("and the sequence is  ");
  // for (int i = startIndex; i <= endIndex; i++)
  // {
  //   printf("%d ",*(sequence + i));
  // }
  // printf("\n");

  
  printf("==================  mothed four ==========================\n");

  long int thisSum = 0;
  maxSum = 0;
  for(int i = 0; i < N; i++)
  {
    thisSum += *(sequence + i);

    if (thisSum > maxSum)
    {
      maxSum = thisSum;
    }
    else if( thisSum < 0)
    {
      thisSum = 0;
    }
    
  }
  printf("\n the sum is %d \n", maxSum);


}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值