当前位置:   article > 正文

求解最大子序列算法及比较_给出求解最大单调子序列问题的算法

给出求解最大单调子序列问题的算法
  1. #include <iostream>
  2. #include <stdlib.h>
  3. #include <time.h>
  4. #include "../StopWath.h"
  5. using namespace std;
  6. int A[2000];
  7. int N = sizeof(A) / sizeof(A[0]);
  8. int Sum1, Sum2, Sum3, Sum4;
  9. CStopWatch stopwatch;
  10. double time1, time2, time3, time4;
  11. int MaxSubsequenceSum1(const int A[], int N);
  12. int MaxSubsequenceSum2(const int A[], int N);
  13. int Max3(int a, int b, int c);
  14. static int MaxSubNum(const int A[], int left, int right);
  15. int MaxSubsequenceSum3(const int A[], int N);
  16. int MaxSubsequenceSum4(const int A[], int N);
  17. int main(int argc, char** argv)
  18. {
  19. srand((unsigned)time(NULL));
  20. for (int i = 0; i < N; i++)
  21. A[i] = (rand() % 100) - 150;
  22. Sum1 = MaxSubsequenceSum1(A, N);
  23. Sum2 = MaxSubsequenceSum2(A, N);
  24. Sum3 = MaxSubsequenceSum3(A, N);
  25. Sum4 = MaxSubsequenceSum4(A, N);
  26. cout << Sum1 << " time: " << time1 << endl;
  27. cout << Sum2 << " time: " << time2 << endl;
  28. cout << Sum3 << " time: " << time3 << endl;
  29. cout << Sum4 << " time: " << time4 << endl;
  30. system("pause");
  31. return 0;
  32. }
  33. int MaxSubsequenceSum1(const int A[], int N)
  34. {
  35. int ThisSum, MaxSum, i, j, k;
  36. stopwatch.Start();
  37. MaxSum = 0;
  38. for (i = 0; i < N; i++)
  39. {
  40. for (j = i; j < N; j++)
  41. {
  42. ThisSum = 0;
  43. for (k = i; k <= j; k++)
  44. ThisSum += A[k];
  45. if (ThisSum > MaxSum)
  46. MaxSum = ThisSum;
  47. }
  48. }
  49. time1 = stopwatch.Stop();
  50. return MaxSum;
  51. }
  52. int MaxSubsequenceSum2(const int A[], int N)
  53. {
  54. int ThisSum, MaxSum, i, j;
  55. stopwatch.Start();
  56. MaxSum = 0;
  57. for (i = 0; i < N; i++)
  58. {
  59. ThisSum = 0;
  60. for (j = i; j < N; j++)
  61. {
  62. ThisSum += A[j];
  63. if (ThisSum > MaxSum)
  64. MaxSum = ThisSum;
  65. }
  66. }
  67. time2 = stopwatch.Stop();
  68. return MaxSum;
  69. }
  70. static int MaxSubNum(const int A[], int left, int right)
  71. {
  72. int MaxLeftSum, MaxRightSum;
  73. int MaxLeftBorderSum, MaxRightBorderSum;
  74. int LeftBorderSum, RightBorderSum;
  75. int Center, i;
  76. if (left == right)
  77. {
  78. if (A[left] > 0)
  79. return A[left];
  80. else
  81. return 0;
  82. }
  83. Center = (left + right) / 2;
  84. MaxLeftSum = MaxSubNum(A, left, Center);
  85. MaxRightSum = MaxSubNum(A, Center + 1, right);
  86. MaxLeftBorderSum = 0;
  87. LeftBorderSum = 0;
  88. for (i = Center; i >= left; i--)
  89. {
  90. LeftBorderSum += A[i];
  91. if (LeftBorderSum > MaxLeftBorderSum)
  92. MaxLeftBorderSum = LeftBorderSum;
  93. }
  94. MaxRightBorderSum = 0;
  95. RightBorderSum = 0;
  96. for (i = Center + 1; i <= right; i++)
  97. {
  98. RightBorderSum += A[i];
  99. if (RightBorderSum > MaxRightBorderSum)
  100. MaxRightBorderSum = RightBorderSum;
  101. }
  102. return Max3(MaxLeftSum, MaxRightSum, (MaxLeftBorderSum + MaxRightBorderSum));
  103. }
  104. int Max3(int a, int b, int c)
  105. {
  106. return (a > b) ? ((a > c) ? a : c) : ((b > c) ? b : c);
  107. }
  108. int MaxSubsequenceSum3(const int A[], int N)
  109. {
  110. int MaxNum = 0;
  111. stopwatch.Start();
  112. MaxNum = MaxSubNum(A, 0, N - 1);
  113. time3 = stopwatch.Stop();
  114. return MaxNum;
  115. }
  116. int MaxSubsequenceSum4(const int A[], int N)
  117. {
  118. int ThisSum, MaxSum, j;
  119. ThisSum = MaxSum = 0;
  120. stopwatch.Start();
  121. for (j = 0; j < N; j++)
  122. {
  123. ThisSum += A[j];
  124. if (ThisSum > MaxSum)
  125. MaxSum = ThisSum;
  126. else if (ThisSum < 0)
  127. ThisSum = 0;
  128. }
  129. time4 = stopwatch.Stop();
  130. return MaxSum;
  131. }




第一种算法最慢,其次第二、第三种算法,最快的是第四中算法。
算法的详细描述见《数据结构与算法分析 C语言描述》

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/煮酒与君饮/article/detail/1007447
推荐阅读
相关标签
  

闽ICP备14008679号