Cod sursa(job #2273175)

Utilizator stefzahZaharia Stefan Tudor stefzah Data 31 octombrie 2018 09:50:46
Problema Sortare prin comparare Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.68 kb
#include <fstream>
using namespace std;
ifstream fin("algsort.in");
ofstream fout("algsort.out");
int aux, N, a[500005];
void Quicksort(int st, int dr)
{
    int x = a[(st + dr)/2], i, j;
    i = st;
    j = dr;

    while(i<j)
   {while ( (i < dr) && (a[i] < x) ) i++;
    while ( (j > st) && (a[j] > x) ) j--;
    if ( i <= j ) {
      aux = a[i];
      a[i] = a[j];
      a[j] = aux;
      i++;j--;
                  }
   }
    if(st < j)
        Quicksort(st, j);
    if(dr > i)
        Quicksort(i, dr);
}
int main()
{
    int i;
    fin>>N;
    for(i=1; i <= N; i++)
        fin>>a[i];
    Quicksort(1,N);
    for(i=1; i <= N; i++)
        fout<<a[i]<<" ";
}