#include<iostream>
#include<cmath>
#include<string>
#include<cstring>
#include<algorithm>
#include<cstdio>
using namespace std;
#define N 100010
int a[N] = {};
int getp( int l, int r){// 边界取等
return l+rand()%(r-l+1);
//return (l+r)/2;
}
void swap( int* c, int p, int q){
int t = c[p];
c[p] = c[q];
c[q] = t;
}
int partition( int l, int r,int* b, int key){
int first = l;
int last = r;
while( first < last ){
while( first < last && b[first] < key )
first++;
if( first >= last)
break;
b[last] = b[first];
last--;
while( first < last && b[last] > key )
last--;
if( first >= last )
break;
b[first] = b[last];
first++;
}
return first;
}
void myqsort( int left, int right, int*b ){
if( left >= right)
return;
int p = getp(left, right);
int tmp = b[p];
swap( b, p, right);
int pos = partition( left , right, b, tmp);
b[pos] = tmp;
myqsort( left, pos-1,b);
myqsort( pos+1, right, b);
}
int main(){
int n;
scanf("%d", &n);
for( int i = 0; i < n; ++i )
scanf("%d", &a[i]);
myqsort( 0, n-1, a);
for( int i = 0; i < n; ++i )
printf( "%d\n", a[i]);
//printf("\n");
//system("pause");
return 0;
}
主要想记住随机数生成函数(代码中的getp(..))