Cod sursa(job #1885966)

Utilizator Kln1000Ciobanu Bogdan Kln1000 Data 20 februarie 2017 16:12:37
Problema Sortare prin comparare Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 4.44 kb
#include <iostream>
#include <vector>
#include <fstream>

using namespace std;

/**

                                          `-.`'.-'
                                       `-.        .-'.
                                    `-.    -./\.-    .-'
                                        -.  /_|\  .-
                                    `-.   `/____\'   .-'.
                                 `-.    -./.-""-.\.-      '
                                    `-.  /< (()) >\  .-'
                                  -   .`/__`-..-'__\'   .-
                                ,...`-./___|____|___\.-'.,.
                                   ,-'   ,` . . ',   `-,
                                ,-'   ________________  `-,
                                   ,'/____|_____|_____\
                                  / /__|_____|_____|___\
                                 / /|_____|_____|_____|_\
                                ' /____|_____|_____|_____\
                              .' /__|_____|_____|_____|___\
                             ,' /|_____|_____|_____|_____|_\
,,---''--...___...--'''--.. /../____|_____|_____|_____|_____\ ..--```--...___...--``---,,
                           '../__|_____|_____|_____|_____|___\
      \    )              '.:/|_____|_____|_____|_____|_____|_\               (    /
      )\  / )           ,':./____|_____|_____|_____|_____|_____\             ( \  /(
     / / ( (           /:../__|_____|_____|_____|_____|_____|___\             ) ) \ \
    | |   \ \         /.../|_____|_____|_____|_____|_____|_____|_\           / /   | |
 .-.\ \    \ \       '..:/____|_____|_____|_____|_____|_____|_____\         / /    / /.-.
(=  )\ `._.' |       \:./ _  _ ___  ____  ____ _    _ _ _ _ _  _ __\        | `._.' /(  =)
 \ (_)       )        \/                                            \       (       (_) /
  \    `----'          """"""""""""""""""""""""""""""""""""""""""""""        `----'    /
   \   ____\__                                                              __/____   /
    \ (=\     \                                                            /     /-) /
     \_)_\     \                                                         /     /_(_/
          \     \                                                        /     /
           )     )  _                                                _  (     (
          (     (,-' `-..__                                    __..-' `-,)     )
           \_.-''          ``-..____                  ____..-''          ``-._/
            `-._                    ``--...____...--''                    _.-'
                `-.._                                                _..-'
                     `-..__          CIOBY KNOWS ALL           __..-'
                           ``-..____                  ____..-''
                                    ``--...____...--''

*/

class parser{
    public:
        parser() {}/// use file.close()?
        parser(const char *file_name){
            input_file.open(file_name,ios::in | ios::binary);
            input_file.sync_with_stdio(false);
            index=0;
            input_file.read(buffer,SIZE);}///input iterators?
        inline parser &operator >>(int &n){
            for (;buffer[index]<'0' or buffer[index]>'9';inc());
            n=0;
            for (;'0'<=buffer[index] and buffer[index]<='9';inc())
                n=10*n+buffer[index]-'0';
            return *this;}
    private:
        fstream input_file;
        static const int SIZE=0x8000;///512kb file buffer?
        char buffer[SIZE];
        int index=0;
        inline void inc(){
            if(++index==SIZE)
                index=0,input_file.read(buffer,SIZE);}
};

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

vector <int> v;
int n;

int partition(int low,int high)
{   int mid=(low+high)>>1,pivot=v[mid],i=low-1,j=high+1;
LOOP:
    for (;v[i]<pivot;++i);
    for (;v[j]>pivot;--j);
    if (i>=j)
        return j;
    swap(v[i],v[j]);
    goto LOOP;
}

void quicksort(int low,int high)
{
    int pivot;
    if (low<high)
    {
        pivot=partition(low,high);
        quicksort(low,pivot);
        quicksort(pivot+1,high);
    }
}

void citire()
{
    f>>n;
    v.resize(n);///push_back faster?
    for (int i=0; i<n; ++i)
        f>>v[i];
}

inline void afisare()
{
    for (int i=0; i<n; ++i)
        t<<v[i]<<" ";
}

int main()
{
    citire();
    quicksort(0,n-1);
    afisare();
    return 0;
}