C++11编译期(模板元编程)快速排序算法

#include 
using namespace std;

template< int...vs >
struct array { using type = array< vs... >; };

template< typename L, typename M, typename R > struct _concat;
template< int...v1, int...v2, int...v3 >
struct _concat< array< v1... >
              , array< v2... >
              , array< v3... >
              > : array< v1..., v2..., v3... > {};

template< int x, int y >
using _le = typename conditional< (x <= y), true_type, false_type >::type;

template< int x, int y >
using _gt = typename conditional< (x > y), true_type, false_type >::type;

template< template< int x, int y > class F, typename L > struct _filter;
template< template< int x, int y > class F >
struct _filter< F, array<> > : array<> {};
template< template< int x, int y > class F, int v1 >
struct _filter< F, array< v1 > > : array<> {};
template< template< int x, int y > class F, int v1, int v2, int...vs >
struct _filter< F, array< v1, v2, vs... > >
        : _concat< typename conditional< F< v2, v1 >::value
                                       , array< v2 >
                                       , array<>
                                       >::type
                 , typename _filter< F, array< v1, vs... > >::type
                 , array<>
                 > {};

template< typename T > struct _quick_sort;
template<> struct _quick_sort< array<> > : array<> {};
template< int x > struct _quick_sort< array< x > > : array< x > {};
template< int x, int...xs >
struct _quick_sort< array< x, xs... > > 
        : _concat< typename _quick_sort< typename _filter< _le
                                                         , array< x, xs... >
                                                         >::type
                                       >::type
                 , array < x >
                 , typename _quick_sort< typename _filter< _gt
                                                         , array< x, xs... >
                                                         >::type
                                       >::type
                 > {};

template< int...vs >
void print_array( array< vs... > ) {
    static constexpr int arr[sizeof...(vs)] = { vs... };
    for (size_t i = 0; i < sizeof...(vs); ++i) {
        cout << arr[i] << " ";
    }
    cout << endl;
}

int main() {
    using input = array< 22, 4, 94, 70, 61, 65, 45, 95, 48, 88,
                         0, 35, 96, 62, 18, 72, 54, 10, 77, 76,
                         64, 26, 41, 20, 36, 58, 43, 25, 2, 78,
                         87, 40, 86, 21, 17, 99, 15, 28, 46, 31,
                         75, 39, 33, 60, 44, 19, 32, 91, 92, 74,
                         5, 83, 29, 79, 100, 55, 81, 23, 71, 42,
                         12, 49, 63, 30, 85, 97, 9, 69, 57, 1,
                         93, 3, 50, 80, 51, 8, 34, 27, 56, 98,
                         89, 38, 52, 16, 13, 6, 47, 59, 68, 7,
                         82, 24, 66, 90, 11, 84, 73, 37, 14, 67 >;
    using result = _quick_sort< input >;
    print_array(result{});
    return 0;
}

你可能感兴趣的:(c++,排序算法,算法)