Skip to content

fix: median_search bug #850

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
131 changes: 76 additions & 55 deletions search/median_search.cpp
Original file line number Diff line number Diff line change
@@ -1,72 +1,93 @@
#include<iostream>
#include<math.h>
#include<cmath>
#include<deque>
#include<stack>
#include<vector>
#include<algorithm>
#include<iterator>
using namespace std;
#include<cstdlib>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
#include<cstdlib>
#include <cstdlib>

#include <cstdio>
using std::cin;
using std::cout;
using std::vector;
using std::endl;
vector<int>v;
vector<int>s1;
vector<int>s2;
vector<int>s3;
template <class X>
void comp(X x)
{
if(s1.size()>=x && s1.size()+s2.size()<x)
{
cout<<s2[0]<<" is the "<<x+1<<"th element from front";

void comp(X x) {
if (s1.size()+s2.size()+s3.size() > x && s1.size()+s2.size() <= x) {
sort(s3.begin(), s3.end());
cout << s3[x-s2.size()-s1.size()] << " is the " << x+1;
if (x < 1) {
cout << "st";
} else if (x < 2) {
cout << "nd";
} else if (x < 3) {
cout << "rd";
} else {
cout << "th";
}
else if(s1.size()>x)
{
sort(s1.begin(),s1.end());
cout<<s1[x]<<" is the "<<x+1<<"th element from front";
cout << " element from the front.\n";
} else if (s1.size()+s2.size() > x && s1.size() <= x) {
sort(s2.begin(), s2.end());
cout << s2[x-s1.size()] << " is the " << x+1;
if (x < 1) {
cout << "st";
} else if (x < 2) {
cout << "nd";
} else if (x < 3) {
cout << "rd";
} else {
cout << "th";
}
else if(s1.size()+s2.size()<=x && s3.size()>x)
{
sort(s3.begin(),s3.end());
cout<<s3[x-s1.size()-s2.size()]<<" is the "<<x+1<<"th element from front";
}
else
{
cout<<x+1<<" is invalid location";
cout << " element from the front.\n";
} else if (s1.size() > x) {
sort(s1.begin(), s1.end());
cout << s1[x] << " is the " << x+1;
if (x < 1) {
cout << "st";
} else if (x < 2) {
cout << "nd";
} else if (x < 3) {
cout << "rd";
} else {
cout << "th";
}
cout << " element from the front.\n";
} else {
cout << x+1 << " is an invalid location.\n";
}
}
int main()
{
for(int i=0;i<1000;i++)
{
v.push_back(rand()%1000);
}
for(int r:v)
{
cout<<r<<" ";
}
int median=rand()%1000;
cout<<"\nmedian="<<median<<endl;
int avg1,avg2,avg3,sum1=0,sum2=0,sum3=0;
for(int i=0;i<1000;i++)
{
if(v.back()==v[median])
{
avg1=sum1+v.back();
s2.push_back(v.back());
}
else if(v.back()<v[median])
{
avg2=sum2+v.back();
s1.push_back(v.back());
}
else
{
avg3=sum3+v.back();
s3.push_back(v.back());
}
v.pop_back();

int main() {
unsigned int seed = 1;
int set_num;
cout << "How many elements should be in the set? ";
cin >> set_num;
for (int i=0; i < set_num; i++) {
v.push_back(rand_r(&seed)%1000);
}
for (int r : v) {
cout << r << " ";
}
int median = rand_r(&seed)%1000;
cout << "\nmedian = " << median << endl;
for (int i=0; i < set_num; i++) {
if (v.back() == median) {
s2.push_back(v.back());
} else if (v.back() < median) {
s1.push_back(v.back());
} else {
s3.push_back(v.back());
}
int x;
cout<<"enter the no. to be searched form begining:- ";
cin>>x;
comp(x-1);
return 0;
v.pop_back();
}
int x;
cout << "enter the no. to be searched from beginning: ";
cin >> x;
comp(x-1);
return 0;
}