快速排序——中位数
#include <iostream>
using namespace std;
int quick_sort(int a[],int left,int right)
{
if(left>right)
return 0;
int i,j,t,key;
key=a[left];
i=left;
j=right;
while(i!=j){
while(i<j&&a[j]>=key)
j--;
while(i<j&&a[i]<=key)
i++;
if(i<j){
t=a[i];
a[i]=a[j];
a[j]=t;
}
}
a[left]=a[i];
a[i]=key;
return i;
/*quick_sort(a,left,i-1);
quick_sort(a,i+1,right);*/
}
double medium(int a[],int n){
int left=1;
int right=n;
int mid=(left+right)/2;
int num;
while(1){
num=quick_sort(a,left,right);
if(num==mid)
break;
if(num<mid)
left=num+1;
if(num>mid)
right=num-1;
}
return (n%2!==0)?a[mid]:(double)(a[mid]+a[mid+1])/2;
}
int main(){
int a[100],n;
cin>>n;
for(int i=1;i<=n;i++){
cin>>a[i];
}
cout<<medium(a,n)<<endl;
}