This repository has been archived by the owner on Sep 16, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
medianMergedNaive.cpp
81 lines (62 loc) · 1.74 KB
/
medianMergedNaive.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
#include <iostream>
#include <fstream>
#include <vector>
#include <iomanip>
#include <algorithm>
using namespace std;
int main() {
vector<float> v1, v2;
string file1 = "median-first-array.txt";
string file2 = "median-second-array.txt";
ifstream fin;
fin.open(file1);
float num;
while (fin >> num) {
v1.push_back(num);
}
cout << "Done reading in first file" << endl;
fin.close();
fin.open(file2);
while(fin >> num) {
v2.push_back(num);
}
cout << "Done reading in second file" << endl;
cout << "Printing v1 unsorted: " << endl;
for (auto value : v1) {
cout << fixed << setprecision(2) << value << endl;
}
cout << "Printing v2 unsorted: " << endl;
for (auto value : v2) {
cout << fixed << setprecision(2) << value << endl;
}
stable_sort(v1.begin(), v1.end());
cout << "Printing v1 sorted: " << endl;
for (auto value : v1) {
cout << fixed << setprecision(2) << value << endl;
}
stable_sort(v2.begin(), v2.end());
cout << "Printing v2 sorted: " << endl;
for (auto value : v2) {
cout << fixed << setprecision(2) << value << endl;
}
vector<float> mergedVector(v1.size() + v2.size());
merge(v1.begin(), v1.end(), v2.begin(), v2.end(), mergedVector.begin());
cout << "Printing merged vector: " << endl;
for (auto value : mergedVector) {
cout << fixed << setprecision(2) << value << endl;
}
// Find median
int len = mergedVector.size();
float median;
if (len % 2 == 0) {
median = (mergedVector[len/2] + mergedVector[(len/2) + 1]) / 2.0;
cout << "HERE" << endl;
} else {
median = mergedVector[(len/2) + 1];
}
cout << "Size of v1: " << v1.size() << endl;
cout << "Size of v2: " << v2.size() << endl;
cout << "Size of merged vector: " << mergedVector.size() << endl;
cout << "Median: " << median << endl;
return 0;
}