Cod sursa(job #240445)

Utilizator AndreiDDiaconeasa Andrei AndreiD Data 7 ianuarie 2009 17:51:41
Problema Sortare prin comparare Scor 20
Compilator cpp Status done
Runda Arhiva educationala Marime 0.75 kb
#include <stdio.h>
#include <stdlib.h>

#define Nmax 500001


int V[Nmax],N;


void qsort(int ls, int ld)
{
  int x,i,j,aux;
  i=ls;
  j=ld;
  x=V[(ls+ld)>>1];
  do
   {
    while (x>V[i] && i<N) ++i;
    while (x<V[j] && j>1) --j;
    if (i<=j)
	{
	 aux=V[i];
	 V[i]=V[j];
	 V[j]=aux;
	 ++i;
	 --j;
	}
   }
   while (i<=j);
   if (ld<j) qsort(ld,j);
   if (ls>i) qsort(i,ls);


}


void read_data()
{
   int i;
   freopen("algsort.in","r",stdin);
   scanf("%d", &N);
   for (i=1;i<=N;++i)
	scanf("%d", V+i);
}



void write_data()
{
  int i;
  freopen("algsort.out","w",stdout);
  qsort(1,N);
  for (i=1;i<=N;++i)
       printf("%d ", V[i]);
}


int main()
{
  read_data();
  write_data();
  return 0;
}