Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8265085
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T04:40:39+00:00 2026-06-08T04:40:39+00:00

I have a strong use-case to define my own sorting algorithm, which is faster

  • 0

I have a strong use-case to define my own sorting algorithm, which is faster than the fastest in stl and by exploiting some nice properties of the underlying data I basically can sort in O(n).

So far so good, now the problem is that I would like to offer a generic interface which will fit any type of container e.g. T* or std::vector<T> etc, as long as couple of key concepts apply e.g.

  • there is a valid operator [] available to access the elements of the collection
  • the elements of the collection support the “less than” comparable concept.

To get ideas I went to the header file <std_algo.h> and found the function interface below which matches exactly what I’m looking for except one detail, I don’t see how looping through the underlying _RandomAccessIterator will be auto-vectorized by the compiler disregard of the container type and this is my question … is there a way I can have it all? auto-vectorization + generic interface disregard of the underlying collection type?

I think the code below won’t auto-vectorize due to the “non-canonical” loop pattern while (__last - __first > int(_S_threshold)) and conditions like if (__depth_limit == 0) but this last I won’t need in my algorithm. So I see the auto-vectorization would be prevented by the non-canonical type of loop.

template<typename _RandomAccessIterator, typename _Compare> 
inline void sort(_RandomAccessIterator __first, _RandomAccessIterator __last, _Compare __comp)
{
    typedef typename iterator_traits<_RandomAccessIterator>::value_type
    _ValueType;

    // concept requirements
    __glibcxx_function_requires(_Mutable_RandomAccessIteratorConcept<
    _RandomAccessIterator>)
    __glibcxx_function_requires(_BinaryPredicateConcept<_Compare, _ValueType,
    _ValueType>)
    __glibcxx_requires_valid_range(__first, __last);

    if (__first != __last)
    {
        std::__introsort_loop(__first, __last,
        std::__lg(__last - __first) * 2, __comp);
        std::__final_insertion_sort(__first, __last, __comp);
    }
}

The loop in question looks like this:

// This is a helper function for the sort routine.
template<typename _RandomAccessIterator, typename _Size, typename _Compare> 
void __introsort_loop(_RandomAccessIterator __first, _RandomAccessIterator __last, _Size __depth_limit, _Compare __comp)
{
    while (__last - __first > int(_S_threshold))
    {
        if (__depth_limit == 0)
        {
            _GLIBCXX_STD_A::partial_sort(__first, __last, __last, __comp);
            return;
        }
        --__depth_limit;
        _RandomAccessIterator __cut =
        std::__unguarded_partition_pivot(__first, __last, __comp);
        std::__introsort_loop(__cut, __last, __depth_limit, __comp);
        __last = __cut;
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T04:40:42+00:00Added an answer on June 8, 2026 at 4:40 am

    The Standard C++ Library uses iterators in standard algorithms, such as sort(). This allows the algorithm implementation to ignore the exact details of the underlying container. Also, this approach doesn’t allow for indexing with operator[]().

    With that in mind, I have two suggestions for you to consider:

    1) Revise your specialized sort to use iterators, rather than operator[]() to access elements in the container. If it is possible to maintain your desired O(n) speed, then this is probably the most desirable method for flexibility.

    2) Implement your sort with the container class templatized. Something like

    template <class Container, class Compare>
    void sort(Container cont, Compare comp);
    

    should do the trick.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a strong use case for pre-allocating all the memory I need upfront
I have a special use case which I do not yet know how to
I have this case class define: class Protocol(protocol:String) object Protocol { def apply(protocol:String) :Protocol
(Note that the examples are a simplification of my use case.) I have a
Lets say you have a class SomeClass which has its own implementation of toString()
I have the following use case: I would like to be able to push
I am attempting to use reflection to determine which Properties of a Type have
In C++, if I want to define some non-local const string which can be
I have a string in XML format and I want to use this string
I have converted my Datatable to json string use the following method... public string

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.