1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
|
#include <stdio.h> #include <stdlib.h> #include <limits.h> #include <windows.h>
struct MAX_SUB_ARRAY; typedef struct MAX_SUB_ARRAY max_subarray;
struct MAX_SUB_ARRAY{ int Low, High; double Sum; };
max_subarray *Find_Max_Crossing_Subarray(int *A, int Low, int Mid, int High) { int i; max_subarray *Result = (max_subarray *)calloc(1, sizeof(struct MAX_SUB_ARRAY)); int sum = 0, Left_Sum = INT_MIN, Right_Sum = INT_MIN;
for (i = Mid; i >= Low; i--) { sum += A[i]; if (sum > Left_Sum) { Left_Sum = sum; Result->Low = i; } }
sum = 0; for (i = Mid + 1; i <= High; i++) { sum += A[i]; if (sum > Right_Sum) { Right_Sum = sum; Result->High = i; } }
Result->Sum = Left_Sum + Right_Sum;
return Result; }
max_subarray *Find_Max_Subarray(int *A, int Low, int High) { max_subarray *Result = (max_subarray *)calloc(1, sizeof(struct MAX_SUB_ARRAY)); max_subarray *LowResult, *HighResult, *MidResult; int Mid;
if (Low == High || (Low < 0 && High < 0)) { Result->Low = Low; Result->High = High; Result->Sum = A[Low]; return Result; } else { Mid = (Low + High) / 2; LowResult = Find_Max_Subarray(A, Low, Mid); MidResult = Find_Max_Crossing_Subarray(A, Low, Mid, High); HighResult = Find_Max_Subarray(A, Mid + 1, High); if(LowResult->Sum >= MidResult->Sum && LowResult->Sum >= HighResult->Sum) return LowResult; else if(MidResult->Sum >= LowResult->Sum && MidResult->Sum >= HighResult->Sum) return MidResult; else return HighResult; } }
max_subarray *Traverse_Solution(int *A, int Low, int High) { int i, j; int sum = 0, sum_Last = 0;; max_subarray *Result = (max_subarray *)calloc(1, sizeof(struct MAX_SUB_ARRAY));
Result->Sum = INT_MIN;
if (Low < 0 && High < 0) { Result->Low = -1; Result->High = -1; Result->Sum = 0; return Result; }
for (i = Low; i <= High; i++) { for (j = i; j <= High; j++) { sum = sum_Last + A[j]; sum_Last = sum; if (sum > Result->Sum) { Result->Sum = sum; Result->Low = i; Result->High = j; } } sum_Last = 0; } return Result; }
max_subarray *Caterpiller_Linear_Solution(int *A, int Low, int High) { max_subarray *Result = (max_subarray *)calloc(1, sizeof(struct MAX_SUB_ARRAY));
Result->Low = Low; Result->High = Low; Result->Sum = A[Low];
int i, wall = A[Low], tmp_Low = Low;
for (i = Low + 1; i <= High; i++) { if (wall > 0) { wall += A[i]; } else { wall = A[i]; tmp_Low = i; } if (Result->Sum < wall) { Result->Sum = wall; Result->High = i; Result->Low = tmp_Low; } } return Result; }
int main() { int Seed = 2020; int Len = 500; int DoubleTop = 500; int *Test = (int *)calloc(Len, sizeof(int)); int i; printf("Test:"); for (i = 0; i < Len; i++) { Test[i] = rand() % DoubleTop - DoubleTop / 2; printf("%d ", Test[i]); } putchar('\n');
SYSTEMTIME st, ed; long long interval; max_subarray *Result = (max_subarray *)calloc(1, sizeof(struct MAX_SUB_ARRAY)); GetSystemTime(&st); Result = Find_Max_Subarray(Test, 0, Len - 1); GetSystemTime(&ed); interval = (ed.wMilliseconds - st.wMilliseconds) + (ed.wSecond - st.wSecond) * 1000; printf("Max Subarray (div):\n"); printf("\tLow: %d\n\tHigh: %d\n\tSum: %lf\n\tInterval: %lld\n", Result->Low, Result->High, Result->Sum, interval); GetSystemTime(&st); Result = Traverse_Solution(Test, 0, Len - 1); GetSystemTime(&ed); interval = (ed.wMilliseconds - st.wMilliseconds) + (ed.wSecond - st.wSecond) * 1000; printf("Max Subarray (tra):\n"); printf("\tLow: %d\n\tHigh: %d\n\tSum: %lf\n\tInterval: %lld\n", Result->Low, Result->High, Result->Sum, interval); GetSystemTime(&st); Result = Caterpiller_Linear_Solution(Test, 0, Len - 1); GetSystemTime(&ed); interval = (ed.wMilliseconds - st.wMilliseconds) + (ed.wSecond - st.wSecond) * 1000; printf("Max Subarray (Cal):\n"); printf("\tLow: %d\n\tHigh: %d\n\tSum: %lf\n\tInterval: %lld\n", Result->Low, Result->High, Result->Sum, interval); return 0; }
|