Cod sursa(job #1176383)

Utilizator SpiderManSimoiu Robert SpiderMan Data 26 aprilie 2014 00:19:51
Problema Sortare prin comparare Scor 100
Compilator cpp Status done
Runda Arhiva educationala Marime 5.58 kb
# include <algorithm>
# include <cstdio>
# include <vector>
using namespace std ;

const char *FIN = "algsort.in", *FOU = "algsort.out" ;

namespace util {
#define NIL -1
#define MAX 8
#pragma region template <class T> class DArray
template <class T> class DArray {
    T* col;
    int lun, maxC;
public:
    #pragma region DArray<T> :: Iterator
    class Iterator {
    protected:
        int current;
        DArray* list;
    public:
        Iterator(int position, const DArray* list): current(position) {
            this->list = const_cast< DArray* >(list);
        }
        virtual Iterator& operator++() {
            ++this->current;
            return *this;
        }
        virtual Iterator operator++(int) {
            Iterator tmp = *this;
            ++*this;
            return tmp;
        }
        virtual Iterator& operator--() {
            --this->current;
            return *this;
        }
        virtual Iterator operator--(int) {
            Iterator tmp = *this;
            --*this;
            return tmp;
        }
        T& operator*() const {
            return list->col[current];
        }
        T* operator->() {
            return &list->col[current];
        }
        bool operator ==(const Iterator& rhs) {
            return current == rhs.current;
        }
        bool operator !=(const Iterator& rhs) {
            return current != rhs.current;
        }
        Iterator operator +(const int n) {
            Iterator tmp = *this;
            tmp.current += n;
            return tmp;
        }
        void operator +=(const int n) {
            this->current += n;
        }
        Iterator operator -(const int n) {
            Iterator tmp = *this;
            tmp.current -= n;
            return tmp;
        }
        int operator -(const Iterator& rhs) {
            return this->current - rhs.current;
        }
        bool operator >(const Iterator& rhs) {
            return (this->current > rhs.current);
        }
        bool operator >=(const Iterator& rhs) {
            return (this->current >= rhs.current);
        }
        bool operator <(const Iterator& rhs) {
            return (this->current < rhs.current);
        }
        bool operator <=(const Iterator& rhs) {
            return (this->current <= rhs.current);
        }
    };
    #pragma endregion
    DArray() :
        lun(0), maxC(MAX) {
        col = new T[MAX];
    }
    DArray(const DArray& da) {
        lun = da.lun;
        maxC = da.maxC;
        col = new T[maxC];
        for (int i = 0; i < lun; ++i)
            col[i] = da.col[i];
    }
    DArray& operator =(DArray da) {
        DArray::swap(*this, da);
        return *this;
    }
    bool operator ==(const DArray& da) const {
        if (lun != da.lun) return 0;
        for (int i = 0; i < lun; ++i)
            if (col[i] != da.col[i]) return 0;
        return 1;
    }
    virtual ~DArray() {
        delete[] col;
    }
    int size() const {
        return lun;
    }
    T& operator[](const int k) {
        return col[k];
    }
    const T& operator[](const int k) const {
        return col[k];
    }
    void PushBack(const T& e) {
        if (lun == maxC) resize();
        col[lun++] = e;
    }
    void remove(const int poz) {
        for (int i = poz; i < lun; ++i)
            col[i] = col[i + 1];
        lun -= 1;
    }
    Iterator Begin() const {
        Iterator it(0, this);
        return it;
    }
    Iterator End() const {
        Iterator it(lun, this);
        return it;
    }
private:
    static void swap(DArray& da1, DArray& da2) {
        std::swap(da1.lun, da2.lun);
        std::swap(da1.maxC, da2.maxC);
        std::swap(da1.col, da2.col);
    }
    void resize() {
        T* tmp = new T[maxC <<= 1];
        for (int i = 0; i < lun; ++i)
            tmp[i] = col[i];
        delete[] col;
        col = tmp;
    }
};
#pragma endregion

} /* namespace darray */

using namespace util;

DArray <int> V ;
int N ;

template <class Iter>
inline Iter SelectPivot(Iter begin, Iter end) {
    begin += (end - begin) / 2;
    return begin;
}

template <class Iter, class Cmp>
void QuickSort(Iter begin, Iter end, Cmp less) {
    Iter left = begin;
    Iter right = end;
    --right;
    if (left >= right) return;
    Iter pivot = SelectPivot(left, right);
    auto val = *pivot;

    //std::swap(*pivot, *right);

    //Iter pivot_position = left;
    while (left <= right) {
        for (; less(*left, val); ++left);
        for (; less(val, *right); --right);
        if (left <= right) {
            std::swap(*left, *right);
            ++left, --right;
        }
    }

    QuickSort(begin, ++right, less);
    QuickSort(left, end, less);
}

template <class It, class Cmp>
inline void Sort(It First, It Last, Cmp FCmp) {
    QuickSort(First, Last, FCmp);
}

struct cmpLess {
    template <class _Ty1, class _Ty2>
    bool operator()(const _Ty1& _Left, const _Ty2& _Right) const {
        return _Left < _Right;
    }
};

template <class It>
inline void Sort(It First, It Last) {
    //Sort(First, Last, cmpLess());
    Sort(First, Last, [](decltype(*First)& a, decltype(*First) b) {
         return a < b;
    });
}

int main ( void ) {
    freopen ( FIN, "r", stdin ) ;

    scanf ( "%d", &N ) ;
    for ( int i = 0, x; i < N; ++i ) {
        scanf ( "%d", &x ) ;
        V.PushBack ( x ) ;
    }

    Sort(V.Begin(), V.End());

    freopen ( FOU, "w", stdout ) ;
    for ( int i = 0; i < N; ++i ) {
        printf ( "%d ", V[i] ) ;
    }
}