Cod sursa(job #3237753)

Utilizator mariusn01Marius Nicoli mariusn01 Data 12 iulie 2024 17:04:15
Problema Sortare prin comparare Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 0.97 kb
#include <fstream>
#include <vector>
#include <algorithm>

using namespace std;

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

struct nod {
    int inf;
    nod *st;
    nod *dr;
};

nod *r;
int n, i, x;

void insereaza(nod *&r, int x) {
    if (r == NULL) {
        r = new nod;
        r->inf = x;
        r->st = r->dr = NULL;
    } else {
        if (r->inf < x)
            insereaza(r->dr, x);
        else
            insereaza(r->st, x);
    }
}

void afiseaza(nod *r) {
    if (r) {
        afiseaza(r->st);
        fout<<r->inf<<" ";
        afiseaza(r->dr);
    }
}

int main() {
    ifstream fin ("algsort.in");
    ofstream fout("algsort.out");

    fin>>n;
    vector<int> v;
    for (i=1;i<=n;i++) {
        fin>>x;
        v.push_back(x);
    }

    random_shuffle(v.begin(), v.end());

    for (i=1;i<=n;i++) {
        fin>>x;
        insereaza(r, x);
    }



    afiseaza(r);

    return 0;
}