#include<stdio.h>
#include<stdlib.h>
double*kuaisuxuanze(double*L,double*R,int k)
{
double t,*i,*j;
double z=R[-1];
if(R==L+1)return L;
for(i=j=L;i!=R;i++)
{
if(*i<z)
{
t=*i;
*i=*j;
*j++=t;
}
}
R[-1]=*j;
*j=z;
if(k<=j-L)return kuaisuxuanze(L,j,k);
else return kuaisuxuanze(j,R,k-(j-L));
}
double getmid(const double*p,int n)
{
double*t=(double*)malloc(sizeof(double)*n);
for(int i=0;i<n;i++)
t[i]=p[i];
if(n&1)
return *kuaisuxuanze(t,t+n,(n>>1)+1);
else
return (*kuaisuxuanze(t,t+n,(n>>1)+1)+*kuaisuxuanze(t,t+n,(n>>1)))/2;
}
int main()
{
double a[9]={3,5,1,7,4,6,2,9,10};
printf("%lf",getmid(a,9));
}
利用快速选择算法在期望O(n)时间内找到中位数