Pagini recente » Cod sursa (job #2085016) | Cod sursa (job #1768124) | Profil mosila.luciana | Profil M@2Te4i | Cod sursa (job #433770)
Cod sursa(job #433770)
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define NMAX 500005
int n,A[NMAX],B[NMAX],r,t;
void read()
{
scanf("%d",&n);
int i;
for (i=1; i<=n; i++)
scanf("%d",&A[i]);
}
void sorteaza(int st,int dr)
{
if (st==dr || st>dr)
return ;
int i,poz=rand()%(dr-st+1)+st,val;
val=A[poz];
r=t=0;
for (i=st; i<=dr; i++)
if (i!=poz)
if (A[i]<A[poz])
B[++r]=A[i];
for (i=st; i<=dr; i++)
if (i!=poz)
if (A[i]>A[poz])
{
t++;
B[r+t]=A[i];
}
for (i=1; i<=r; i++)
A[st+i-1]=B[i];
A[st+r]=val;
for (i=r+1; i<=r+t; i++)
A[st+i]=B[i];
sorteaza(st,st+r-1);
sorteaza(st+r+1,dr);
}
void show()
{
int i;
for (i=1; i<=n; i++)
printf("%d ",A[i]);
printf("\n");
}
int main()
{
freopen("algsort.in","r",stdin);
freopen("algsort.out","w",stdout);
read();
srand(time(0));
sorteaza(1,n);
show();
return 0;
}