Cod sursa(job #543430)

Utilizator alexeiIacob Radu alexei Data 27 februarie 2011 23:48:15
Problema Sortare prin comparare Scor 40
Compilator c Status done
Runda Arhiva educationala Marime 0.98 kb
#include<stdio.h>
#include<time.h>
#include<stdlib.h>
#define NMAX 500010

//quick by the book
//sort of ish
int A[NMAX];

inline void swap(int i,int j)
{
    int aux=A[j];
    A[j]=A[i];
    A[i]=aux;
}

int partition( int left, int right )
{
    int i,j;

    srand( time( NULL ) );

    int p=rand()%(right-left);
    swap(left+p,right);

    i=left-1;
    for( j=left; j<right; ++j)
    {
        if( A[j]<=A[right] )
        {
                swap(++i,j);
        }
    }

    swap(++i,right);
    return i;
}

void quicksort(int left,int right)
{
    int p;//pivot
    if( left<right )
    {
        p=partition( left, right );
        quicksort( left,p-1 );
        quicksort( p+1,right );
    }
}

int main()
{
    freopen("algsort.in","r",stdin);
    freopen("algsort.out","w",stdout);

    int N;
    scanf("%d",&N);

    int i;
    for(i=1; i<=N; ++i)
        scanf("%d",&A[i]);

    quicksort(1,N);

    for(i=1; i<=N; ++i)
        printf("%d ",A[i]);
    printf("\n");

    return 0;
}