Cod sursa(job #1936957)

Utilizator medicinedoctoralexandru medicinedoctor Data 23 martie 2017 16:19:56
Problema Sortare prin comparare Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 1.74 kb
#include <iostream>
#include <fstream>
#include <vector>
#include <cstdlib>

using namespace std;

vector <int> a;

void write()
{
    for (int i = 0; i < a.size(); i++)
        cout << a[i] << ' ';
    cout << '\n';
}

void read()
{
    int n;
    cin >> n;
    a.resize(n);

    ofstream fo("rand");
    fo << n;
    fo.close();

    system("pabcnetc.exe rand.pas");
    system("rand.exe");
    cout << "\n\n\n";

    ifstream fi("rand");

    for (int i = 0; i < a.size(); i++)
        fi >> a[i];

    fi.close();

    system("del rand.exe rand.o rand");
    write();
}

void qs(vector <int> &a)
{
    if (a.size() < 2) return;
    vector <int> x, y, z;
    int q = a[a.size() / 2];

    for (; !a.empty(); )
    {
        int last = a[a.size() - 1];
        if (last == q) y.push_back(last), a.pop_back();
        if (last > q) z.push_back(last), a.pop_back();
        if (last < q) x.push_back(last), a.pop_back();
    }

    qs(x);
    qs(z);

    for (int i = 0; i < x.size() / 2; i++)
    {
        int p = x[i];
        x[i] = x[x.size() - 1 - i];
        x[x.size() - 1 - i] = p;
    }
    for (int i = 0; i < z.size() / 2; i++)
    {
        int p = z[i];
        z[i] = z[z.size() - 1 - i];
        z[z.size() - 1 - i] = p;
    }

    for (; !x.empty(); )
    {
        int last = x[x.size() - 1];
        a.push_back(last);
        x.pop_back();
    }
    for (; !y.empty(); )
    {
        int last = y[y.size() - 1];
        a.push_back(last);
        y.pop_back();
    }
    for (; !z.empty(); )
    {
        int last = z[z.size() - 1];
        a.push_back(last);
        z.pop_back();
    }
}

int main()
{
    read();

    qs(a);

    write();

    return 0;
}