#include<iostream>
#include<time.h>
#include<stdlib.h>
#include<stdio.h>
using namespace std;
template<class Type>
void Swap(Type &a,Type &b)
{
Type c;
c = a;
a = b;
b = c;
}
template<class Type>
int Partition(Type a[],int p, int r,Type x)
{
int i = p-1,j = r+1;
while(true){
while(a[++i]<x);
while(a[--j]>x);
if(i>=j) break;
Swap(a[i],a[j]);
}
return j;
}
template<class Type>
void BubbleSort(Type a[],int p,int r)
{
for(int i = p;i < r;i++)
{
for(int j = i+1;j < r;j++)
{
Type c;
if(a[i]>a[j]){
c=a[i];
a[i]=a[j];
a[j]=c;
}
}
}
}
template<class Type>
Type Select(Type a[],int p,int r,int k)
{
if(r-p<75)
{
BubbleSort(a,p,r);
return a[p+k-1];
}
for(int i=0;i<=(r-p-4)/5;i++)
{
BubbleSort(a,p+5*i,p+5*i+4);
Swap(a[p+5*i+2],a[p+i]);
}
Type x = Select (a,p,p+(r-p-4)/5,(r-p-4)/10);
int i = Partition(a,p,r,x),j=i-p+1;
if(k<=j)
return Select(a,p,i,k);
else
return Select(a,i+1,r,k-j);
}
void main(){
srand((unsigned)time(NULL));
int a[100];
int i;
printf("随即初始化数组的100个数(范围是0~100).\n");
for(int i=0;i<100;i++){
a[i]=rand()%100+1;
printf("%d",a[i]);
if(i%10==0 && i!=0)
printf("\n");
}
getchar();
Select(a,0,99,10);
for(int i=0;i<100;i++)
printf("%d",a[i]);
}
你好,我是有问必答小助手,非常抱歉,本次您提出的有问必答问题,技术专家团超时未为您做出解答
本次提问扣除的有问必答次数,将会以问答VIP体验卡(1次有问必答机会、商城购买实体图书享受95折优惠)的形式为您补发到账户。
因为有问必答VIP体验卡有效期仅有1天,您在需要使用的时候【私信】联系我,我会为您补发。