Cod sursa(job #1020703)

Utilizator vlad.florescu94FMI Florescu Vlad - Adrian vlad.florescu94 Data 2 noiembrie 2013 15:19:12
Problema Sortare prin comparare Scor 40
Compilator cpp Status done
Runda Arhiva educationala Marime 0.6 kb
#include<fstream>
#include<ctime>
#include<cstdlib>
using namespace std;
ifstream f("algsort.in");
ofstream g("algsort.out");
long n,v[500001],i;
void qsort(long st,long dr)
{long i=st,j=dr,piv;
 srand(time(0));
 piv=v[i+rand()%(j-i+1)];
 while(i<=j)
   {
    while(v[i]<piv)
        i++;
    while(v[j]>piv)
        j--;
    if(i<=j)
      {v[i]=v[i]+v[j]-(v[j]=v[i]);i++;j--;}
   }
 if(st<=j)
    qsort(st,j);
 if(i<=dr)
    qsort(i,dr);
}
int main()
{f>>n;
 for(i=1;i<=n;i++)
    f>>v[i];
 qsort(1,n);
 for(i=1;i<=n;i++)
    g<<v[i]<<" ";
 f.close();g.close();
 return 0;
}