Skip to content
Snippets Groups Projects
Commit ca6a465b authored by Stefano Zibetti's avatar Stefano Zibetti
Browse files

Upload New File

parent c0d59541
Branches
Tags
No related merge requests found
#include <math.h>
#define BIGPART 600
#define SWAP(a,b) temp=(a);(a)=(b);(b)=temp;
float select_FR(float arr[], int l_par, int r_par, int k)
{
int n,i,j,ll,rr,l,r,s,sd;
float z,temp,t;
l=l_par;
r=r_par;
while (r>l) {
if (r-l > BIGPART) {
n=r-l+1;
i=k-l+1;
z=log(n);
s=(int)(0.5*exp(2.*z/3.));
sd=(int)(0.5*sqrt(z*s*(n-s)/n)*((i-n/2)>0?1:-1));
ll=(int)(k-i*s/n+sd);
if (l>ll) ll=l;
rr=(int)(k+(n-i)*s/n+sd);
if (r<rr) rr=r;
select_FR(arr,ll,rr,k);
}
t=arr[k];
i=l;
j=r;
SWAP(arr[l],arr[k]);
if (arr[r]>t) {SWAP(arr[r],arr[l])}
while (i<j) {
SWAP(arr[i],arr[j])
i++;
j--;
while (arr[i]<t) i++;
while (arr[j]>t) j--;
}
if (arr[l]==t) {
SWAP(arr[l],arr[j])
} else {
j++;
SWAP(arr[j],arr[r])
}
if (j <= k) l = j + 1;
if (k <= j) r = j - 1;
}
return arr[k];
}
#undef SWAP
#undef BIGPART
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment