-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrunning_median.cpp
More file actions
67 lines (55 loc) · 1.78 KB
/
Copy pathrunning_median.cpp
File metadata and controls
67 lines (55 loc) · 1.78 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
/*
*/
#include <iostream> // std::cout
#include <algorithm> // std::generate
#include <ctime> // std::time
#include <deque>
#include <vector>
#include <cstdlib> // std::rand, std::srand
using std::cout;
using std::deque;
using std::vector;
// function generator of random ints:
int RandInt() { return (std::rand()%100); }
int main() {
std::srand(97);
//std::srand(unsigned(std::time(0)));
// declare a list and populate it with randomly generated ints
int N = 13;
deque<int> rDeq (N);
std::generate(rDeq.begin(), rDeq.end(), RandInt);
vector<int> sVec;
// at each iteration a previous value is removed from the back of the list and
// a new value inserted at the front. Calculate the median of the list.
int ntrials = 100;
int midx1, midx2;
size_t sz = rDeq.size();
bool ISODD = (sz & 1);
for (int i=0; i<ntrials; i++) {
rDeq.pop_front();
int irand = std::rand()%100;
rDeq.push_back(irand);
cout << "deque : ";
for (auto& e:rDeq)
cout << e << " ";
cout << "\n";
// copy to a vector and sort it to find the median
sVec.clear(); // to keep the vector from growing with each copy
std::copy(rDeq.begin(), rDeq.end(), std::back_inserter(sVec));
std::sort(sVec.begin(), sVec.end());
cout << "sorted vector: ";
for (auto& e:sVec)
cout << e << " ";
cout << "\nmedian: ";
if (ISODD) { // odd number of elements
midx1 = sz/2;
cout << sVec[midx1];
}
else { // even number of elements
midx1 = sz/2 - 1;
midx2 = midx1 + 1;
cout << (sVec[midx1] + sVec[midx2]) / 2;
}
cout << "\n\n";
}
}