#include<iostream>
#include<cstdlib>
#include<ctime>
#define MAX_VALUE 10000
#define random() rand()%MAX_VALUE
#define N 10000
using namespace std;
int a[N];
class Find
{
public:
void bubble(int first, int last) //冒泡排序
{
for(int flag=first; flag<last; flag++)
{
for(int i=last; i<flag; i--)
{
if(a[i-1]<a[i])
{
int temp=a[i];
a[i]=a[i-1];
a[i-1]=temp;
}
}
}
}
int partition(int p, int r, int x) //以数值x对数组进行分割
{
int i, j;
for(i=p,j=r; i<j; i++)
{
if(a[i]>x)
{
while(i<j && a[j]>x)
j--;
if(i!=j)
{
int temp=a[i];
a[i]=a[j];
a[j]=temp;
j--;
}
}
}
return i-1; //i多自加了一次
}
int select(int p, int r, int k) //第k小的元素
{
if(r-p<5)
{
bubble(p,r);
return a[p-1+k];
}
for(int i=0; i<(r-p-4)/5; i++)
{
int s=p+5*i, t=s+4;
bubble(s,t);
int temp=a[p+i]; //把所有中位数都移到数组前面
a[p+i]=a[s+2];
a[s+2]=temp;
}
int x=select(p, p+(r-p-4)/5, (r-p+6)/10);
//计算已经得到的一组中位数的中位数,即第(((r-p-4)/5)/2+1)小的数(根据约定,有偶数个中位数的话取下中位数)
int i=partition(p,r,x);
int j=i-p+1;
if(j=k)
return a[i];
else if(j<k)
return select(p,i,k);
else
return select(i+1,r,k-j);
}
};
void main()
{
srand((int)time(NULL));
for(int i=0; i<N; i++)
{
a[i]=random();
cout<<a[i]<<"\t";
}
cout<<endl;
Find f;
int n=5000;
cout<<"The No."<<n<<" is:"<<f.select(0,N-1,n)<<endl;
}
9.3 最坏情况下线性时间选择
最新推荐文章于 2022-08-11 13:09:10 发布