Pagini recente » Cod sursa (job #1277369) | Cod sursa (job #3260643) | Cod sursa (job #2989979) | Cod sursa (job #2486963) | Cod sursa (job #1103774)
#include<cstdio>
#include<cstdlib>
#include<time.h>
#define nMax 500005
using namespace std;
int N, V[nMax];
inline void swap(int &x, int &y){
int aux = x;
x = y;
y = aux;
}
void Quick3Way(int lo, int hi){
if (hi <= lo)
return;
int lt = lo, gt = hi;
int idx = lo;
int v = V[lo];
while(idx <= gt){
if(V[idx] < v)
swap(V[lt ++], V[idx ++]);
else if(V[idx] > v)
swap(V[idx], V[gt --]);
else
idx ++;
}
Quick3Way(lo, lt - 1);
Quick3Way(gt + 1, hi);
}
void RandomShuffle(){
srand ( time(NULL) );
for(int i = 0; i < N; ++ i)
swap(V[i], V[rand() % N]);
}
int main(){
freopen ("r", "algsort.in", stdin);
freopen ("w", "algsort.out", stdout);
scanf("%d", &N);
for(int i = 0; i < N; ++ i)
scanf("%d", V + i);
RandomShuffle();
Quick3Way(0, N - 1);
for(int i = 0; i < N; ++ i)
printf("%d ", V[i]);
fclose(stdin);
fclose(stdout);
return 0;
}