-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmedian_orderd_stats.cpp
More file actions
executable file
·138 lines (124 loc) · 2.27 KB
/
median_orderd_stats.cpp
File metadata and controls
executable file
·138 lines (124 loc) · 2.27 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#include<iostream>
#include<algorithm>
#include<cstring>
#include<climits>
#include<cmath>
using namespace std;
class med
{
public:int rno,ctc;
char name[20];
void create();
};
int f[26],c[26];
int op[100];
med ob2[20];
int fm(int ob[],int n);
int kelement(int ob[],int l,int r,int k);
void swap(int *a,int *b);
int partition(int ob[],int l,int r,int x);
void cs(int ob[],int n,int p);
//void rad(int ob[],int n);
//void cs(int ob[],int n,int p);
int main()
{
int n;
cout<<"Enter the no. of ppl\n";
cin>>n;
cout<<"Enter rno,name,ctc of"<<n<<" values\n";
for(int i=0;i<n;i++)
ob2[i].create();
int ob[100];
for(int i=0;i<n;i++)
ob[i]=ob2[i].ctc;
for(int i=0;i<n;i++)
cout<<ob[i]<<" ";
//rad(ob,n);
//cout<<"median:"<<ob[n/2];
//cout<<"min:"<<ob[0];
//cout<<"max:"<<ob[n-1];
cout<<kelement(ob,0,n-1,(n+1)/2);
cout<<"max:\n";
cout<<kelement(ob,0,n-1,n);
cout<<"min:\n";
cout<<kelement(ob,0,n-1,1);
return 0;
}
/*void rad(int ob[],int n)
{
for(int i=1;i<3;i++)
{int m=pow(10,i-1); cs(ob,n,m);
}*/
void med::create()
{
cin>>rno>>name>>ctc;
}
int fm(int ob[],int n)
{
sort(ob,ob+n);
return(ob[n/2]);
}
int kelement(int ob[],int l,int r,int k)
{
if(k>0&&k<=r-l+1)
{
int n=r-l+1;
int i,m[(n+4)/5];
for(i=0;i<n/5;i++)
m[i]=fm(ob+l+i*5,5);
if(i*5<n)
{ m[i]=fm(ob+l+i*5,n%5);i++;}
int mom=(i==1)?m[i-1]:
kelement(m,0,i-1,i/2);
int pos=partition(ob,l,r,mom);
if(pos-l==k-1)
return(ob[pos]);
if(pos-l>k-1)
return(kelement(ob,l,pos-1,k));
return(kelement(ob,pos+1,r,k-pos+l-1));
}
return INT_MAX;
}
int partition(int ob[],int l,int r,int x)
{
int i;
for(i=l;i<r;i++)
{
if(ob[i]==x)
break;
}
swap(&ob[i],&ob[r]);
i=l;
for(int j=l;j<=r-1;j++)
{
if(ob[j]<=x)
{
swap(&ob[i],&ob[j]);i++;
}
}
swap(&ob[i],&ob[r]);
return i;
}
void swap(int *a,int *b)
{
int temp=*a;
*a=*b;
*b=temp;
}
/*(void cs(int ob[],int n,int p)
{
for(int i=0;i<100;i++)
f[i]=c[i]=0;
for(int i=0;i<n;i++)
f[(ob[i]/p)%10]++;
c[0]=f[0];
for(int i=1;i<100;i++)
{c[i]=c[i-1]+f[i];}
for(int i=n-1;i>=0;i--)
{
op[(c[ob[i]/p%10])-1]=ob[i];
c[ob[i]/p%10]--;
}
for(int i=0;i<n;i++)
ob[i]=op[i];
}*/