-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathminMaxAlgorithm.cpp
More file actions
110 lines (96 loc) · 2.49 KB
/
Copy pathminMaxAlgorithm.cpp
File metadata and controls
110 lines (96 loc) · 2.49 KB
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
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
void file_i_o()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
}
pair<int, int> getMinMax(vector<int> &arr, int n)
{
pair<int, int> minmax;
int i;
if (n == 1)
{
minmax.first = arr[0];
minmax.second = arr[0];
return minmax;
}
if (arr[0] > arr[1])
{
minmax.first = arr[0];
minmax.second = arr[1];
}
else
{
minmax.first = arr[1];
minmax.second = arr[0];
}
for (i = 2; i < n; i++)
{
if (arr[i] > minmax.first)
minmax.first = arr[i];
else if (arr[i] < minmax.second)
minmax.second = arr[i];
}
return minmax;
}
// Divide and Conqueuer
int noOfComparision = 0;
pair<int, int> getMinMaxRecursion(vector<int> &arr, int low, int high)
{
pair<int, int> minmax, minmaxleft, minmaxright;
int mid;
if (low == high)
{
minmax.first = arr[low];
minmax.second = arr[low];
return minmax;
}
if (high == low + 1)
{
noOfComparision += 1;
if (arr[low] > arr[high])
{
minmax.second = arr[low];
minmax.first = arr[high];
}
else
{
minmax.second = arr[high];
minmax.first = arr[low];
}
return minmax;
}
mid = (low + high) / 2;
minmaxleft = getMinMaxRecursion(arr, low, mid);
minmaxright = getMinMaxRecursion(arr, mid + 1, high);
noOfComparision += 2;
if (minmaxleft.first < minmaxright.first)
minmax.first = minmaxleft.first;
else
minmax.first = minmaxright.first;
if (minmaxleft.second > minmaxright.second)
minmax.second = minmaxleft.second;
else
minmax.second = minmaxright.second;
return minmax;
}
int main()
{
file_i_o();
vector<int> arr = {1, 200, 4000, 56, 19, 20};
pair<int, int> minmax = getMinMax(arr, arr.size());
pair<int, int> minmaxRecursion = getMinMaxRecursion(arr, 0, arr.size() - 1);
cout << "Maximum Element : " << minmax.first << endl;
cout << "Minimum Element : " << minmax.second << endl;
cout << "Maximum Element recursively : " << minmaxRecursion.second << endl;
cout << "Minimum Element recursively : " << minmaxRecursion.first << endl;
cout << "Number Of Comparision : " << noOfComparision << endl;
return 0;
}