median search=>

This commit is contained in:
Ayush Nagar 2018-10-02 12:00:43 +05:30 committed by GitHub
parent 87e79e916c
commit bb46f062dc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 323 additions and 0 deletions

251
Search/interpolition.cpp Normal file
View File

@ -0,0 +1,251 @@
#include<iostream>
#include<vector>
#include<iterator>
#include<algorithm>
using namespace std;
class interpolition
{
public:
int i,j=0,j1=0,j2=0,j3=0;
vector<int>a;
vector<double>a1;
vector<char>a2;
vector<string>a3;
void input(int n)
{
a[j]=n;j++;
}
void input(double n)
{
a1.push_back(n);
}
void input(char n)
{
a2.push_back(n);
}
void input(string n)
{
a3.push_back(n);
}
void sorting()
{
if(a.size()!=0)
{sort(a.begin(),a.end());
}
if(a1.size()!=0)
{sort(a1.begin(),a1.end());
}
if(a2.size()!=0)
{sort(a2.begin(),a2.end());
}
if(a3.size()!=0)
{sort(a3.begin(),a3.end());
}
}
int binary(int x)
{
int s=0,l=a.size(),c=0,mid;
do
{
c=0;
mid=(l-s/(a[l]-a[s]))*(x-a[s]);
if(a[mid]==x)
{
return mid;
}
if(a[mid]>x)
{
l=mid-1;
}
if(a[mid]<x)
{
s=mid+1;
}
}while(l<=mid);
return -1;
}
int binary(double x)
{
int s=0,l=a.size(),c=0,mid;
do
{
c++;
mid=(l-s/(a1[l]-a1[s]))*(x-a1[s]);
if(a1[mid]==x)
{
return mid;
}
if(a1[mid]>x)
{
l=mid-1;
}
if(a1[mid]<x)
{
s=mid+1;
}
}while(l<=mid);
return -1;
}
int binary(char x)
{
int s=0,l=a2.size(),c=0,mid;
do
{
c++;
mid=(l-s/(a[l]-a[s]))*(x-a2[s]);
if(a2[mid]==x)
{
return mid;
}
if(a2[mid]>x)
{
l=mid-1;
}
if(a2[mid]<x)
{
s=mid+1;
}
}while(l<=mid);
return -1;
}
int binary(string st1)
{
int low=0,hi=a3.size()-1,pos=0,h=hi,l=low,x=hi;
cout<<a3.size()<<endl;
while(x<=h&&l<=pos&&l<h)
{
l=(int)(a3[low].at(0));
h=(int)(a3[hi].at(0));
x=(int)(st1.at(0));
pos=low+((hi-low)*(x-l)/(h-l));
cout<<pos<<endl;
if(a3[pos]==st1)
{
return pos;
}
else if(a3[pos] > st1)
{
hi=pos-1;
}
else
{
low=pos+1;
}
}
return -1;
}
};
int main()
{
interpolition b;
int ch;
cout<<"what is the datatype of input:-\n1. int\n2. double\n3. char\n4. string"<<endl;
cin>>ch;
switch(ch)
{
case 1:
{
int n,m;
do
{
cout<<"enter the value to be inserted in array:-";
cin>>n;
b.input(n);
cout<<"do you want to add more elements? 1. yes 2. no\n";
cin>>m;
}while(m!=0);
b.sorting();
int x;
cout<<"enter the value to be searched in array:-";
cin>>x;
int k=b.binary(x);
if(k==-1)
{
cout<<x<<" is not found"<<endl;
}
else
{
cout<<x<<" is found at "<<k<<"th location"<<endl;
}
break;
}
case 2:
{
double n;int m;
do
{
cout<<"enter the value to be inserted in array:-";
cin>>n;
b.input(n);
cout<<"do you want to add more elements? 1. yes 2. no\n";
cin>>m;
}while(m!=0);
b.sorting();
double x;
cout<<"enter the value to be searched in array:-";
cin>>x;
int k=b.binary(x);
if(k==-1)
{
cout<<x<<" is not found"<<endl;
}
else
{
cout<<x<<" is found at "<<k<<"th location"<<endl;
}
break;
}
case 3:
{
char n;int m;
do
{
cout<<"enter the value to be inserted in array:-";
cin>>n;
b.input(n);
cout<<"do you want to add more elements? 1. yes 2. no\n";
cin>>m;
}while(m!=0);
b.sorting();
char x;
cout<<"enter the value to be searched in array:-";
cin>>x;
int k=b.binary(x);
if(k==-1)
{
cout<<x<<" is not found"<<endl;
}
else
{
cout<<x<<" is found at "<<k<<"th location"<<endl;
}
break;
}
case 4:
{
string n;int m;
do
{
cout<<"enter the value to be inserted in array:-";
cin>>n;
b.input(n);
cout<<"do you want to add more elements? 1. yes 2. no\n";
cin>>m;
}while(m!=0);
b.sorting();
string x;
cout<<"enter the value to be searched in array:-";
cin>>x;
int k=b.binary(x);
if(k==-1)
{
cout<<x<<"is not found"<<endl;
}
else
{
cout<<x<<" is found at "<<k<<"th location"<<endl;
}
break;
}
}
}

72
Search/median_search.cpp Normal file
View File

@ -0,0 +1,72 @@
#include<iostream>
#include<math.h>
#include<deque>
#include<stack>
#include<vector>
#include<algorithm>
#include<iterator>
using namespace std;
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";
}
else if(s1.size()>x)
{
sort(s1.begin(),s1.end());
cout<<s1[x]<<" is the "<<x+1<<"th element from front";
}
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";
}
}
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 x;
cout<<"enter the no. to be searched form begining:- ";
cin>>x;
comp(x-1);
return 0;
}