attempted to document median search

This commit is contained in:
Krishna Vedala 2020-05-28 21:06:53 -04:00
parent 5ab43ad039
commit 8291db4f9f
No known key found for this signature in database
GPG Key ID: BA19ACF8FC8792F7

View File

@ -1,18 +1,21 @@
/**
* \file
* \brief [Median search](https://en.wikipedia.org/wiki/Median_search) algorithm
* \warning This core is erroneous and gives invorrect answers. Tested using
* cases from [here](https://brilliant.org/wiki/median-finding-algorithm/)
* \ingroup median search
* \{
*/
#include <algorithm> #include <algorithm>
#include <cmath>
#include <deque>
#include <iostream> #include <iostream>
#include <iterator>
#include <stack>
#include <vector> #include <vector>
std::vector<int> v; /**
std::vector<int> s1; * @todo add documentation
std::vector<int> s2; */
std::vector<int> s3;
template <class X> template <class X>
void comp(X x) { void comp(X x, std::vector<int> &s1, std::vector<int> &s2,
std::vector<int> &s3) {
if (s1.size() >= x && s1.size() + s2.size() < x) { if (s1.size() >= x && s1.size() + s2.size() < x) {
std::cout << s2[0] << " is the " << x + 1 << "th element from front"; std::cout << s2[0] << " is the " << x + 1 << "th element from front";
} else if (s1.size() > x) { } else if (s1.size() > x) {
@ -26,17 +29,32 @@ void comp(X x) {
std::cout << x + 1 << " is invalid location"; std::cout << x + 1 << " is invalid location";
} }
} }
#define MAX_NUM 20 ///< maximum number of values to sort from
/**
* Main function
*/
int main() { int main() {
for (int i = 0; i < 1000; i++) { std::vector<int> v{25, 21, 98, 100, 76, 22, 43, 60, 89, 87};
v.push_back(std::rand() % 1000); std::vector<int> s1;
} std::vector<int> s2;
for (int r : v) { std::vector<int> s3;
std::cout << r << " ";
} // creates an array of random numbers
int median = std::rand() % 1000; // for (int i = 0; i < MAX_NUM; i++) {
// int r = std::rand() % 1000;
// v.push_back(r);
// std::cout << r << " ";
// }
for (int r : v) std::cout << r << " ";
int median = std::rand() % 1000; // initialize to a random numnber
std::cout << "\nmedian=" << median << std::endl; std::cout << "\nmedian=" << median << std::endl;
int avg1, avg2, avg3, sum1 = 0, sum2 = 0, sum3 = 0; int avg1, avg2, avg3, sum1 = 0, sum2 = 0, sum3 = 0;
for (int i = 0; i < 1000; i++) {
for (int i = 0; i < v.size(); i++) { // iterate through all numbers
if (v.back() == v[median]) { if (v.back() == v[median]) {
avg1 = sum1 + v.back(); avg1 = sum1 + v.back();
s2.push_back(v.back()); s2.push_back(v.back());
@ -49,9 +67,12 @@ int main() {
} }
v.pop_back(); v.pop_back();
} }
int x; int x;
std::cout << "enter the no. to be searched form begining:- "; std::cout << "enter the no. to be searched form begining:- ";
std::cin >> x; std::cin >> x;
comp(x - 1); comp(x - 1, s1, s2, s3);
return 0; return 0;
} }
/// }