Cod sursa(job #3253845)

Utilizator Manolea_Teodor_StefanManolea Teodor Stefan Manolea_Teodor_Stefan Data 4 noiembrie 2024 23:29:56
Problema Sortare prin comparare Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 3.63 kb
// Author: Manolea Teodor-Stefan
#include <bits/stdc++.h>
//#pragma GCC optimize("O3,unroll-loops")
//#pragma GCC target("avx2,bmi,bmi2,popcnt,lzcnt")

//#define fin cin
//#define fout cout

namespace ano {
    // typenames
    template<typename T>
    using  _iterator = typename std::vector<T>::iterator;
    template<typename T>
    using _queue = typename std::queue<T>;
    template<typename T>
    using _pair = typename std::pair<T,T>;

    // functions
    template<typename T>
    /**
     * Merge
     *
     * @brief collates the vector
     * @tparam T
     * @param begin and end iterators of a vector
     */
    void Merge(_iterator<T> __begin, _iterator<T> __end) {
        _queue<T> __q;
        const size_t __dist = (__end - __begin) / 2;
        _iterator<T> __mid = __begin + __dist;
        _iterator<T> __i = __begin;
        _iterator<T> __j = __mid;

        while (__i < __mid || __j < __end) {
            if (__i < __mid && __j < __end) {
                if (*__i < *__j) {
                    __q.push(*__i);
                    __i++;
                } else {
                    __q.push(*__j);
                    __j++;
                }
            } else {
                if (__i < __mid) {
                    __q.push(*__i);
                    __i++;
                } else if (__j < __end) {
                    __q.push(*__j);
                    __j++;
                }
            }
        }
        while (!__q.empty()) {
            *__begin = __q.front();
            __q.pop();
            __begin++;
        }
    }

    template<typename T>
    /**
     * Merge sort
     *
     * @brief sorts the vector using merge sort
     * @tparam T
     * @param begin and end iterators of a vector
     */
    void MergeSort(_iterator<T> __begin, _iterator<T> __end) {
        const size_t __dist = __end - __begin;
        _iterator<T> __mid = __begin + __dist / 2;

        if (__dist > 1) {
            MergeSort<T>(__begin, __mid);
            MergeSort<T>(__mid, __end);
            Merge<T>(__begin,__end);
        }
    }

    template<typename T>
    /**
     * Partition
     *
     * @brief sorts the vector using quick sort
     * @tparam T
     * @param begin and end iterators of a vector
     * @return pivot iterator
     */
    _pair<_iterator<T>> Partition(_iterator<T> __begin, _iterator<T> __end) {
        _iterator<T> __pivot = __begin + (__end - __begin) / 2;
        __end--;
        while (__begin < __end) {
            while (*__begin < *__pivot) {
                __begin++;
            }
            while (*__pivot < *__end) {
                __end--;
            }

            if (__begin <= __end) {
                std::swap(*__begin,*__end);
                __begin++;
                __end--;
            }
        }
        return {__begin,__end};
    }

    template<typename T>
    /**
     * Quick sort
     *
     * @brief sorts the vector using quick sort
     * @tparam T
     * @param begin and end iterators of a vector
     */
    void QuickSort(_iterator<T> __begin, _iterator<T> __end) {
        const size_t __dist = __end - __begin;
        if (__dist > 1) {
            _pair<_iterator<T>> __pivot = Partition<T>(__begin,__end);
            QuickSort<T>(__begin, __pivot.second);
            QuickSort<T>(__pivot.first, __end);
        }
    }
};
using namespace std;
ifstream fin("algsort.in");
ofstream fout("algsort.out");

int n;
vector<int> v;
int main() {
    fin >> n;
    v.resize(n);
    for (int& i : v) {
        fin >> i;
    }

    random_shuffle(v.begin(),v.end());
    swap(v.front(),v.back());
    ano::QuickSort<int>(v.begin(),v.end());
    for (const int i : v) {
        fout << i << ' ';
    }
    return 0;
}