Cod sursa(job #965001)

Utilizator AlexandruValeanuAlexandru Valeanu AlexandruValeanu Data 22 iunie 2013 21:54:30
Problema Sortare prin comparare Scor 40
Compilator cpp Status done
Runda Arhiva educationala Marime 0.86 kb
#include <iostream>
#include <fstream>
#include <algorithm>

using namespace std;

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

int a[500005];

struct node
{
    int value;

    node* left;
    node* right;
};

void update( node*& root, int val )
{
    if ( !root )
    {
        root = new node;
        root->value = val;

        return;
    }

    if ( val <= root->value )
        update( root->left, val );

    if ( val > root->value )
        update( root->right, val );
}

void print( node *root )
{
    if ( root->left )
        print( root->left );

    g << root->value << " ";

    if ( root->right )
        print( root->right );
}

int main()
{
    node *R = NULL;

    int n;

    f >> n;

    for ( int i = 0; i < n; i++ )
        f >> a[i];

     random_shuffle ( a, a + n );

    for ( int i = 0; i < n; i++ )
        update(R,a[i]);

    print(R);
}