#include<iostream>
#include<time.h>
#include <stdlib.h>
#define Max 10002
using namespace std;
int a[Max],num;
void rand_quick_sort(int q,int r)
{
if(q<r)
{
srand(time(0));
int index=rand()%(r-q+1)+q;
int temp,i,j;
i=q+1;j=r;
temp=a[q];a[q]=a[index];a[index]=temp;
while(1)
{
while(i<=r&&a[i]<a[q]) i++;
while(j>=q&&a[j]>a[q]) j--;
if(i>=j) break;//注意这里还有一个等于号
else{
temp=a[i];a[i]=a[j];a[j]=temp;
}
}
if(a[q]!=a[j])//加上了就是稳定排序
{
temp=a[q];a[q]=a[j];a[j]=temp;
}
rand_quick_sort(q,j-1);
rand_quick_sort(j+1,r);
}
}
int main()
{
cin >> num;
for (int i=0;i<num;++i)
cin >> a[i];
rand_quick_sort(0,num-1);
cout << a[num/2] << endl;
}
poj2388
最新推荐文章于 2021-07-27 18:12:26 发布