-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathMinMaxInArray.cpp
217 lines (160 loc) · 3.46 KB
/
MinMaxInArray.cpp
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#include<iostream>
using namespace std;
/* Program to find min-max in an array
1) linear method
2)Divide and conquer
*/
struct minMax{
int mini;
int maxi;
} p;
//Method-1 Linear method with linear comparisons
struct minMax findMinMax(int *arr,int low,int high)
{
p.maxi = arr[0];
p.mini = arr[0];
//if size of array is 1
if(low==high)
{
p.mini = p.maxi = arr[0];
return p;
}
//if size of arr is 2
if(arr[0]>arr[1])
{
p.maxi = arr[0];
p.mini = arr[1];
}
else{
p.maxi = arr[1];
p.mini = arr[0];
}
//otherwise simply search for minimum and maximum
for(int i = 2 ; i < high ; i++)
{
if(arr[i] < p.mini)
{
p.mini = arr[i];
}
if(arr[i] > p.maxi)
{
p.maxi = arr[i];
}
}
return p;
}
//TIME COMPLEXITY = O(n),In the above implementation,
//worst case occurs when elements are sorted in descending order and best case occurs when elements are sorted in ascending order
//Method 2 - DIVIDE AND CONQUER based on TOURNAMENT METHOD
//Divide the array into two parts and compare the maximums and minimums of the the two parts to
//get the maximum and the minimum of the the whole array.
struct minMax FindMaxMinDivide(int *arr,int low,int high)
{
struct minMax p,mml,mmr;
//if size of array is 1
if(low==high)
{
p.maxi = p.mini = arr[low];
return p;
}
//if size of array is 2
if(high == low+1)
{
if(arr[low] < arr[high])
{
p.maxi = arr[high];
p.mini = arr[low];
}
else
{
p.maxi = arr[low];
p.mini = arr[high];
}
return p;
}
//otherwise find the middle index.
int mid = low + (high-low)/2;
mml = FindMaxMinDivide(arr,low,mid);//left half
mmr = FindMaxMinDivide(arr,mid+1,high);//right half
//now comparing the minimums in left and right subarrays
if(mml.mini < mmr.mini)
{
p.mini = mml.mini;
}
else
p.mini = mmr.mini;
//comparing the maximums in left and right halves
if(mml.maxi > mmr.maxi)
p.maxi = mml.maxi;
else
p.maxi = mmr.maxi;
return p;
} //TIME COMPLEXITY = 2T(n/2) + 2 and after solving this recurrence relation T(n) = O(logn)
/*Method 3- compare in pairs-
algorithm-
If n is odd then initialize min and max as first element.
If n is even then initialize min and max as minimum and maximum of the first two elements respectively.
For rest of the elements, pick them in pairs and compare their
maximum and minimum with max and min respectively.
*/
struct minMax minMaxCompare(int *arr,int n)
{
struct minMax m;
int i=0;
//if array has even number of elements
if(n%2==0)
{
if(arr[0]>arr[1])
{
m.maxi = arr[0];
m.mini = arr[1];
}
else
{
m.maxi = arr[1];
m.mini = arr[0];
}
//set i = 2 for the loop
i = 2;
}
//if odd no of elements-
/* If array has odd number of elements then
initialize the first element as minimum and
maximum */
else
{
m.maxi = arr[0];
m.mini = arr[0];
i=1; //setting the starting index for loop
}
while(i < n-1)
{
if(arr[i] > arr[i+1])
{
if(arr[i] > m.maxi)
{
m.maxi = arr[i];
}
if(arr[i+1] < m.mini)
m.mini = arr[i+1];
}
else
{
if(arr[i+1] > m.maxi)
m.maxi = arr[i+1];
if(arr[i] < m.mini)
m.mini = arr[i];
}
i = i + 2;
}
return m;
}
int main()
{
int arr[] = {1000, 11, 445, 1, 330, 3000};
int size = sizeof(arr)/sizeof(arr[0]);
// struct minMax p = findMinMax(arr,0,size-1);
struct minMax c = minMaxCompare(arr,size-1);
cout<<c.maxi<<endl;
cout<<c.mini<<endl;
}