What magic uses `std :: sort` internally, which makes it much faster?

I have a trivial quicksort implementation that passes:

template <typename IteratorType>
void quicksort(IteratorType begin, IteratorType end)
{
  if (begin != end)
  {
    const auto pivot = *(begin + distance(begin, end) / 2);
    const IteratorType sep = std::partition(begin, end, [pivot](typename IteratorType::value_type v){ return (v < pivot); });

    if (sep != begin)
    {
      quicksort(begin, sep);
    }

    if (sep != end)
    {
      quicksort(sep + 1, end);
    }
  }
}

Testing it on an array of 1,000,000 elements takes about forever (6300 ms), before recursion sometimes dies, and std::sorttakes about 30 ms.

Of course, I do not expect my crappy implementation to be as fast as std::sort, but how can there be such a huge difference?

I understand that it std::sortuses something more complicated than simple high-speed sorting (I think it is introsort), which prevents me from going too far to the recursion level and so on. But still, is there something obvious that I don’t see that can explain such a huge difference?

arraw , , , , .

+5
2

( 3) .

Pivot , N * log (n) N ^ 2.

+1

, (quicksort ), , , , . , , .

Rinky-dink ++ 11 , , .

+1

All Articles