-
Notifications
You must be signed in to change notification settings - Fork 0
/
Week-05_Question-2_runningmedian.cpp
119 lines (96 loc) · 2.59 KB
/
Week-05_Question-2_runningmedian.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
#include <iostream>
#include <vector>
using namespace std;
// Merge function to merge two sorted arrays
vector<int> merge(vector<int>& arr1, vector<int>& arr2) {
vector<int> merged;
int i = 0, j = 0;
while (i < arr1.size() && j < arr2.size()) {
if (arr1[i] <= arr2[j]) {
merged.push_back(arr1[i]);
i++;
}
else {
merged.push_back(arr2[j]);
j++;
}
}
while (i < arr1.size()) {
merged.push_back(arr1[i]);
i++;
}
while (j < arr2.size()) {
merged.push_back(arr2[j]);
j++;
}
return merged;
}
// Merge sort function to sort an array
vector<int> mergeSort(vector<int>& arr) {
int n = arr.size();
if (n == 1) {
return arr;
}
int mid = n / 2;
vector<int> left(arr.begin(), arr.begin() + mid);
vector<int> right(arr.begin() + mid, arr.end());
left = mergeSort(left);
right = mergeSort(right);
return merge(left, right);
}
// void userInput()
// {
// int size=5;
// int arr[size];
// cout<<"Enter array elements"<<endl;
// for(int i=0;i<size;i++)
// {
// cin>>arr[i];
// }
// cout<<"Array elements are:"<<endl;
// for(int i=0;i<size;i++)
// cout<<arr[i]<<" ";
// cout<<endl;
// }
int main() {
int size1;
cin>>size1;
int* arr1 = new int[size1];
cout<<"Enter array elements"<<endl;
for(int i=0;i<size1;i++)
{
cin>>arr1[i];
}
std::vector<int> arr(arr1, arr1 + size1);
for (int x : arr) {
std::cout << x << " ";
}
std::cout << std::endl;
vector<int> sortedArr;
double median = 0;
cout << "median" << " sorted"<<endl;
for (int i = 0; i < arr.size(); i++) {
// Add the i-th element to the sorted array
sortedArr.push_back(arr[i]);
// Sort the updated array using merge sort
sortedArr = mergeSort(sortedArr);
// Calculate the median
int n = sortedArr.size();
if (n % 2 == 0) {
median = (double)(sortedArr[n/2 - 1] + sortedArr[n/2]) / 2.0;
}
else {
median = sortedArr[n/2];
}
// Print the updated median
cout << "[";
for (int j = 0; j < sortedArr.size(); j++) {
cout << sortedArr[j];
if (j != sortedArr.size() - 1) {
cout << ", ";
}
}
cout << "] " << median << endl;
}
return 0;
}