Cod sursa(job #302854)

Utilizator cristikIvan Cristian cristik Data 9 aprilie 2009 12:37:21
Problema Sortare prin comparare Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.64 kb
#include <stdio.h>
#include <algorithm>
using namespace std;
int a[500001],n;
void quick(int x,int y)
{
    int i,j,p;
    if(x<y)
    {
        i=x-1;
        j=y+1;
        p=a[(x+y)/2];
        while(i<j)
        {
            do i++; while(a[i]<p);
            do j--; while(a[j]>p);
            if(i<j) swap(a[i],a[j]);
        }
        quick(x,i-1);
        quick(j+1,y);
    }
}
int main()
{
    int i;
    freopen("algsort.in","r",stdin);
    freopen("algsort.out","w",stdout);
    scanf("%d",&n);
    for(i=1; i<=n; i++) scanf("%d",&a[i]);
    quick(1,n);
    for(i=1; i<=n; i++) printf("%d ",a[i]);
    return 0;
}