Cod sursa(job #2197763)

Utilizator LivcristiTerebes Liviu Livcristi Data 22 aprilie 2018 20:47:00
Problema Sortare prin comparare Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 1.09 kb
#include <iostream>
#include <fstream>
#define NUM 500005
int v[NUM];
int n;
void schimb(int &a, int &b)
{
    int aux = a;
    a = b;
    b = aux;
}
void quickSort(int left, int right) {

      int i = left, j = right;

      int tmp;

      int pivot = v[(left + right) / 2];



      /* partition */

      while (i <= j) {

            while (v[i] < pivot)

                  i++;

            while (v[j] > pivot)

                  j--;

            if (i <= j) {

                  tmp = v[i];

                  v[i] = v[j];

                  v[j] = tmp;

                  i++;

                  j--;

            }

      };



      /* recursion */

      if (left < j)
            quickSort(left, j);

      if (i < right)
            quickSort(i, right);

}
using namespace std;
int main()
{
    ifstream f("algsort.in");
    ofstream g("algsort.out");
    f >> n;
    for(int i = 0; i < n; ++i)
        f >> v[i];
    quickSort(0, n - 1);
    for(int i = 0; i < n; ++i)
        g << v[i] << " ";
    f.close();
    g.close();
}