Cod sursa(job #2942864)

Utilizator andiRTanasescu Andrei-Rares andiR Data 20 noiembrie 2022 11:10:36
Problema Sortare prin comparare Scor 100
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.68 kb
#include <iostream>
#include <fstream>

#define Nmax 500000
using namespace std;
ifstream fin ("algsort.in");
ofstream fout ("algsort.out");
int n;
int v[Nmax];
void quicksort (int v[], int begin, int end)
{
  	int aux, b=begin, e=end;
  	long long pivot=v[(begin+end)/2];

  while (b<=e){
  	while (v[b]<pivot) b++;
  	while (v[e]>pivot) e--;
    	if (b<=e){
     	    swap(v[b], v[e]);
      	    b++; e--;
        }
   }
   if (begin<e) quicksort(v, begin, e);
   if (b<end) quicksort(v, b, end);
}
int main()
{
    fin>>n;
    for (int i=0;i<n;i++)
        fin>>v[i];
    quicksort(v, 0, n-1);
    for (int i=0;i<n;i++)
        fout<<v[i]<<' ';
    return 0;
}