Cod sursa(job #1815922)

Utilizator SlevySlevoaca Stefan-Gabriel Slevy Data 25 noiembrie 2016 22:19:48
Problema Sortare prin comparare Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.88 kb
#include <iostream>
#include <fstream>
#define NMAX 500001

using namespace std;

ifstream in("algsort.in");
ofstream out("algsort.out");

int A[NMAX], n;

void quicksort(int x, int y)
{
    if (x < y)
    {
        int i = x, j = y, pivot = A[(x + y)/2];
        while (i <= j)
        {
            while (A[i] < pivot)
                i++;
            while (A[j] > pivot)
                j--;
            if (i <= j)
            {
                swap(A[i], A[j]);
                i++;
                j--;
            }
        }
        if (i < y)
            quicksort(i, y);
        if (j > x)
            quicksort(x, j);
    }
}

int main()
{
    in >> n;
    for (int i = 1; i <= n; i++)
        in >> A[i];
    in.close();
    quicksort(1, n);
    for (int i = 1; i <= n; i++)
        out << A[i] << " ";
    out.close();
    return 0;
}