Cod sursa(job #1322820)

Utilizator tweetyMarinescu Ion tweety Data 20 ianuarie 2015 13:53:16
Problema Sortare prin comparare Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 0.93 kb
#include <fstream>
#include <algorithm>
#include <ctime>
using namespace std;

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

int N;
int* v;

void QuickSort(int* a, int start, int end)
{
    if (start >= end)
        return;

    int i = start;
    int j = end;
    int pivot = a[rand() % (end - start + 1) + start];

    while (i <= j)
    {
        while (a[i] < pivot)
            ++i;

        while (a[j] > pivot)
            --j;

        if (i <= j)
            swap (a[i++], a[j--]);
    }

    if (end > i)
        QuickSort(a, i, end);
    if (start < j)
        QuickSort(a, start, j);
}

int main()
{
    in >> N;
    v = new int[N + 1];

    for (int i = 1; i <= N; ++i)
        in >> v[i];

    srand(time(NULL));
    QuickSort(v, 1, N);

    for (int i = 1; i <= N; ++i)
        out << v[i] << " ";

    in.close();
    out.close();
    delete[] v;

    return 0;
}