forked from gdsc-bit/Coding-Linguistic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
max_subarray.c
149 lines (143 loc) · 3.79 KB
/
max_subarray.c
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
#include <stdio.h>//Header file for input and output
#include <math.h>
static int max_left, max_right;
// Here structure is created with a name node
struct m_subarr
{
int low;
int high;
int maxsum;
};
struct m_subarr maxcrossingsum(int a[], int low, int mid, int high)
{
struct m_subarr arrr;
double left_sum = -INFINITY;
int sum = 0, i, j, k, finalsum = 0, maxs;
for (i = mid; i >= low; i--)
{
sum = sum + a[i];
if (sum > left_sum)
{
left_sum = sum;
max_left = i;
}
}
double right_sum = -INFINITY;
sum = 0;
for (j = mid + 1; j <= high; j++)
{
sum = sum + a[j];
if (sum > right_sum)
{
right_sum = sum;
max_right = j;
}
}
arrr.high = max_right;
arrr.low = max_left;
arrr.maxsum = (left_sum + right_sum);
return arrr;
}
struct m_subarr maxsubarraynaive(int a[], int n)
{
int i, sum, maxsum, j,l,m;
struct m_subarr arrr;
maxsum = a[1];
for (i = 1; i <= n; i++)
{
sum = 0;
for (j = i; j <= n; j++)
{
sum = sum + a[j];
if (sum > maxsum)
{
l = i;
m = j;
maxsum = sum;
}
}
}
arrr.low =l;
arrr.high = m;
arrr.maxsum = maxsum;
printf("low = %d high = %d",arrr.low,arrr.high);
return arrr;
}
struct m_subarr maxsubarraysumdivcon(int a[], int low, int high)
{
int A, B, C;
struct m_subarr left, right, cross;
int mid, leftlow, lefthigh, leftsum, rightlow, righthigh, rightsum, crosslow, crosshigh, crosssum;
if (high == low)
{
left.high = high;
left.low = low;
left.maxsum = a[low];
return left;
}
else
{
mid = (low + high) / 2;
}
left = maxsubarraysumdivcon(a, low, mid);
right = maxsubarraysumdivcon(a, mid + 1, high);
cross = maxcrossingsum(a, low, mid, high);
if ((left.maxsum >= right.maxsum) && (left.maxsum >= cross.maxsum))
{
return left;
}
else if ((right.maxsum >= left.maxsum) && (right.maxsum >= cross.maxsum))
{
return right;
}
else
{
return cross;
}
}
int main()
{
int i, n, row, x, k, maxsum;
struct m_subarr arrr;
printf("Enter Number of Element in the subarray.......: ");
scanf("%d", &n);
int a[n];
for (i = 1; i <= n; i++)
{
printf("Enter elements at %d: ", i);
scanf("%d", &a[i]);
}
do
{
printf("\n----Main Menu-----\n");
printf("1.To Find the maximum subarray using naive approach\n");
printf("2.To Find the maximum subarray using Divide-and-Conquer approach\n");
printf("3.To Exit from the program\n");
printf("Enter your choice: ");
scanf("%d", &x);
switch (x)
{
case 1:
arrr = maxsubarraynaive(a, n);
printf("\n-----Naive Approach-------\n");
printf("\nThe maximum sum is: %d: ", arrr.maxsum);
printf("\nThe maximum subarray is\n");
for (k = arrr.low; k <= arrr.high; k++)
{
printf("a[%d] = %d\n", k, a[k]);
}
break;
case 2:
arrr = maxsubarraysumdivcon(a, 1, n);
printf("\n-----Divide and Conquer approach-----\n");
printf("\nMaximum Sum Of Sub-Array is: %d\n", arrr.maxsum);
printf("\nThe maximum subarray is\n");
for (k = arrr.low; k <= arrr.high; k++)
{
printf("a[%d] = %d\n", k, a[k]);
}
break;
}
} while (x <= 2);
return 0;
}