Implement both the brute-force and recursive algorithms for the maximum-subarray problem on your own computer. What problem size gives the crossover point at which the recursive algorithm beats the brute-force algorithm? Then, change the base case of the recursive algorithm to use the brute-force algorithm whenever the problem size is less than . Does that change the crossover point?

Here is the data collected in my computer (size is the number of elements processed and times are in seconds):

Size Brute Force Recursive
2 0.050000 0.090000
3 0.080000 0.180000
4 0.120000 0.180000
5 0.080000 0.170000
6 0.110000 0.220000
7 0.150000 0.270000
8 0.210000 0.310000
9 0.260000 0.370000
10 0.320000 0.420000
11 0.370000 0.490000
12 0.450000 0.550000
13 0.540000 0.600000
14 0.570000 0.660000
15 0.660000 0.700000
16 0.740000 0.750000
17 0.810000 0.800000

So, the value of is 17 in my computer. However, it varies a bit. I have noticed it is mostly 17 but sometimes takes any value between 17 and 25.

If we modify the the base case of the recursive algorithm to use the brute-force algorithm whenever the problem size is less than 17, the crossover point doesn’t change significantly.


Here is the code written in c to get these results.

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
#include <time.h>
#include <stdio.h>
#include <limits.h>

#define CROSSOVER   17

/* structure to store the result */
typedef struct {
    int left;
    int right;
    int sum;
} max_subarray_tuple;

/* brute force method */
max_subarray_tuple find_max_subarray_bf(int A[], int low, int high)
{
    int i, j, temp_sum;
    max_subarray_tuple ret = {0, 0, INT_MIN};
    
    for (i = low; i < high; i++) {
        temp_sum = 0;
        for (j = i; j < high; j++) {
            temp_sum += A[j];
            if (temp_sum > ret.sum) {
                ret.sum = temp_sum;
                ret.left = i;
                ret.right = j + 1;
            }
        }
    }
    return ret;
}

/* function for finding crossing maximum sub-array */
max_subarray_tuple find_max_crossing_subarray(int A[], int low, int mid, int high)
{
    int i, j;
    max_subarray_tuple ret = {0, 0, 0};
    int left_sum = INT_MIN;
    int right_sum = INT_MIN;
    int sum = 0;
    
    for (i = mid - 1; i >= low; i--) {
        sum += A[i];
        if (sum > left_sum) {
            left_sum = sum;
            ret.left = i;
        }
    }
    
    sum = 0;
    for (j = mid; j < high; j++) {
        sum += A[j];
        if (sum > right_sum) {
            right_sum = sum;
            ret.right = j + 1;
        }
    }
    ret.sum = left_sum + right_sum;
    
    return ret;
}

/* recursive method */
max_subarray_tuple find_max_subarray_rc(int A[], int low, int high)
{
    if (high == low + 1) {
        max_subarray_tuple ret = {low, high, A[low]};
        return ret;
    } else {
        int mid = (low + high) / 2;
        max_subarray_tuple left = find_max_subarray_rc(A, low, mid);
        max_subarray_tuple right = find_max_subarray_rc(A, mid, high);
        max_subarray_tuple cross = find_max_crossing_subarray(A, low, mid, high);
        
        if (left.sum >= right.sum && left.sum >= cross.sum)
            return left;
        else if (right.sum >= left.sum && right.sum >= cross.sum)
            return right;
        else
            return cross;
    }
}

/* modified mixed method */
max_subarray_tuple find_max_subarray_mx(int A[], int low, int high)
{
    if (high - low < CROSSOVER) {
        return find_max_subarray_bf(A, low, high);
    } else {
        int mid = (low + high) / 2;
        max_subarray_tuple left = find_max_subarray_rc(A, low, mid);
        max_subarray_tuple right = find_max_subarray_rc(A, mid, high);
        max_subarray_tuple cross = find_max_crossing_subarray(A, low, mid, high);
        
        if (left.sum >= right.sum && left.sum >= cross.sum)
            return left;
        else if (right.sum >= left.sum && right.sum >= cross.sum)
            return right;
        else
            return cross;
    }
}

/* driver code to measure performance */
int main()
{
    int i, n = 5, flag = 0;
    int Arr[100] = {20, -21, 43, -23, -92, 45, -56, -5, 34, -17,
                    34, 65, 89, -109, 125, 2, -10, 89, 46, 65, -49, 
                    3, -45, 34, 76, 32, -76, -2, 3, -45, 44, 34, 67, 
                    -67, 99, -104, 11, -37, 44, 76, -90, 89, -32, 34, 
                    88, 56, -6, -89, -90, -34, -56, 23, 29, 2, 6, 9, 
                    2, -34, -45, 34, 22, -177, 44, 90, -45, -36, 55, 
                    23, 56, -56, -167, -54, 23, 45, 14, 62, -46, -56, 
                    -34, 45, 32, 20, -87, 39, 82, 95, -67, -45, 88, 
                    -36, 21, 18, 16, 81, -102, 99, -45, -67, -45, -76};
    
    clock_t start, stop;
    double time_bf, time_rc;

    printf("Size BruteForce Recursive Mixed\n");
    printf("-------------------------------\n");
    while(!flag) {
        start = clock();
        for (i = 0; i < 1000000; i++) {
            max_subarray_tuple R1 = find_max_subarray_bf(Arr, 0, n);
        }
        stop = clock();
        time_bf = ((double) (stop - start)) / CLOCKS_PER_SEC;
        
        start = clock();
        for (i = 0; i < 1000000; i++) {
            max_subarray_tuple R2 = find_max_subarray_rc(Arr, 0, n);
        }
        stop = clock();
        time_rc = ((double) (stop - start)) / CLOCKS_PER_SEC;
        
        n++;
        if (time_bf > time_rc) {
            flag = 1;
        }
    }

    return 0;
}
If you have any question or suggestion or you have found any error in this solution, please leave a comment below.