template< class T >

auto    sorted_merge( const T* a1, const int size1, const T* a2, const int size2, T* result )

{

    auto    head    = (T*)a1;

    auto    ohead   = (T*)a2;

    auto    otail   = (T*)a1 + size1;

    auto    tail    = (T*)a2 + size2;

    auto    limit   = otail;


    if( a1[ 0 ] > a2[ 0 ] )

    {

        std::swap( head, ohead );

        limit = tail;

    }


    if( a1[ size1 - 1 ] > a2[ size2 - 1 ] )

        std::swap( tail, otail );


    while( head < limit && *head < *ohead )

        *result++ = *head++;


    if( limit == tail )

        std::swap( head, ohead );


    while( head < otail )

        *result++ = *head <= *ohead ? *head++ : *ohead++;


    while( ohead < tail )

        *result++ = *ohead++;


    return  result;

}


루프 내 조건 비교가 최대 2개 존재하게 했다능.

참고.